Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename or remove prefix for multiple files to each ones' number in Windows

I am trying to change all the files names in a current folder and I am trying to achieve this either by removing the files prefix (every file has a common prefix) or changing their names to their count (if there are 5 files, the filenames will be 1.txt 2.txt 3.txt 4.txt 5.txt).

Now I have found the ren command in cmd and played with it a little but so far I was not able to achieve the result and I can only run this in cmd, so no batch files can be used.

This is the closest that I got, but it only adds a prefix:

FOR %f IN (*.*) DO ren %f prefix%f

I have tried doing the opposite:

FOR %f IN (*.*) DO ren prefix%f %f

But of course, didn't work so I am now asking for help and some explanation if possible (I like to understand how these things work). Thanks in advance for any help.

like image 279
Shadow Avatar asked Apr 18 '12 03:04

Shadow


People also ask

How do I rename multiple files with sequential numbers in Windows?

Select multiple files in a folder. To do so, press and hold down the CTRL key while you are clicking files. After you select the files, press F2. Type the new name, and then press ENTER.

How do I rename multiple files numerically?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

How do you rename multiple files in numerical order without brackets?

In the File Explorer window, select all files, right-click and select rename. Windows will select the starting number as the number supplied between the round brackets so name the file using a number that is 1 digit more than the number of digits required.


2 Answers

A much simpler solution is provided in
https://superuser.com/a/871799/497147

I copied it here for easier access.

Forget about complicated scripts for this.

rename is a very old and never properly completed command. If you do not use it properly, the result might surprise you.

For example to remove a prefix abcd from abcd1.txt, abcd2.txt, abcd3.txt etc. in order to get 1.txt, 2.txt, 3.txt simply use

rename "abcd*.txt" "////*.txt"

You need the same number of / as the number of initial characters you would like to remove.

Do place double quotes for both arguments.

like image 167
Alnair Avatar answered Oct 18 '22 20:10

Alnair


I don't understand why you can't use a batch file. But here is a solution that should work with most file names.

Critical - first you must make sure you have an undefined variable name, I'll use fname

set "fname="

Next is the command to actually do the renaming. It won't work properly if fname is already defined.

for %a in (prefix*.txt) do @(set "fname=%a" & call ren "%fname%" "%fname:*prefix=%")

The fname variable is defined for each iteration and then the syntax %fname:*prefix=% replaces the first occurrence of "prefix" with nothing. The tricky thing is Windows first attempts to expand %fname% when the command is first parsed. Of course that won't work because it hasn't been defined yet. On the command line the percents are preserved if the variable is not found. The CALL causes an extra expansion phase that occurs after the variable has been set, so the expansion works.

If fname is defined prior to running the command, then it will simply try to rename that same file for each iteration instead of the value that is being assigned within the loop.

If you want to run the command again with a different prefix, you will have to first clear the definition again.

EDIT - Here is a batch file named "RemovePrefix.bat" that does the job

::RemovePrefix.bat  prefix  fileMask
@echo off
setlocal
for %%A in ("%~1%~2") do (
  set "fname=%%~A"
  call ren "%%fname%%" "%%fname:*%~1=%%"
)

Suppose you had files named like "prefixName.txt", then you would use the script by executing

RemovePrefix  "prefix"  "*.txt"

The batch file will rename files in your current directory. The batch file will also have to be in your current directory unless the batch file exists in a directory that is in your PATH variable. Or you can specify the full path to the batch file when you call it.

The rules for expansion are different in a batch file. FOR variables must be referenced as %%A instead of %A, and %%fname%% is not expanded initially, instead the double percents are converted into single percents and then %fname% is expanded after the CALL. It doesn't matter if fname is already defined with the batch file. The SETLOCAL makes the definition of fname temporary (local) to the batch file.

like image 27
dbenham Avatar answered Oct 18 '22 19:10

dbenham