Batch file .bat文件可以';逃逸字符

Batch file .bat文件可以';逃逸字符,batch-file,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 !newfi

我的脚本中有一个问题:

@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!我的计算机

整个脚本一起读取文件夹中的文件,然后逐行搜索每个文件,搜索以下字符串:
dojo/i18n
,找到后,字符串应替换为前面提到的文本
dojo/i18n!我的计算机
,包含!。我试图逃跑!带有两个^^^的字符,因为当
setlocal EnableDelayedExpansion
时它应该工作,但此组合:^^!在这行中不起作用,但当我尝试simple
echo^^时成功了

所以我在问如何逃跑!那一行的人物


提前谢谢

这可能适合您:

@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

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

@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

这对我有用。文件名和掩码自然地被更改以保护无辜者。

谢谢你的回答,但最后我决定切换到
.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

线路应该做什么?您想替换字符串的一部分,什么是
dddddd?请解释更多。@Oskars Abuhovs不可能使用
时,搜索中的code>或替换字符串中的code>本身。当您在百分比扩展中使用
%
时也是如此。@jeb谢谢您的回答,我会记住这一点。
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