1

Hi there :) I am currently writing a batch file to automate FTP transfers on a regular basis. The name and format of the file does not change. However the data in that file is updated by the host every ~10 minutes. I need to acquire each version and store them in the same directory (with different names)

The aim of the code is this:

  • Download a file by specific name from a server and save it with a timestamp added to the name.
  • Check to see if a file already exists that has the same value in the "date modified field"
  • If such a file exists, delete it.
  • Wait 5 minutes, then repeat.

So far I have come fairly close to this with my batch file. The problem I am facing is this. About half the time two files with the same "date modified" value being saved. I believe that the error is occurring in the code that finds the second newest and newest files is not producing the intended results, meaning that files are not marked for deletion as they should be. I suspect that it is something to do with the nature of batch file execution that I do not fully understand.

Here is a sample of the results I have produced running the file for an hour: sample output. Note that several of the files with the same date are included twice, whereas others are only included once.

Thanks for reading and (if you do) answering :)

Finally here is the code I am using.

@echo off
cls

:start
rem Find the currently newest file
for /F "delims=|" %%I IN ('dir "*.xml" /B /O:D') DO set SecondNewestFile=%%I

winscp.com /ini=nul /script=ten_minute_sync.txt

rem Find the "new" newest file
for /F "delims=|" %%J IN ('dir "*.xml" /B /O:D') DO set NewestFile=%%J

echo SecondNewestFile = %SecondNewestFile%
echo NewestFile = %NewestFile%

rem Count the number of files in the directory to determine if we need to check the date of the file
for /f %%A in ('dir "*.xml" /a-d-s-h /b ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%

if %cnt% gtr 1 (
    rem Check the dates of the files and if there is a duplicate, remove it.
    echo Checking dates of files

    for %%a IN (%SecondNewestFile%) do set DATE1=%%~ta
    for %%b IN (%NewestFile%) do set DATE2=%%~tb

    echo Date of the NewestFile is %DATE1%
    echo Date of the SecondNewestFile is %DATE2%

    if "%DATE1%"=="%DATE2%" (
        echo Deleting duplicate file
        del /F %NewestFile%
    )
)

rem Wait 5 minutes before executing the script again
echo "Waiting 5 minutes before checking again..."
timeout 300

goto start
Stringers
  • 127
  • 12

0 Answers0