检查目录中的文件是否仍在使用 Windows 批处理脚本写入 [英] Check if files in a directory are still being written using Windows Batch Script
问题描述
这是我的批处理文件,用于解析目录和特定类型的 zip 文件
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 ---------------------------
这段代码可以满足我 90% 的需求.它将作为计划任务进行部署.
This code works just fine for 90% of my needs. It will be deployed as a scheduled task.
然而,*.ps 文件在实时情况下相当大(最小 1GB).所以代码应该检查传入的文件是否完全写入并且没有被写入它的应用程序锁定.
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
然而,这个例子适用于固定文件.如何在 for 循环中使用那么多标签和 GoTo 而不会导致无限循环?或者这段代码在 For 循环中使用是否安全?
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?
感谢您的帮助.
推荐答案
以下是我最终使用的批处理文件脚本.它非常适合该要求.我希望它可以帮助和我有相同要求的人.
The following is the batch file script I used eventually. It is working very well for the requirement. I hope it helps someone with the same requirement as mine.
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\ZIP\*.txt /b') do (
:TestFile
"C:\Program Files\7-Zip\cmdline\7za.exe" a -tzip -mx9 "C:\temp\Zip\ZipDone%%A.zip" "C:\temp\zip\%%A"
Move "C:\temp\ZIP\%%A" "C:\Temp\ZIP\Archive"
)
:end
del search.log
REM pause
Exit
这篇关于检查目录中的文件是否仍在使用 Windows 批处理脚本写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!