1

I write a .bat to automate generating and compiling a cmake project. The batch file will

  1. optional rmdir build
  2. mkdir build if not exist
  3. cd build
  4. cmake .. generate a nmake project from upper folder
  5. nmake compile project

the source:

@ECHO OFF
set "OpenCV_LIB=C:\Program Files (x86)\OpenCV2.1"
echo !! OpenCV Library: [ %OpenCV_LIB% ]
IF NOT EXIST "%OpenCV_LIB%" (
    echo Can't find OpenCV Library, please change OpenCV_LIB setting
    GOTO END
)

if %PROCESSOR_ARCHITECTURE%==x86   set BUILD_ARCH=x86
if %PROCESSOR_ARCHITECTURE%==AMD64 set BUILD_ARCH=x86_amd64
if %PROCESSOR_ARCHITECTURE%==IA64  set BUILD_ARCH=x86_IPF
echo !! Target architecture [ %BUILD_ARCH% ]
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" %BUILD_ARCH%

cd "sikuli-script"
set /p answer=Do you want to make clean first? (Y/N):
if %answer% == Y ( IF EXIST build rmdir /s /q build )
if %answer% == y ( IF EXIST build rmdir /s /q build )

IF NOT EXIST build mkdir build
cd build

IF NOT EXIST CMakeCache.txt ( cmake -G "NMake Makefiles" -D CMAKE_BUILD_TYPE=Release -D "OpenCV_DIR=%OpenCV_LIB%" .. )
nmake

:END
pause

After i run this .bat over and over, i notice that cd build failed sometimes, so i change my code to test it:

@ECHO ON
set /p answer=Do you want to make clean first? (Y/N):
if %answer% == Y ( IF EXIST build rmdir /s /q build )
if %answer% == y ( IF EXIST build rmdir /s /q build )
:DO_MKDIR_CD
IF NOT EXIST build mkdir build
cd build
if errorlevel 1 (
    pause
    GOTO DO_MKDIR_CD
)

It comes out the error happens every time choose to clean up build, even the build is at small size (e.g. after interrupted compiling, size 3.08 MB, 183 files, 67 dirs).

!! OpenCV Library: [ C:\Program Files (x86)\OpenCV2.1 ]
!! Target architecture [ x86_amd64 ]
Setting environment for using Microsoft Visual Studio 2010 x64 cross tools.

D:\repo\sikuli>cd "sikuli-script"

D:\repo\sikuli\sikuli-script>set /p answer=Do you want to make clean first? (Y/N):
Do you want to make clean first? (Y/N):y

D:\repo\sikuli\sikuli-script>if y == Y (IF EXIST build rmdir /s /q build  )

D:\repo\sikuli\sikuli-script>if y == y (IF EXIST build rmdir /s /q build  )

D:\repo\sikuli\sikuli-script>IF NOT EXIST build mkdir build

D:\repo\sikuli\sikuli-script>cd build
Access is denied.

D:\repo\sikuli\sikuli-script>if errorlevel 1 (
pause
 GOTO DO_MKDIR_CD
)
Press any key to continue . . .

So every time after remove and recreate build, the cd build fails, then cmake start messing up my source tree.

The error check and loop trying could fix this problem, but why is the file system not stable? or am i writing it in a wrong way?

4

1 回答 1

0

That build directory check:

IF NOT EXIST build mkdir build

Will create the directory if both directory and file named "build" are not exist.

One way to check a directoy existence is:

if exist build\nul echo directory exist

If you want to make sure a directory exist use:

if not exist build\nul mkdir build

or better:

if not exist build\nul (
  mkdir build
  if not exist build\nul (
    echo mkdir failed
    goto :eof
  )
)
于 2012-08-20T12:25:56.973 回答