2012-11-15 3 views
0

다음은 VS에서 미리 빌드 이벤트로 실행해야하는 프로그램입니다. 직접 명령 줄에서 작동하지만 VS에서는 작동하지 않습니다.어떻게 Visual Studio와 cmd.exe가 배치 파일을 다르게 처리합니까?

@echo off 
:: Direct From cmd.exe 
:: "G:Google Drive\GitHub\toolset\Site\pre-build.bat" Release "G:Google Drive\GitHub\toolset\Site\Web" 
:: From Visual Studio Pre-Build Event 
:: "$(SolutionDir)pre-build.bat" $(ConfigurationName) "$(ProjectDir)" 

if %1==Release (

    if exist %2 (
     set location=%2 
     set dotlessVersion=v1.3.1.0 

     :: Compress Less Files 
     for /r %location% %%a in (*.less) do (
      "%~dp0..\SharedLibs\dotless\%dotlessVersion%\dotless.Compiler.exe" -m -r "%%a" "%%~da%%~pa%%~na.min.css" 
     ) 

     :: Minify js Files 
     for /r %location% %%a in (*.closure.js) do (
      set newFilename=%%a 
      call java -jar "%~dp0..\SharedLibs\Closure Compiler\compiler.jar" --js "%%a" --js_output_file "%%newFilename:.closure.js=.min.js%%" 
     ) 
    ) 
) 
+1

왜 작동하지 않습니까? 무슨 일이야? 출력 창에서 무엇을 볼 수 있습니까? – SLaks

+0

출력 창에 아무 것도 표시되지 않습니다. 'set location = % 2' 다음에'echo value : % location %'를 추가하면 결과는'Value :'입니다. cmd.exe는 변수를 echo로 출력합니다. – roydukkey

답변

1

Visual Studio와는 아무런 관련이 없습니다. 표준 일괄 초급 버그입니다.
예상 한대로 괄호 안에 확장 비율이 작동하지 않습니다.
모든 블록이 실행되기 전에 전체 블록을 구문 분석 할 때 확장됩니다.

그래서 %location%은 확장되지 않고 블록으로 들어가기 전에 값이 확장됩니다.
cmd line에서도 같은 방식으로 작동하지만 배치를 두 번 시작하면 작동하는 것처럼 보이지만 여전히 설정되어 있으므로 올바른 값만 표시됩니다.

어떻게 해결할 수 있습니까?
사용하지 마십시오 (퍼센트 확장). 대신 지연된 확장을 사용하십시오!

setlocal EnableDelayedExpansion 
if %1==Release (

    if exist %2 (
     set location=%2 
     echo !location! 
     .... 
관련 문제