2

我的脚本有问题:

@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

4 回答 4

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
于 2013-07-15T20:27:39.897 回答
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
于 2013-07-15T23:35:02.547 回答
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!
)

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

于 2013-07-16T00:49:53.660 回答
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
于 2013-07-18T11:38:32.147 回答