-1

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.111thepartIwanttoremove.exe
  2. thisisatestfile.111thepartIwanttoremove.exe
  3. this?392!.111thepartIwanttoremove.exe

  4. thankyouall.222thepartIwanttoremove.exe

  5. test.222thepartIwanttoremove.exe
  6. whatis@d354.222thepartIwanttoremove.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.

MathiusBacharus
  • 15
  • 1
  • 1
  • 4
  • Variable expansion and substitution is the function you can use. You may wish to do this using a loop, and depending upon the loop type possibly utilise delayed expansion. – Compo Jun 05 '18 at 10:03
  • Possible duplicate of [How to replace substrings in windows batch file](https://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file) – zett42 Jun 05 '18 at 11:42
  • Please post realistic data. It's not clear whether some specific string needs to removed from the name or the name be truncated after the first 3 characters of the extension or that it be truncated after the first numeric-string in the extension. In fact, your examples are of `.exe` files but your code is oriented towards `.jpg` files. – Magoo Jun 05 '18 at 12:32
  • This **IS** the realistic data. Files with the extension with three number ones (123876.111thepartIwanttoremove.exe) must be renamed to `.doc` files and files with three number twos (thankyouall.222thepartIwanttoremove.exe) must be renamed to `.jpg` files BUT without the middle part. 123876.111thepartIwanttoremove.exe ---> 123876.doc thankyouall.222thepartIwanttoremove.exe ---> thankyouall.jpg – MathiusBacharus Jun 05 '18 at 12:48
  • Definitely do not need FORFILES. The example in your initial question made it look like you needed to keep the first three numbers after the first period. But now your example in your comment is dropping everything after the first period. If that is the case you can leverage the power of the command modifiers in the FOR command. – Squashman Jun 05 '18 at 12:58
  • I really lost myself trying to find the answer from [link]( ss64.com) to MS Windows Official Commands PDF and all over google. I am just going to add a lot of " ? " since it is working but just takes a lot of time. – MathiusBacharus Jun 05 '18 at 13:21

1 Answers1

0

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"
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • This code is working great in the same directory I run the batch file from. What about the other directories. Is it possible to run this command in say "C:\" to look through all the directories? – MathiusBacharus Jun 06 '18 at 11:15
  • @MathiusBacharus, it can be done but it completely changes the code because if a single directory name has a period in it, the results will be incorrect. I will have to rewrite it completely with nested `FOR` commands. – Squashman Jun 06 '18 at 14:18
  • When you want to search subdirectories as well, you can replace `dir /a-d` by `dir /S /a-d` (adding `-S` causes subdirectory search). – Dominique May 22 '21 at 11:04
  • @Dominique did you read my comment about what happens if a directory name has a period. – Squashman May 22 '21 at 15:35
  • @Squashman: I don't get it: I've just created a directory "test.test.test" and in there I've made a file, called "file.txt". From the main directory I launched `dir /S file.txt` and it got found, so I don't understand your issue. – Dominique May 22 '21 at 15:40
  • @Dominique there is no issue with finding the file. The issue comes into play when implementing the logic to rename the files based on the users technical specifications. – Squashman May 23 '21 at 19:44