I created an batch file that builds and executes FTP scripts every week for a special case where I needed to transfer a file from one FTP server to another through an intermediary that had security access to both. The file basically:
- Grabs 2 files from server X and places them in a backup folder with the name of "[JobName]_yyyymmdd.txt".
- Verifies the files have been downloaded, then deletes them from server X.
- Uploads the files to server Y from the backup folder, changing the name of the server Y file in the process.
- Verifies (by parsing a temporary sub-log) that server Y acknowledges the receipt of the file (See my Reverse Parsing post).
- Adds the sub-log to the main log.
- Deletes the sub-log and the FTP working scripts (clean-up).
I had a problem - I wanted to be able to automatically manage the backup files, getting rid of files older than 5 weeks. At first, I thought I'd create a separate text file to keep track of the files and delete them. Then I hit upon a more elegant solution:
Code: Select all
:: Determine working / backup directory
set vLocalDir="%~dp0%Backup\"
:: Create backup / working directory if the directory does not exist.
if not exist "%vLocalDir%" (mkdir "%vLocalDir%")
:: Set current directory to backup / working directory.
cd /d "%vLocalDir%"
...
:: Count the backup files.
for /f "delims=*" %%a in ('dir /a:-d /b') do (
set /a vCntr+=1
)
:: Delete backup files older than 5 runs ago, which should be 5 weeks ago.
if %vCntr%==12 (
for /f "delims=*" %%a in ('dir /a:-d /b ^| sort /+9') do (
if !vCntr! GTR 10 (
del /q /s %%a
)
set /a vCntr-=1
)
)
Because the first part of the file name was different for both files, I couldn't just sort the directory files within the dir command. I could have simply reconstructed the way I created the filename and put the date first, but I just didn't like the idea. I was especially proud of the solution because this is the first time I've ever used a pipe in my code and I really didn't understand how it worked. In fact, I put the sort function first. thinking that the pipe somehow took the place of the input file. Then I realized that DOS worked sequentially - it would have to have the results of the dir command before it could feed it into the sort function.
I know that this is no big deal to you guys [Ed especially - Hi Ed!
![Very Happy :D](./images/smilies/icon_biggrin.gif)
![Very Happy :D](./images/smilies/icon_biggrin.gif)