Check if files in a directory are still being written using Windows Batch Script
- by FMFF
Hello.
Here's my batch file to parse a directory, and zip files of certain type
REM Begin ------------------------
tasklist /FI "IMAGENAME eq 7za.exe" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end
for /f "delims=" %%A in ('dir C:\Temp\*.ps /b') do (
"C:\Program Files\7-Zip\cmdline\7za.exe" a -tzip -mx9 "C:\temp\Zip\%%A.zip" "C:\temp\%%A"
Move "C:\temp\%%A" "C:\Temp\Archive"
)
:end
del search.log
REM pause
exit
REM End ---------------------------
This code works just fine for 90% of my needs. It will be deployed as a scheduled task.
However, the *.ps files are rather large (minimum of 1GB) in real time cases. So the code is supposed to check if the incoming file is completely written and is not locked by the application that is writing it.
I saw another example elsewhere, that suggested the following approach
:TestFile
ren c:\file.txt c:\file.txt
if errorlevel 0 goto docopy
sleep 5
goto TestFile
:docopy
However this example is good for a fixed file. How can I use that many labels and GoTo's inside a for loop without causing an infinite loop? Or is this code safe to be used in the For Loop?
Thank you for any help.