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

时间:2021-01-20 02:06:36

Please help, here is what i'm looking for, it should ask for logs\outputpath, user supplied path should replace text1.txt

请帮忙,这是我正在寻找的,它应该询问logs \ outputpath,用户提供的路径应该替换text1.txt

--text1.txt contains c:\temp

--text1.txt包含c:\ temp

As and when user supply logs\output path, it should replace only above path of text1.txt.

当用户提供日志\输出路径时,它应仅替换text1.txt的上述路径。

This is the batch code i have got, it's not working.

这是我得到的批处理代码,它不起作用。

@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 个解决方案

#1


2  

You can use variables in Batch (in fact, you set one variable, but you don't use it afterwards)

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

@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

#1


2  

You can use variables in Batch (in fact, you set one variable, but you don't use it afterwards)

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

@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