Detecting if a file is open in a batch file

后端 未结 3 729
灰色年华
灰色年华 2020-12-30 14:02

Say I have a batch file for carrying out a long build and at the end it creates an EXE. If I forget to close the app down before I start the build, the link phase fails whe

3条回答
  •  有刺的猬
    2020-12-30 14:34

    @echo off
    
    :start
    ren filename filename        // rename file to same name
    if errorlevel 1 goto errorline
    echo command successfull file is not in use anymore
    goto end
    :errorline
    echo Rename wasnt possible, file is in use try again in 5seconds
    timeout /T 5
    goto :start
    :end
    exit
    

    renames file to the same name, if possible the script jumps to end whichs exit the code, otherwize it produces error code1 and jumps to errorline, after a timeout of 5 seconds it jumps to :start and script starts from beginning.

提交回复
热议问题