检查,如果仍然被使用Windows批处理脚本写入一个目录中的文件 [英] Check if files in a directory are still being written using Windows Batch Script

查看:260
本文介绍了检查,如果仍然被使用Windows批处理脚本写入一个目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的批处理文件来分析一个目录,某些类型的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 ---------------------------

这code工作得很好,为我的需求90%。它将被部署为一个计划任务。

This code works just fine for 90% of my needs. It will be deployed as a scheduled task.

不过,* .PS文件是相当大的(1GB及以上)的实时情况。所以code为应该检查传入文件被完全写入,并且不是由正在写入它的应用程序锁定。

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

然而该实施例是良好的一个固定文件。我怎么可以使用多个标签和GOTO的内部循环,而不会导致一个无限循环?或者,这是code安全在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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆