0

We have an integration engine, which creates txt files for the opposite host system. Our system writes files to local folder. I created a bat file like that and scheduled for every 1 minute:

xcopy /v /y E:\*.txt Z:\ 
move E:\*.txt E:\Processed  (for backup purpose)

Z:\ is the mapping folder of the host system and that folder is being scanned frequently. If a file is processed it will be deleted immediately by the host system.

My problem is, sometimes files are written duplicated. I mean users see the activies as twice. I think that's because of this; consider a moment which the host system processes my file at the same time of xcopy executes and things get messed up. I know it is impossible to happen those at the same time but maybe network lags causing machines to behave like that?

Any ideas?

Thanks

eyetouch
  • 1
  • 1

1 Answers1

0

What about using following batch file?

@echo off
set "SleepTimeInSeconds=60"

rem For sleep time using PING command 1 must be added
rem because the first trial is always successful.
set /A SleepTimeInSeconds+=1

:NextRun
echo %TIME% Searching for files to copy and move ...

for %%I in (E:\*.txt) do (
    copy /B /V /Y /Z "%%I" Z:\
    move /Y "%%I" E:\Processed\
)

%SystemRoot%\System32\ping.exe 127.0.0.1 -n %SleepTimeInSeconds% >nul
goto NextRun

It copies each file separately on the server and then moves the file to the backup directory.

See How to sleep for 5 seconds in Windows's Command Prompt for the sleep time solution using command PING. You could perhaps also use command TIMEOUT depending on version of Windows which would be even better.

This batch file must be started only once on startup of machine and should run forever until shutdown of machine. Don't run this batch file as scheduled task every minute.

The scheduled task starting the batch file every minute caused the problem with processing files twice because if copying and then moving all *.txt files does not finish within 1 minute caused for example by a temporary network problem, another batch process was started doing the same as the still running batch process.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115