批处理文件读取和修改文本文件

发布于 2025-01-02 21:15:46 字数 463 浏览 3 评论 0原文

好吧,基本上,我在纯文本记事本文件中有一个完整的链接列表,每个链接都在单独的行上。我想做的就是在每个链接之前添加一些文本,特别是: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2025-01-09 21:15:46

阅读 HELP FOR ,然后在命令提示符下尝试

  FOR /F "delims=" %a in (input.txt) do @echo 127.0.0.1   %a >>output.txt

这里是一些解释和一些注意事项,以使用更完整的功能扩展它并将其放入 BAT 文件

  1. FOR 是迭代命令超过输入文本文件的行。请阅读 Microsoft 文档,网址为 http://www .microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx

  2. input.txt 是包含您的列表的文本文件域名,它必须位于当前目录

  3. output.txt< /code> 将是包含前缀为 127.0.0.1 的域名列表的结果文件,它将在当前目录中创建

  4. 如果要创建BAT文件,需要把FOR命令移过来稍微编辑一下,改一下%a循环变量名称为%%a

  5. 然后,您可以将 BAT 文件放置在当前目录(您的输入所在的位置以及将创建输出的位置)中。

  6. 或者,您可以将 BAT 文件放置在其他位置。在这种情况下,您需要使用其完整路径来调用它。

  7. 或者你甚至可以将它放在一个特殊的目录中(我有自己的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 永久更改的说明。

  8. 此外,您可能会想为 BAT 文件添加一些灵活性,而不是使用恒定的 input.txtoutput.txt 文件名,而是将它们替换为 %1%2 分别代表 BAT 文件的第一个和第二个命令行参数。

  9. 然后,用户可能想要使用文件名中包含空格的文件。他们可能会用引号 " 在名称周围指定它们。在这种情况下,您需要在 FOR 命令中添加一些咒语 usebackq,以免在用户使用时造成严重破坏引号。

  10. 最后,您需要决定在输出文本文件已存在的情况下该怎么做,您可能需要考虑防止覆盖。

因此,将所有这些内容放在一起,这里有一个简短的 BAT 文件,可帮助您开始...

    @echo off
    if .%2==. goto help
    if not exist %1 goto helpno1
    if exist %2 goto helpalready2
    FOR /F "usebackq delims=" %%a in (%1) do @echo 127.0.0.1   %%a >>%2
    goto :eof
    :help
    echo you need to specify input and output text files
    goto :eof
    :helpno1
    echo %1 not found
    goto :eof
    :helpalready2
    echo %2 already exist
    goto :eof

欢迎使用 BAT 编程并享受!

read HELP FOR and then try this in a command prompt

  FOR /F "delims=" %a in (input.txt) do @echo 127.0.0.1   %a >>output.txt

here is some explanation and some considerations to extend it with a little more complete functionality and put it in a BAT file

  1. 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

  2. input.txt is the text file that contains your list of domain names, it must reside in the current directory

  3. output.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 directory

  4. If 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.

  5. You can then place the BAT file either in the current directory, where your input resided and where the output will be created.

  6. Alternativelly, you may place your BAT file, elsewhere. In that case, you need to invoke it with its full path.

  7. 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.

  8. Also, you might be tempted to add some flexibility to your BAT file, instead of having constant input.txt and output.txt filename, replace them with %1 and %2 which represent the first and second command line parameters of your BAT file.

  9. 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 incantation usebackq in the FOR command for it to not break havoc when the user uses quotes.

  10. 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...

    @echo off
    if .%2==. goto help
    if not exist %1 goto helpno1
    if exist %2 goto helpalready2
    FOR /F "usebackq delims=" %%a in (%1) do @echo 127.0.0.1   %%a >>%2
    goto :eof
    :help
    echo you need to specify input and output text files
    goto :eof
    :helpno1
    echo %1 not found
    goto :eof
    :helpalready2
    echo %2 already exist
    goto :eof

welcome to BAT programming and enjoy!

水波映月 2025-01-09 21:15:46

开始了!

(
Set /p line1=
Set /p line2=
Set /p line3=
Set /p line4=
)<Filename.txt
echo 127.0.0.1    %line1%>Filename.txt
echo 127.0.0.1    %line2%>>Filename.txt
echo 127.0.0.1    %line3%>>Filename.txt
echo 127.0.0.1    %line4%>>Filename.txt

这将读取文本文件的前四行,然后将您的内容和每一行放回其来源行中。
玩得开心!

here we go!

(
Set /p line1=
Set /p line2=
Set /p line3=
Set /p line4=
)<Filename.txt
echo 127.0.0.1    %line1%>Filename.txt
echo 127.0.0.1    %line2%>>Filename.txt
echo 127.0.0.1    %line3%>>Filename.txt
echo 127.0.0.1    %line4%>>Filename.txt

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!

任性一次 2025-01-09 21:15:46

除了 PA. 的答案之外,如果您需要特定数量的空格,您可以将它们放入变量中并将其添加到命令中。

  SET spaces=          # to the left is 10 spaces
  FOR /F "delims=" %a in (input.txt) do @echo 127.0.0.1%spaces%%a>>output.txt

所以输出将是

127.0.0.1          somelink.com

批处理文件风格:

  SET spaces=          # to the left is 10 spaces
  FOR /F "delims=" %%a in (input.txt) do @echo 127.0.0.1%spaces%%%a>>output.txt

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.

  SET spaces=          # to the left is 10 spaces
  FOR /F "delims=" %a in (input.txt) do @echo 127.0.0.1%spaces%%a>>output.txt

So the output will be

127.0.0.1          somelink.com

Batch-File flavor:

  SET spaces=          # to the left is 10 spaces
  FOR /F "delims=" %%a in (input.txt) do @echo 127.0.0.1%spaces%%%a>>output.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文