jueves, 18 de noviembre de 2010

Avoid uploading duplicated files in FTP using MSDOS script

I was uploading files using the script I made in a previous post, and all was working smoothly until I reach a directory full of images used for an address book application, there were about 2,000 files. The current version of the script made no comparison for file existence (actually it deletes the old file in the server before attempting to save the new one) or date comparison. In this case, and assuming those files change don’t change too often, I start thinking in a new process to remove the duplicated files and only upload the non existing files.

I made a new FTP script to read the remote files and then delete the existing files from the local PC. This process is a dumb process and only task is to delete all files from the list. It is not aware of date changes or new file versions. Saying these, if you use this script, always work with a backup of your files, never with your original files as they might be deleted.

The script usage is:

deleteDuplicatedFiles.cmd <Root directory to validate>

Current version of the script has no recursive processes implemented

The operation is as follows:

1. Set working variables and defaults

set user=<ftp user>
set pass=<ftp password>
set site=<ftp site>
set base=<remote path to validate>
set file=<FTP commands file>
set outputFile=<local file to store file listing>

2. Start creating the commands file

echo user %user% %pass% >> %file%
echo cd %base% >> %file%
echo ls >> %file%
echo bye >> %file%

3. Execute the commands´ file

ftp -n %site% < %file% > %outputFile%

4. Call to delete the preexisting files

call :deleteFiles "%workDir%" %outputFile%

5. The deleteFiles subroutine reads all lines from the output file and, assuming is a file name, it validate its existing using another subroutine

FOR /F "usebackq delims==" %%i IN (`type %localFile%`) do (
call :deleteSingleFile %%i
)

6. I’m using deleteSingleFile in case another validation is required or needed to implement, currently it only check the file existence and then delete the file

if exist %deleteFile% del %deleteFile%

Here you can find a copy of the script in case you want to try it: deleteDuplicatedFiles.zip

DISCLAIMER: this script is delivered as is and I take no responsibility of any deleted or damage file. Use this script cautiously and always work on a backup to preserve the original files.

No hay comentarios: