AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1542652
Accepted
Steven Loretero
Steven Loretero
Asked: 2020-04-17 13:04:27 +0800 CST2020-04-17 13:04:27 +0800 CST 2020-04-17 13:04:27 +0800 CST

如何批量重命名以 % 为分隔符的 windows 文件?

  • 772

我有很多文件名包含 %. 见下文:

c:\temp>
dir /b
114902_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489879.pdf
114904_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489880.pdf
114906_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491058.pdf
114916_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491059.pdf
114918_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491452.pdf
114920_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491453.pdf
114923_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491454.pdf
115513_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039292647.pdf
115515_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039295860.pdf
115517_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039296218.pdf
115523_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039308592.pdf
115525_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039308593.pdf
120342_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039328905.pdf
120345_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337204.pdf
120348_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337221.pdf
120351_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337461.pdf
120639_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168731.pdf
120640_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168988.pdf
120642_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168993.pdf
120644_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63169027.pdf

这些 % 实际上是 : (%3A) 和 \ (%5C) 的十六进制代码。我无法解释当它们从另一个系统复制时它们是如何得到这种方式的,但这是我必须使用的。为清楚起见,这就是十六进制代码的样子。

114902_T:\Accounting\Payables\APA\Boshart Apr 25\BII_inv_1489879.pdf

我想要做的是从这里重命名文件:

114902_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489879.pdf

对此:

BII_inv_1489879.pdf

我尝试按照其他重命名示例使用 % 作为分隔符,但更复杂的是最后一个分隔符(pdf 文件名)并不总是第 7 个标记。

任何帮助将不胜感激。

windows-10 batch-rename
  • 5 5 个回答
  • 2963 Views

5 个回答

  • Voted
  1. Best Answer
    Keith Miller
    2020-04-17T20:53:59+08:002020-04-17T20:53:59+08:00

    如果您使用的是Windows 10,则应该学习和使用 Powershell进行管理和自动化。它非常强大而且很有趣。

    使用您的示例名称和所需的结果,我选择在 ' %5C ' 上拆分并使用 -1 的索引,它获取数组中的最后一个元素,而不管长度。

    详细:

    $path = 'c:\temp'
    Get-ChildItem $path | Rename-Item -NewName { ($_ -split '%5C')[-1] }
    

    在控制台:

    gci c:\temp | ren -NewName { ($_ -split '%5C')[-1] }
    

    获取子项

    重命名项目

    关于分裂

    字符串操作演示:

    PS C:\...\keith>$oldnames
    114902_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489879.pdf
    114904_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489880.pdf
    114906_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491058.pdf
    114916_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491059.pdf
    114918_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491452.pdf
    114920_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491453.pdf
    114923_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491454.pdf
    115513_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039292647.pdf
    115515_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039295860.pdf
    115517_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039296218.pdf
    115523_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039308592.pdf
    115525_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039308593.pdf
    120342_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039328905.pdf
    120345_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337204.pdf
    120348_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337221.pdf
    120351_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337461.pdf
    120639_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168731.pdf
    120640_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168988.pdf
    120642_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168993.pdf
    120644_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63169027.pdf
    PS C:\...\keith>$oldnames | ForEach{ ($_ -split '%5C')[-1] }
    BII_inv_1489879.pdf
    BII_inv_1489880.pdf
    BII_inv_1491058.pdf
    BII_inv_1491059.pdf
    BII_inv_1491452.pdf
    BII_inv_1491453.pdf
    BII_inv_1491454.pdf
    0039292647.pdf
    0039295860.pdf
    0039296218.pdf
    0039308592.pdf
    0039308593.pdf
    0039328905.pdf
    0039337204.pdf
    0039337221.pdf
    0039337461.pdf
    INV#63168731.pdf
    INV#63168988.pdf
    INV#63168993.pdf
    INV#63169027.pdf
    
    • 15
  2. Meow_ly
    2020-04-17T13:22:03+08:002020-04-17T13:22:03+08:00

    我会编写一个 PowerShell 脚本来遍历文件名并用以下内容重写它们:

    $dir = Get-ChildItem -Path "PUTFULLPATHHERETODIRECTORY"
    foreach($P in $dir) {
        Rename-Item -path $P -newname $P.FullName.Substring($P.FullName.lastindexof("%5C")+3)
    }
    

    首先,我们获取目标文件夹的内容。然后,对于目录中的每个项目,将其重命名为从 "%5C" 模式的最后一个索引开始的名称,并添加 3 个空格以将其删除。

    当然,把完整路径放在我放PUTFULLPATHHERETODIRECTORY的地方。

    • 7
  3. Io-oI
    2020-04-17T13:55:16+08:002020-04-17T13:55:16+08:00

    *5c用作分隔符并通过命令获取目标名称子字符串,set这将获取文件名的最后一部分,而不用担心它是否是标记 5、6、7 等,因为它总是获取最后一个分隔符之后的字符串。 .

    根据 Sponge Belly / DosTips.com 的分隔符将字符串拆分为子字符串

    set "_trick=%_name:*5c=" & set "_name=%"
    

    @echo off && setlocal enabledelayedexpansion
    
    for %%i in (*.pdf)do call :_ren_: "%%~ni" && ren "%%~fi" "!_name!%%~xi"
    
    :_ren_:
    set "_name=%~1" <nul || endlocal && goto :EOF
    set "_trick_=%_name:*5c=" & set "_name=%" && exit /b 
    
    • 在 Powershell 中也是如此
    gci C:\temp\*.pdf|Ren -New {$_ -replace '.*%5C',''}
    

    在此处输入图像描述

    • 4
  4. DavidPostill
    2020-04-17T13:28:06+08:002020-04-17T13:28:06+08:00

    如何批量重命名%分隔符的 windows 文件?

    使用以下批处理文件 (test.cmd):

    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=1-3 delims= " %%a in ('dir /b *.pdf') do (
      set _temp=%%c
      set _name=!_temp:~5!
      ren "%%a %%b %%c" !_name!
      )
    endlocal
    

    例子:

    F:\test>dir /b *.pdf
    114902_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489879.pdf
    114904_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489880.pdf
    115513_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039292647.pdf
    
    F:\test>test
    F:\test>dir /b *.pdf
    BII_inv_1489879.pdf
    BII_inv_1489880.pdf
    0039292647.pdf
    
    F:\test>
    

    延伸阅读

    • Windows CMD 命令行的 AZ 索引 | SS64.com
    • Windows CMD 命令(分类) - Windows CMD - SS64.com
    • Dir - 列出文件和文件夹 - Windows CMD - SS64.com
    • EnableDelayedExpansion - Windows CMD - SS64.com
    • For /f - 循环通过命令输出 - Windows CMD - SS64.com
    • 变量子字符串 - Windows CMD - SS64.com
    • 3
  5. Scott - Слава Україні
    2020-04-19T06:50:54+08:002020-04-19T06:50:54+08:00

    这是一个带有变体的命令提示符(批处理)解决方案。(我将问题解释为通过(包括)每个文件名的 最后一个 序列删除所有内容,并将文件重命名为该文件。)%nn.pdf

    @echo off
    setlocal enabledelayedexpansion
    for %%F in (*.pdf) do (
        set file=%%F
        call :process
    )
    goto :eof
    
    :process
        set temp=%file%
        :find_all
        REM Remove everything through first %.
        set right=!temp:*%%=!
        REM If there's no change, there were no %'s.
        if not "%right%" == "%temp%" (
            REM Remove the two characters after the %.
            set temp=%right:~2%
            goto :find_all
        )
        if "%file%" == "%right%" (
            REM Filename didn't contain %.
            echo File %file% is unchanged.
        ) else if "%right:~4%" == "" (
            REM Name was abcd%5C.pdf (or abcd%.pdf), so there's nothing left.
            echo Nothing left after truncating %file%
        ) else if exist "%right%" (
            REM The "ren" command will give an error message for this,
            REM but it doesn't say the filename.
            echo Cannot rename %file% because %right% already exists.
        ) else (
            echo ren "%file%" "%right%"
        )
        goto :eof
    

    总体方法是获取每个文件名并通过(包括)第一个序列删除所有内容,然后重复该过程直到没有剩余。我们有几个技巧合二为一。%nn

    • 我们使用语法来编辑文件名。%var:str1=str2%
    • 我们使用表单,通过(包括)第一次出现的 .%var:*str1=str2%str1
    • 我们使用,不是因为我们本身setlocal enabledelayedexpansion需要延迟扩展, 而是我们可以使用 进行替换 ,所以我们可以使用in 。!var:str1=str2!%str1
    • 像往常一样,当我们想%在批处理文件中使用时,我们必须输入 %%.

    因此,该语句set right=!temp:*%%=!设置right 为tempfirst 之后(右侧)的 部分%。这将是一个可能以3Aor开头的字符串5C (因为这些是出现%在示例文件名之后的十六进制字符)。然后我们去掉这两个字符并将结果分配回 temp.

    运行上述程序并检查输出。如果看起来没问题,请更改echo ren为,ren以便它实际重命名文件。


    如果要求是通过 last%5C 而不是 last删除所有内容,请使用以下命令:%nn

    @echo off
    setlocal enabledelayedexpansion
    for %%F in (*.pdf) do (
        set file=%%F
        call :process
    )
    goto :eof
    
    :process
        set temp=%file%
        :find_all
        REM Remove everything through first %5C.
        set right=!temp:*%%5C=!
        REM If there's no change, there were no %5C's.
        if not "%right%" == "%temp%" (
            set temp=%right%
            goto :find_all
        )
        if "%file%" == "%right%" (
            REM Filename didn't contain %5C.
            echo File %file% is unchanged.
        ) else if "%right:~4%" == "" (
            REM Filename was abcdef%5C.pdf, so there's nothing left.
            echo Nothing left after truncating %file%
        ) else if exist "%right%" (
            REM The "ren" command will give an error message for this,
            REM but it doesn't say the filename.
            echo Cannot rename %file% because %right% already exists.
        ) else (
            echo ren "%file%" "%right%"
        )
        goto :eof
    

    其他注意事项:

    • 这些已在 Windows 7 上进行了测试。
    • 原则上,这些可以通过一个简单的for循环来完成,迭代文件。但是 for 当我使用时循环中断了goto,所以我不得不将辅助循环移动到子程序中。
    • 我通常会将信息作为参数传递到子程序中。但是当文件名包含 %.
    • 这些例程会进行适量的错误检查。
    • 这些处理任意数量的(或 )序列。%nn%5C
    • 它们处理任意数量的空格,包括在最后一个序列之后(即,在新文件名中)。%nn
    • 2

相关问题

  • VMware Workstation USB 仲裁服务无法自动启动

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 资源管理器侧面板中的桌面外壳快捷方式

  • 为什么我不能将文件从 Android 发送到 Windows 10?

  • 在多个文件上打开方式?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve