Batch file 用户输入应替换另一个文件的行

Batch file 用户输入应替换另一个文件的行,batch-file,Batch File,请帮助,这是我正在寻找的,它应该要求日志\outputpath,用户提供的路径应该替换text1.txt -text1.txt包含 c:\temp 当用户提供logs\output path时,它应仅替换text1.txt路径上方的内容 这是我得到的批处理代码,它不工作 @echo off echo. SET /P USERDEST=Please enter output\Logs path:c:\temp\path echo output\logs:c:\temp\path call:DoR

请帮助,这是我正在寻找的,它应该要求日志\outputpath,用户提供的路径应该替换text1.txt

-text1.txt包含 c:\temp

当用户提供logs\output path时,它应仅替换text1.txt路径上方的内容

这是我得到的批处理代码,它不工作

@echo off
echo.
SET /P USERDEST=Please enter output\Logs path:c:\temp\path
echo output\logs:c:\temp\path

call:DoReplace "C:\temp" "c:\temp\path" test1.txt
exit /b

:DoReplace
echo ^(Get-Content "%3"^) ^| ForEach-Object { $_ -replace %1, %2 } ^| Set-Content %4>Rep.ps1
Powershell.exe -executionpolicy ByPass -File Rep.ps1
if exist Rep.ps1 del Rep.ps1
echo Done
pause

您可以在批处理中使用变量事实上,您只设置了一个变量,但之后不使用它

@echo off
echo.
rem set Default ( set /p variable remains unchanged, if empty Input):
set "USERDEST=c:\temp\path"
SET /P "USERDEST=Please enter output\Logs path (or ENTER for Default): "
echo output\logs: %USERDEST%
rem it's better to enclose your Parameters in quotes (because contained spaces will be problematic):
call:DoReplace "C:\temp" "%USERDEST%" "test1.txt"
exit /b

:DoReplace
rem remove surrounding quotes with around %3 with %~3:
echo ^(Get-Content "%~3"^) ^| ForEach-Object { $_ -replace %1, %2 } ^| Set-Content %4>Rep.ps1
rem (where do you take %4 from? You call with only three Arguments)
Powershell.exe -executionpolicy ByPass -File Rep.ps1
if exist Rep.ps1 del Rep.ps1
echo Done
pause

我是一个使用批处理文件的初学者,所以请容忍我的错误和误解,下面是使用代码后发生的情况。正如您在下面看到的,输入了一个新的路径c:\temp\1234,还显示了我给出的回音路径,并在下面抛出了错误。仅供参考..test1.txt文件存在于执行路径中,test1.txt已设置了c:\temp路径,我期望的是我的新路径c:\temp\1234应该已经将test1.txt文件的c:\temp更新为c:\temp\1234请输入output\Logs path或输入默认值:c:\temp\1234 output\Logs:c:\temp\1234 Set Content:无法绑定输入对象,因为它不包含绑定所有必需参数所需的信息:path AtE:\Auto\Rep.ps1:1 char:99+Get Content test1.txt | ForEach对象{$|-replace C:\temp,C:\temp\1234}| Set Content rem从何处获取%4?你只需要三个参数就可以调用。我使用test2.txt作为调用行的第四个参数进行了测试,结果很好。