【问题标题】:Windows batch - Delayed expansion removes exclamation markWindows 批处理 - 延迟扩展删除感叹号
【发布时间】:2013-06-28 17:32:51
【问题描述】:

我编写了批处理脚本将CSS文件复制到某个目录,问题是当我打开目标目录中的CSS文件时,感叹号被删除

原始 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. 最终文件被复制到目标目录

如果您需要更多说明,请告诉我,提前致谢。

批次(sn-p):

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

更新

以下是我根据建议工作的解决方案。不同之处在于,按照指示, 在脚本开头设置 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

【问题讨论】:

  • 禁用延迟扩展,在您的 sn-p 中没有必要。
  • 这是必要的,因为我有 !filename!.css 否则无法转换。
  • 看不到。 filename 一开始就不会在resettheme 中更改,所以%filename%!filename! 应该是一样的...
  • hmm 但是当我设置 DisableDelayedExpansion 时,结果文件名是 !filename!.css 而不会被“foobar.css”之类的东西替换
  • )) > Batch_tmp\!fileName!.css 中的!filename! 替换为%filename% 并禁用脚本中的延迟扩展。

标签: windows batch-file


【解决方案1】:

如果我是你,我会把它放在这里...

(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

所以 - 我只能说“为我工作!”

【讨论】:

  • 感谢您的更新,您是对的。我想我还有其他一些事情(代码的其他部分)阻止了获得正确的值。在我清除该问题后,您的解决方案有效。
猜你喜欢
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 2012-10-15
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
相关资源
最近更新 更多