Batch file to detect only part of the file name

I need a batch file fragment that can detect part of the file name, and then rename it. Note that the numbers after the file name change randomly, but the โ€œfileโ€ is always the same.

Example:

filename: file143424

There will always be only one file in a folder that needs to be renamed. The script will delete older versions of the file. For example, I put there the first file inside the folder and script. Then I run a script that renames it to just a file. Then, if I placed a new file, the script, when I run it again, will recognize it from the "file" prefix and rename it to the file after deleting the old one.

0
source share
3 answers

Sorry. I think your question is incomplete. Do you want to rename "file134342" to what?

In your comment, you said: rename it only to โ€œfileโ€, but this only works if there is only one file with this name. Anyway, here it is:

for %%f in (file*) do ren %%f file 

If this is not what you want, then give us more detailed information (you always show details from the very beginning).

+3
source

You can check the first four characters in this way:

 if "%filename:~0,4%"=="file" ... 
+1
source

The @Aacini suggestion should work fine in your case. But you can do just fine without a for loop, just one REN ( RENAME ) command is enough:

 RENAME file* file 
+1
source

Source: https://habr.com/ru/post/1437982/


All Articles