【问题标题】:Renaming txt file using data in text file使用文本文件中的数据重命名 txt 文件
【发布时间】:2014-02-21 12:40:42
【问题描述】:

我的第一篇文章,所以请温柔。

我有一个批处理文件,可以将 ipconfig/all 数据输出到我桌面上的 txt 文件中。

是否可以使用文件中包含的ip地址或mac地址数据来重新命名文件。

标记

【问题讨论】:

    标签: batch-file ip-address rename mac-address


    【解决方案1】:

    是的,有可能。

    首先你需要找到地址。例如。与

    findstr /C:"  IPv6 Address" foo.txt
    

    那你需要把地址变成一个变量:

    set Address=
    for /f "delims=: tokens=1*" %%a in ('findstr /C:"  IPv6 Address" foo.txt') do (
      if not defined Address=%%b
    )
    

    注意这里使用了%%b,尽管循环变量是%%a。那是因为标记化创建了新变量。此外,我们只使用第一个地址是因为可能有多个地址,但这只是一种模糊的启发式方法,因为非 NIC 设备是在它们之前还是之后并不是一成不变的。

    然后我们需要清理地址,因为冒号在文件名中无效:

    set Address=%Address::=_%
    

    并重命名文件

    ren foo.txt %Address%.txt
    

    我猜,您肯定可以弄清楚如何将其适应 IPv4 或 MAC 地址。

    【讨论】:

    • 谢谢乔伊。抱歉,如果我有点厚,但是如何将此代码添加到我的 bat 文件“ipconfig /all >”
    • 您说,您已经有一个批处理文件,可以将其输出到文件中。
    【解决方案2】:

    如果您调整条款,这允许您选择所需的 MAC 地址。

    此时它会选择在ipconfig /all 输出中找到的第一个以太网设备,并使用 MAC 地址创建一个 txt 文件。

    @echo off
    for /f "delims=" %%a in ('ipconfig /all ^|findrepl "Ethernet" /e:"Physical Address" /b:"Physical Address" ^|findrepl ".*Physical Address.*: (.*)" "$1" ^|findrepl /o:1:1 ') do (
    ipconfig /all >"%%a.txt"
    )
    

    上面使用了一个名为 findrepl.bat 的辅助批处理文件 - 下载地址:https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

    将findrepl.bat 放在与批处理文件相同的文件夹中或路径上。

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 2019-05-12
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      相关资源
      最近更新 更多