So I want to create a script that takes 3 arguments: path to file, exact word to replace, and with what to replace it. How to create such thing?
所以我想创建一个带有3个参数的脚本:文件路径,要替换的确切单词,以及替换它的内容。怎么创造这样的东西?
Generally I want it to have an API like this:
通常我希望它有这样的API:
script.bat "C:/myTextDoc.xml" "_WORD_TO_REPLACE_" "WordTo Use"
3 个解决方案
#1
7
I have written something like 2 batch scripts in my life, but here's how to take input from the command line:
在我的生活中,我写了两个批处理脚本,但是这里是如何从命令行获取输入:
script.bat filepath find replace
%1 = filepath, %2 = find, %3 = replace
To do replacement, do something like:
要做替换,请执行以下操作:
for /f "tokens=1,* delims=]" %%A in ('"type %1|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~2=%~3%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
(taken directly from the link posted by @russ, with the variable numbers changed.)
(直接取自@russ发布的链接,变量编号已更改。)
I think this should work for you.
我认为这对你有用。
#2
7
Use fnr utility its better than other famous utility since it can search and replace based on regular expressions. Also for the UI lovers you can configure options in UI and it can generate command line string which can then be used in your script. Very easy to use even as command line stirng.
使用fnr实用程序比其他着名实用程序更好,因为它可以基于正则表达式进行搜索和替换。此外,对于UI爱好者,您可以在UI中配置选项,它可以生成命令行字符串,然后可以在您的脚本中使用。即使作为命令行搅拌也很容易使用。
Find it here http://findandreplace.codeplex.com/
在这里找到它http://findandreplace.codeplex.com/
Also it is single exe without any dependicies, so easy to use.
它也是没有任何依赖性的单一exe,因此易于使用。
Example:
例:
fnr --cl --dir "" --fileMask "hibernate.*" --useRegEx
--find "find_str_expression" --replace "replace_string"
#3
3
A quick google search found this:
快速谷歌搜索发现:
http://www.dostips.com/?t=Batch.FindAndReplace
http://www.dostips.com/?t=Batch.FindAndReplace
#1
7
I have written something like 2 batch scripts in my life, but here's how to take input from the command line:
在我的生活中,我写了两个批处理脚本,但是这里是如何从命令行获取输入:
script.bat filepath find replace
%1 = filepath, %2 = find, %3 = replace
To do replacement, do something like:
要做替换,请执行以下操作:
for /f "tokens=1,* delims=]" %%A in ('"type %1|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~2=%~3%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
(taken directly from the link posted by @russ, with the variable numbers changed.)
(直接取自@russ发布的链接,变量编号已更改。)
I think this should work for you.
我认为这对你有用。
#2
7
Use fnr utility its better than other famous utility since it can search and replace based on regular expressions. Also for the UI lovers you can configure options in UI and it can generate command line string which can then be used in your script. Very easy to use even as command line stirng.
使用fnr实用程序比其他着名实用程序更好,因为它可以基于正则表达式进行搜索和替换。此外,对于UI爱好者,您可以在UI中配置选项,它可以生成命令行字符串,然后可以在您的脚本中使用。即使作为命令行搅拌也很容易使用。
Find it here http://findandreplace.codeplex.com/
在这里找到它http://findandreplace.codeplex.com/
Also it is single exe without any dependicies, so easy to use.
它也是没有任何依赖性的单一exe,因此易于使用。
Example:
例:
fnr --cl --dir "" --fileMask "hibernate.*" --useRegEx
--find "find_str_expression" --replace "replace_string"
#3
3
A quick google search found this:
快速谷歌搜索发现:
http://www.dostips.com/?t=Batch.FindAndReplace
http://www.dostips.com/?t=Batch.FindAndReplace