簡體   English   中英

Windows 批處理文件,如何重命名和刪除部分文件名

[英]Windows Batch File, how to rename and remove part of filename

我正在嘗試重命名目錄中的一組文件,這些文件都包含在我稍后要刪除的文件名中。 名稱示例如下:

  1. 123876. 111 thepartIwanttoremove.exe
  2. 這是測試文件。 第111
  3. 這?392!。 第111

  4. 謝謝你們。 第222

  5. 測試。 第222
  6. whatis@d354。 第222

我的代碼是:

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 部分,而不用寫這么多行“?”?

非常感謝您的幫助。

假設所有文件都包含由句點分隔的文件名的三個部分,這種代碼風格應該適合您。

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM