Reputation: 520
I have a .bat file with these lines executed by Jenkins CI job. It should remove all subdirectories in specified directory except two matching IF conditions.
SET EXPORT_ROOT=C:\WWW\LocalUser\myfolder
SET EXPORT_BUILD_ROOT=%EXPORT_ROOT%\build
SET EXPORT_BUILD_PATH=%EXPORT_BUILD_ROOT%\26
SET LAST_EXPORT_BUILD_PATH=C:\WWW\LocalUser\myfolder\build\25
cd %EXPORT_BUILD_ROOT%
FOR /D %G IN (*) DO (
IF NOT ("%EXPORT_BUILD_ROOT%\%G" == "%LAST_EXPORT_BUILD_PATH%") IF NOT ("%EXPORT_BUILD_ROOT%\%G" == "%EXPORT_BUILD_PATH%") (
rd /s /q "%G"
)
)
cd "%DEPLOY_DIR%"
In Jenkins CI log a see:
C:\WWW\LocalUser\myfolder\deploy>cd C:\WWW\LocalUser\myfolder\build
G" == "C:\WWW\LocalUser\myfolder\build\57") IF NOT ("EXPORT_BUILD_PATHG" ) ) was unexpected at this time.
C:\WWW\LocalUser\myfolder\build>FOR /D G" == "C:\WWW\LocalUser\myfolder\build\57") IF NOT ("EXPORT_BUILD_PATHG" ) )
Build step 'Execute Windows batch command' marked build as failure
Upvotes: 1
Views: 82
Reputation: 80211
FOR /D %G IN (*) DO (
IF NOT "%G" == "%LAST_EXPORT_BUILD_PATH%" IF NOT "%G" == "%EXPORT_BUILD_PATH%" (
rd /s /q "%G"
)
)
Each %G
referring to the metavariable
(loop-control variable) must be %%G
Upvotes: 2
Reputation: 38719
You need to change it more like this:
FOR /D %%G IN (*) DO (
IF /I NOT "%EXPORT_BUILD_ROOT%\%%G"=="%LAST_EXPORT_BUILD_PATH%" (
IF /I NOT "%EXPORT_BUILD_ROOT%\%%G"=="%EXPORT_BUILD_PATH%" RD/S/Q "%%G")
)
The positioning of your parentheses being important as well as the doubling of the %
in a batch file meatvariable.
Upvotes: 4