开发者

Is there a way of checking file availability in a DOS script?

Background: I have a post-build process that copies a file to another location. It looks like this:

copy $(TargetPath) "%programfiles%\mypath"

This step can fail if the another process is using the file. The step is not critical, so if possible I would like to ignore the failure. To do this I need the script to check to determine if the file is being use开发者_开发知识库d by another process.

Question: Is there a way of testing a file in a DOS script to determine if it is being used by another process?


You can see if the file exists, then rename a .dll/.exe even if it is being executed. Might want to do .pdb files, too.

IF EXIST $(TargetName).deleted del $(TargetName).deleted
IF EXIST $(TargetName).pdb.deleted del $(TargetName).pdb.deleted
IF EXIST "%programfiles%\mypath\$(TargetName)$(TargetExt)" REN "%programfiles%\mypath\$(TargetName)$(TargetExt)" $(TargetName).deleted
IF EXIST "%programfiles%\mypath\$(TargetName)$(TargetExt)" REN "%programfiles%\mypath\$(TargetName)$.pdb" $(TargetName).pdb.deleted
copy $(TargetPath) "%programfiles%\mypath"


Ok, so I needed to check the errorlevel after performing the copy, so that I could handle the exit properly. The solution is below:

copy $(TargetPath) "%programfiles%\mypath"
if errorlevel 1 goto BuildProcessFailed

goto BuildProcessOK
:BuildProcessFailed
echo BUILDPROCESS FAILED FOR PROJECT $(ProjectName)
goto ExitBuildProcess
:BuildProcessOK
echo BUILDPROCESS OK FOR PROJECT $(ProjectName)

:ExitBuildProcess
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜