Windows批处理文件,如何重命名和删除部分文件名

时间:2018-06-05 09:07:50

标签: windows file batch-file rename filenames

我正在尝试重命名目录中的一组文件,这些文件都包含我想要删除的文件名中的一部分。 名称的例子是:

  1. 123876.的 111 thepartIwanttoremove.exe
  2. thisisatestfile。的 111 thepartIwanttoremove.exe
  3. 此?392!。的 111 thepartIwanttoremove.exe

  4. thankyouall。的 222 thepartIwanttoremove.exe

  5. 测试。的 222 thepartIwanttoremove.exe
  6. 头朝下@ D354。的 222 thepartIwanttoremove.exe
  7. 我的代码是:

    forfiles /S /M *.111thepartIwanttoremove.exe /C "cmd /c rename @file @fname.doc"
    ren ???.111thepartIwanttoremove.* ???.doc
    ren ????.111thepartIwanttoremove.* ????.doc
    ren ?????.111thepartIwanttoremove.* ?????.doc (and so on)
    
    forfiles /S /M *.222thepartIwanttoremove.exe /C "cmd /c rename @file @fname.jpg"
    ren ???.222thepartIwanttoremove.* ???.jpg
    ren ????.222thepartIwanttoremove.* ????.jpg
    ren ?????.222thepartIwanttoremove.* ?????.jpg (and so on)
    

    所以我想要一个例子:

    123876.111thepartIwanttoremove.exe
    

    看起来像这样:

    123876.doc
    

    我可以使用什么功能删除名称的。* thepartIwanttoremove.exe部分,而不用#34;?"?"?

    写出这么多行

    非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

假设所有文件都有三个由句点分隔的文件名部分,这种代码样式应该适合你。

for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.111*.exe') do rename "%%G.%%H.%%I" "%%G.doc"
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.222*.exe') do rename "%%G.%%H.%%I" "%%G.jpg"
相关问题