我寫了批處理腳本將CSS
文件複製到某個目錄,問題是當我在目標目錄中打開CSS
文件時,會刪除感嘆號。Windows批處理 - 延遲擴展刪除感嘆號
原來的CSS包含感嘆號如樣式:
.foo
{
color: pink !important;
}
我的CSS結果變得
.foo
{
color: pink important;
}
我看着這一現象的根本原因和依據:exclamation point being removed constantly in batch file
但我無法弄清楚我需要在哪裏禁用延遲擴展。
僅供參考,我的一批是做如下:
由逗號分隔每個主題串中,找到啓動和某些字符串相匹配的行號結束。
現在我有開始和結束行號(例如500和1000)
拷貝除了線500和1000
之間
定稿文件被複制到目的地目錄中的文件內容
請讓我知道,如果您需要更多的澄清,在此先感謝。
批次(段):
set themes=Platinum,Granite,Sun,Sky,Burgundy,Leaf,Twilight,Sharepoint 2010,Sharepoint 2013
rem Directory that contains theme css files
set RUNTIME_DEST=%K2DIR%\K2 SmartForms Runtime\Styles\Themes
REM Do something with each substring
call :parse "%themes%"
goto :eof
:parse
setlocal
for %%a in ("%themes:,=" "%") do (
call :getLineNumber %%a
)
endlocal
goto :eof
:getLineNumber
setlocal
echo file name is %~1
set filename=%~1
rem Get start and end line numbers of the unwanted section
for /F "delims=:" %%a in ('findstr /N /C:"PickerCollectionThemeStart" "%RUNTIME_DEST%\%filename%.css"') do (
set start=%%a
)
for /F "delims=:" %%a in ('findstr /N /C:"PickerCollectionThemeEnd" "%RUNTIME_DEST%\%filename%.css"') do (
set end=%%a
)
if not defined start (
echo start not defined...
set start=0
set end=0
)
echo start val = !start!
echo end val = !end!
call :resetTheme "%filename%" "%start%" "%end%"
endlocal
goto :eof
:resetTheme
setlocal
set filename=%~1
set start=%~2
set end=%~3
echo %fileName%
echo %start%
echo %end%
echo ---------------
rem Create a copy file to modify
xcopy "%RUNTIME_DEST%\%filename%.css" "%cd%\Batch_tmp" /y /z /r
echo creating tmp copy...
rem Rename the file so we can modify and create finalized version
ren "Batch_tmp\%fileName%.css" "%fileName%_tmp.css"
echo coping all line except section...
rem Copy all lines, except the ones in start-end section
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "Batch_tmp\%fileName%_tmp.css"') do (
if %%a lss %start% echo(%%b
if %%a gtr %end% echo(%%b
)) > Batch_tmp\!fileName!.css
echo copying to blackpearl dir
rem Finally move the css over to blackpearl directory
setlocal DisableDelayedExpansion
xcopy "Batch_tmp\%fileName%.css" "%RUNTIME_DEST%" /y /z /r
endlocal
endlocal
goto :eof
:eof
pause
UPDATE
下面是我的解決方案,根據配置的建議。不同的是,按照說明, 在腳本開始處設置了EnableDelayedExpansion,以便對整個批處理作用域產生影響,然後僅在其應用的位置設置DisableDelayedExpansion。
set themes=Platinum,Granite,Sun,Sky,Burgundy,Leaf,Twilight,Sharepoint 2010,Sharepoint 2013
rem Directory that contains theme css files
set RUNTIME_DEST=%K2DIR%\K2 SmartForms Runtime\Styles\Themes
REM Do something with each substring
call :parse "%themes%"
goto :eof
:parse
setlocal
for %%a in ("%themes:,=" "%") do (
echo ------------------------
call :getLineNumber %%a
)
endlocal
goto :eof
:getLineNumber
setlocal
echo file name is %~1
set filename=%~1
rem Get start and end line numbers of the unwanted section
for /F "delims=:" %%a in ('findstr /N /C:"PickerCollectionThemeStart" "%RUNTIME_DEST%\%filename%.css"') do (
set start=%%a
)
for /F "delims=:" %%a in ('findstr /N /C:"PickerCollectionThemeEnd" "%RUNTIME_DEST%\%filename%.css"') do (
set end=%%a
)
if not defined start (
echo start not defined...
set start=0
set end=0
)
echo start val = !start!
echo end val = !end!
call :resetTheme "%filename%" "%start%" "%end%"
endlocal
goto :eof
:resetTheme
setlocal
set filename=%~1
set start=%~2
set end=%~3
echo %fileName%
echo %start%
echo %end%
rem Create a copy file to modify
xcopy "%RUNTIME_DEST%\%filename%.css" "%cd%\Batch_tmp" /y /z /r
echo creating tmp copy...
rem Rename the file so we can modify and create finalized version
ren "Batch_tmp\%fileName%.css" "%fileName%_tmp.css"
echo coping all line except section...
rem Copy all lines, except the ones in start-end section
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "Batch_tmp\%fileName%_tmp.css"') do (
setlocal DisableDelayedExpansion
if %%a lss %start% echo(%%b
if %%a gtr %end% echo(%%b
endlocal
)) > Batch_tmp\!fileName!.css
echo copying to blackpearl dir
rem Finally move the css over to blackpearl directory
xcopy "Batch_tmp\%fileName%.css" "%RUNTIME_DEST%" /y /z /r
endlocal
goto :eof
:eof
禁用延遲擴展,在你的代碼段是沒有必要的。 – Endoro
這是必要的,因爲我有!名!它的CSS不能以其他方式轉換。 –
不能看到這一點。'filename'沒有內改變' resettheme'一旦它建立在開始,所以'%filename%'和'!filename!'應該是一樣的... – Magoo