我正在尝试从蝙蝠文件中搜索文本文件中的特定字符串。如果该字符串存在,请在下一行之后添加一个新字符串。我似乎无法正常工作下面的代码。有任何想法吗?
这是我在文本文件中搜索的字符串。 [/Script/MyGame.Mode]
这是文本文件的外观。
[/Script/Config.Mode]
Something here 1
Something here 2
[/Script/MyGame.Mode]
Something here 1
Something here 2
[/Script/Edit.Mode]
Something here 1
Something here 2
这就是我想要的样子。
[/Script/Config.Mode]
Something here 1
Something here 2
[/Script/MyGame.Mode]
RedirectReferences=(PackageName="%Package%",PackageURLProtocol="%PackageURLProtocol%",PackageURL="%WebAddress%/%Package%%Ext%",PackageChecksum="")
Something here 1
Something here 2
[/Script/Edit.Mode]
Something here 1
Something here 2
这是我到目前为止的代码。
@echo off
:GETINFO
echo.
echo.
cls
echo.
echo Let's get some information for your config.
echo Note: The information you enter below is case sensitive. You can copy and paste.
echo.
echo Here's a Package Name example: "DM-MyTest-WindowsNoEditor"
echo.
set /p Package=Enter Package Name:
echo.
echo.
echo.
echo The Package URL Protocol will be "http" or "https"
echo.
set /p PackageURLProtocol=Enter Package URL Protocol:
echo.
echo.
echo.
echo Here's a WebAddress example: "www.myredirect.com/test" (Don't add the trailing /)
set /p WebAddress=Enter Redirect(WebAddress)URL:
echo.
echo.
echo.
echo The file extention is usually ".pak"
echo.
set /p Ext=Enter Map File Extention:
echo.
cls
echo.
echo Please wait... Currently Creating Test References.
:SHOWLINE
echo.
set NewURL=RedirectReferences=(PackageName="%Package%",PackageURLProtocol="%PackageURLProtocol%",PackageURL="%WebAddress%/%Package%%Ext%",PackageChecksum=""^^)
pause
:WRITENEW
set inputfile=game.txt
set outputfile=game.temp.txt
(for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do (
if "%%~a"=="[/Script/MyGame.Mode]" call echo %NewURL%
echo %%a
))>>"%outputfile%"
echo.
pause
当我在命令提示符控制台中运行发布的代码时,我看到语法错误:
)目前是出乎意料的。
显然,NewURL
当在循环中扩展时,内部的括号会破坏事物。
一个简单的解决方案是使用call
技巧来延迟扩展:
call echo %%NewURL%%
或者:
setlocal enableDelayedExpansion & echo !NewURL! & endlocal
或用两次转义结束括号^^
(一次为一次set
,另一次为循环内的扩展值):
set NewURL=.............PackageChecksum=""^^)
另一个问题是输出文件名与输入文件名相同,但是无法将输出重定向到与您正在读取的文件相同的文件中。
将输出名称更改为其他文件。然后在循环完成后替换原始文件:
set inputfile=game.txt
set outputfile=game.temp.txt
...................
))>>"%outputfile%"
move/y "%outputfile%" "%inputfile%"
并更改新字符串的顺序以在找到的行之后打印它,只需在内部循环内交换两行即可:
echo %%a
if "%%~a"=="[/Script/MyGame.Mode]" call echo %%NewURL%%
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句