I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
相关问题
- Inheritance impossible in Windows Runtime Componen
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
- How can I have a python script safely exit itself?
- I want to trace logs using a Macro multi parameter
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- Is there a non-java, cross platform way to launch
- Compile and build with single command line Java (L
- CosmosDB emulator can't start since port is al
Download Cygwin (free) and use unix-like commands at the Windows command line.
Your best bet: sed
I know I am late to the party..
Personally, I like the solution at: - http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
We also, use the Dedupe Function extensively to help us deliver approximately 500 e-mails daily via SMTP from: - https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
and these both work natively with no extra tools or utilities needed.
REPLACER:
DEDUPLICATOR (note the use of -9 for an ABA number):
Thanks!
When you work with Git on Windows then simply fire up
git-bash
and usesed
. Or, when using Windows 10, start "Bash on Ubuntu on Windows" (from the Linux subsystem) and usesed
.Its a stream editor, but can edit files directly by using the following command:
-i
option is used to edit in place on filename.-e
option indicates a command to run.s
is used to replace the found expression "foo" with "bar" andg
is used to replace any found matches.Note by ereOn:
If you want to replace a string in versioned files only of a Git repository, you may want to use:
git ls-files <eventual subfolders & filters> | xargs sed -i -e 's/foo/bar/g'
which works wonders.
This is one thing that batch scripting just does not do well.
The script morechilli linked to will work for some files, but unfortunately it will choke on ones which contain characters such as pipes and ampersands.
VBScript is a better built-in tool for this task. See this article for an example: http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx
I don't think there's a way to do it with any built-in commands. I would suggest you download something like Gnuwin32 or UnxUtils and use the
sed
command (or download onlysed
):Use powershell in .bat - for Windows 7+
encoding utf8 is optional, good for web sites