简体   繁体   中英

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

I am trying to rename a group of files in a directory that all have a part in the filename I want removed later on. The example of names are:

  1. 123876. 111 thepartIwanttoremove.exe
  2. thisisatestfile. 111 thepartIwanttoremove.exe
  3. this?392!. 111 thepartIwanttoremove.exe

  4. thankyouall. 222 thepartIwanttoremove.exe

  5. test. 222 thepartIwanttoremove.exe
  6. whatis@d354. 222 thepartIwanttoremove.exe

My code is:

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)

So for an example I want the file:

123876.111thepartIwanttoremove.exe

To look like this:

123876.doc

what function can I use to remove the .*thepartIwanttoremove.exe part of the name afterwards without writing so many lines with "?"?

Thank you very much for your help.

Assuming all the files have three parts to the file name that are separated by periods, this style of code should work for you.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM