.bat文件无法转义字符

时间:2013-07-15 19:06:44

标签: batch-file

我的剧本中有问题:

@echo on
setlocal EnableDelayedExpansion
pushd C:\Users\Oskars.Abuhovs\Desktop\testJstoReplace
for /f "delims=" %%a in ('dir /b /a-d-h-s') do (
  set newFile=C:\Users\Oskars.Abuhovs\Desktop\tempjs\%%a
  if exist !newfile! del /f /q !newfile!
  for /f "delims=" %%L in (%%a) do (
    set newLine=%%L
    echo !^newLine:dojo/i18n=ggggggggg^^!dddd! >> !newFile!
  )
)
popd
pause

问题是连续的:

echo !^newLine:dojo/i18n=ggggggggg^^!dddd! >> !newFile!

哪个角色!没有逃脱,我在setlocal EnableDelayedExpansion读到了!字符应使用^^进行转义,但不会发生。我怎么能逃脱这个角色?

编辑:

我想替换文件中的部分字符串。此特定字符串dojo/i18n应替换为包含!的另一个字符串。例如dojo/i18n!mycomputer

整个脚本一起从文件夹中读取文件,然后逐行搜索每个文件,搜索此字符串:dojo/i18n,当找到它时,字符串应该用早期提到的文本dojo/i18n!mycomputer替换,包含!我试图逃避!有两个^^的字符,因为它应该在setlocal EnableDelayedExpansion时工作,但这个组合:^^!在这一行中不起作用,但当我尝试简单echo ^^!时,它起作用了。

所以我在问如何逃避!那条线上的角色?

提前致谢!

4 个答案:

答案 0 :(得分:2)

这可能对您有用:

@echo off
setlocal EnableDelayedExpansion
pushd "C:\Users\Oskars.Abuhovs\Desktop\testJstoReplace"
for /f "delims=" %%a in ('dir /b /a-d-h-s') do (
  set "newFile=C:\Users\Oskars.Abuhovs\Desktop\tempjs\%%~a"
  if exist "!newfile!" del /f /q "!newfile!"
  for /f "delims=" %%L in ("%%~a") do call:doline "%%~L" "!newfile!"
)
popd
PAUSE
goto:eof

:doline
SETLOCAL DISABLEDELAYEDEXPANSION
set "newLine=%~1"
echo %newLine:dojo/i18n=ggggggggg!dddd% >> "%~2"
ENDLOCAL
goto:eof

答案 1 :(得分:0)

您可以重写代码以避免延迟扩展:

@echo on
pushd C:\Users\Oskars.Abuhovs\Desktop\testJstoReplace
set newFile=C:\Users\Oskars.Abuhovs\Desktop\tempjs
for /f "delims=" %%a in ('dir /b /a-d-h-s') do (
  if exist "%newfile%\%%a" del /f /q "%newfile%\%%a"
  for /f "delims=" %%L in ('type "%%a" ') do (
    >>"%newfile%\%%a" echo %%L:dojo/i18n=ggggggggg!dddd
  )
)
popd
pause

答案 2 :(得分:0)

@echo off
setlocal ENABLEDELAYEDEXPANSION
SET dddd=replacement^^!data
for /f "delims=" %%a in ('dir /b /a-d-h-s *.doj') do (
  set newFile=u:\temp\%%a
  if exist !newfile! del /f /q !newfile!
  (
  for /f "delims=" %%L in (%%a) do (
    set newLine=%%L
    FOR /f "tokens=1*delims==" %%Q IN ('set dddd') DO (
     SETLOCAL disabledelayedexpansion
     CALL echo %%newLine:dojo/i18n=ggggggggg%%R%%
     endlocal
    )
  )
  )>>!newfile!
)

这对我有用。文件名和面具自然而然地改变了以保护无辜者。

答案 3 :(得分:0)

感谢您的回答,但最后我决定切换到.vbs以解决我的问题。

使用此代码,如果有人需要它:

Const ForReading = 1    
Const ForWriting = 2

strFilePath = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFilePath)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each testFile in colFiles
    FileToReplace = strFilePath + testFile.Name
    Wscript.Echo FileToReplace
    Set objFile = objFSO.OpenTextFile(FileToReplace, ForReading)
    strText = objFile.ReadAll
    objFile.Close

If InStr(strText, strOldText) Then
    strText = Replace(strText, strOldText, strNewText)
    Set objFile = objFSO.OpenTextFile(FileToReplace, ForWriting)
    objFile.Write strText  'WriteLine adds extra CR/LF
    objFile.Close
End If
    strText = ""
Next