2013-06-28 44 views
1

我寫了批處理腳本將CSS文件複製到某個目錄,問題是當我在目標目錄中打開CSS文件時,會刪除感嘆號。Windows批處理 - 延遲擴展刪除感嘆號

原來的CSS包含感嘆號如樣式:

.foo 
{ 
    color: pink !important; 
} 

我的CSS結果變得

.foo 
{ 
    color: pink important; 
} 

我看着這一現象的根本原因和依據:exclamation point being removed constantly in batch file

但我無法弄清楚我需要在哪裏禁用延遲擴展。

僅供參考,我的一批是做如下:

  1. 由逗號分隔每個主題串中,找到啓動和某些字符串相匹配的行號結束。

  2. 現在我有開始和結束行號(例如500和1000)

  3. 拷貝除了線500和1000

  4. 之間

  5. 定稿文件被複制到目的地目錄中的文件內容

請讓我知道,如果您需要更多的澄清,在此先感謝。

批次(段):

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 
+0

禁用延遲擴展,在你的代碼段是沒有必要的。 – Endoro

+0

這是必要的,因爲我有!名!它的CSS不能以其他方式轉換。 –

+1

不能看到這一點。'filename'沒有內改變' resettheme'一旦它建立在開始,所以'%filename%'和'!filename!'應該是一樣的... – Magoo

回答

4

我把它放在這裏是我,你......

(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 

,但不保證......由OP :(

報道


結果令人失望這裏是我的測試設置:

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
:: force directories, start and end 
SET runtime_dest=c:\temp 
SET filename=test 
SET start=2 
SET end=4 

:: create batch_temp & clear 

MD "%cd%\Batch_tmp" 2>NUL >NUL 
DEL "%cd%\Batch_tmp\*?*" /F /Q 

:: make copy of original .css 
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 
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!WITHSETLOCAL_ENDLOCAL.css 

ECHO ============================ 
ECHO Batch_tmp\%fileName%_tmp.css 
TYPE Batch_tmp\%fileName%_tmp.css 
ECHO =======^^^^^^^^=================this is the original file 
ECHO Batch_tmp\%fileName%.css 
TYPE Batch_tmp\%fileName%.css 
ECHO =======^^^^^^^^=================this is the result with delayed expansion enabled 
ECHO Batch_tmp\%fileName%WITHSETLOCAL_ENDLOCAL.css 
TYPE Batch_tmp\%fileName%WITHSETLOCAL_ENDLOCAL.css 
ECHO ========^^^^^^^^================this is the result with the advised setlocal/ENDLOCAL IN place 
GOTO :eof 

和結果:

C:\temp\test.css 
1 File(s) copied 
creating tmp copy... 
coping all line except section... 
============================ 
Batch_tmp\test_tmp.css 
.foo 
omit me 
and me 
and me 
{ 
    color: pink !important; 
} 
=======^^^^=================this is the original file 
Batch_tmp\test.css 
.foo 
{ 
    color: pink important; 
} 
=======^^^^=================this is the result with delayed expansion enabled 
Batch_tmp\testWITHSETLOCAL_ENDLOCAL.css 
.foo 
{ 
    color: pink !important; 
} 
========^^^^================this is the result with the advised setlocal/ENDLOCAL IN place 

所以 - 我能說的是「我的作品!「

+0

感謝您的提示,但感嘆號仍然被刪除.. –

+0

t hank的更新,你是對的。我想我還有其他一些東西(代碼的其他部分)阻止了正確的價值。清除該問題後,您的解決方案正常工作。 –