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

时间:2015-09-19 05:03:32

标签: batch-file

请帮助,以下是我要找的内容,它应该询问logs\output路径,用户提供的路径应该替换为text1.txt

--text1.txt包含 c:\temp

当用户提供logs\output路径时,它应仅替换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

1 个答案:

答案 0 :(得分:2)

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

@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