批处理文件读取和修改文本文件
好吧,基本上,我在纯文本记事本文件中有一个完整的链接列表,每个链接都在单独的行上。我想做的就是在每个链接之前添加一些文本,特别是:127.0.0.1 和几个空格。
所以这...
somelink.com
变成了...
127.0.0.1 somelink.com
现在您可能已经猜到我正在尝试编辑文本文件的内容以使其可用作 Windows 中的 HOSTS 文件。
所以我想要一些批处理文件代码,在 .bat 文件中可执行,它基本上读取记事本文本文件,然后在每行的开头添加“127.0.0.1”并在其上添加文本。我猜想对于具有一定 MS DOS 和批处理文件代码知识的人来说,这可能是一段非常简单的代码,但那肯定不是我,而且我写过的唯一批处理文件都是在像现在这样的帮助下完成的。
感谢您提前提供的所有帮助,非常感谢。
Okay, so basically, I have a whole list of links in a plain text Notepad file, each link on a seperate line. All I am wanting to do is to add a bit of text before each link, specifically: 127.0.0.1 and a couple of spaces.
So this...
somelink.com
becomes this...
127.0.0.1 somelink.com
By now you've probably already guessed that I am trying to edit the contents of a text file to make it useable as a HOSTS file in Windows.
So I am wanting some batch file code, executable in a .bat file, which basically reads a Notepad text file, and then add "127.0.0.1 " at the beginning of each line with text on it. I am guessing this is probably a very simple piece of code for someone with some knowledge of MS DOS and batch file code, but that most certainly isn't me, and the only batch files I have ever written have been with help like now.
Thanks for any and all help in advance with this, it really is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读
HELP FOR
,然后在命令提示符下尝试这里是一些解释和一些注意事项,以使用更完整的功能扩展它并将其放入 BAT 文件
FOR 是迭代命令超过输入文本文件的行。请阅读 Microsoft 文档,网址为 http://www .microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
input.txt
是包含您的列表的文本文件域名,它必须位于当前目录output.txt< /code> 将是包含前缀为 127.0.0.1 的域名列表的结果文件,它将在当前目录中创建
如果要创建BAT文件,需要把FOR命令移过来稍微编辑一下,改一下
%a
循环变量名称为%%a
。然后,您可以将 BAT 文件放置在当前目录(您的输入所在的位置以及将创建输出的位置)中。
或者,您可以将 BAT 文件放置在其他位置。在这种情况下,您需要使用其完整路径来调用它。
或者你甚至可以将它放在一个特殊的目录中(我有自己的
C:\Program Files\CMD
)并将其添加到 PATH 系统变量中。请参阅此处 www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true 如何更改当前会话的 PATH。在这里 ss64.com/nt/path.html 您可能会找到一些关于如何使 PATH 永久更改的说明。此外,您可能会想为 BAT 文件添加一些灵活性,而不是使用恒定的
input.txt
和output.txt
文件名,而是将它们替换为%1
和%2
分别代表 BAT 文件的第一个和第二个命令行参数。然后,用户可能想要使用文件名中包含空格的文件。他们可能会用引号
"
在名称周围指定它们。在这种情况下,您需要在 FOR 命令中添加一些咒语usebackq
,以免在用户使用时造成严重破坏引号。最后,您需要决定在输出文本文件已存在的情况下该怎么做,您可能需要考虑防止覆盖。
因此,将所有这些内容放在一起,这里有一个简短的 BAT 文件,可帮助您开始...
欢迎使用 BAT 编程并享受!
read
HELP FOR
and then try this in a command prompthere is some explanation and some considerations to extend it with a little more complete functionality and put it in a BAT file
FOR is the command to iterate over the lines of your input text file. Read microsoft documentation at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
input.txt
is the text file that contains your list of domain names, it must reside in the current directoryoutput.txt
will be the result file that will contain the list of domain names prefixed with 127.0.0.1, it will be created in the current directoryIf you want to create a BAT file, you need to move the FOR command and edit it a little bit, changing the
%a
loop variable names to be%%a
.You can then place the BAT file either in the current directory, where your input resided and where the output will be created.
Alternativelly, you may place your BAT file, elsewhere. In that case, you need to invoke it with its full path.
Or you may even place it in a special directory (I have my own
C:\Program Files\CMD
) and add it in the PATH system variable. See here www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true how you can change your PATH for your current session. And here ss64.com/nt/path.html you may find some explanation on how to make the PATH change permanent.Also, you might be tempted to add some flexibility to your BAT file, instead of having constant
input.txt
andoutput.txt
filename, replace them with%1
and%2
which represent the first and second command line parameters of your BAT file.the user might then want to use files which contain blanks in their filenames. They might specify them surrounding the names with quotes
"
. In that case, you need to add some incantationusebackq
in the FOR command for it to not break havoc when the user uses quotes.Finally, you will need to decide what to do in case the output text file already exists, you migh want to consider prevent overwriting.
So putting all this pieces together, here is a short BAT file to get you started...
welcome to BAT programming and enjoy!
开始了!
这将读取文本文件的前四行,然后将您的内容和每一行放回其来源行中。
玩得开心!
here we go!
This will Read the first four lines of the text file, and then put in your stuff and each line back into the line it came from.
Have Fun!
除了 PA. 的答案之外,如果您需要特定数量的空格,您可以将它们放入变量中并将其添加到命令中。
所以输出将是
批处理文件风格:
In addition to PA.'s answer, if you require a specific number of spaces, you can throw them into a variable and add it to the command as well.
So the output will be
Batch-File flavor: