Saturday, November 12, 2011

Windows: remove initial characters from file names

In my continuation of last post, if you do not have a specific pattern to delete, but you know the number of characters to delete, you can use following script. Following script will remove initial 5 characters from the file name. (you can change 5 to any number, ofcourse)

@ECHO ON
FOR %%F IN ("*.mp3") DO CALL :process "%%F"
GOTO :EOF

:process
SET oldname=%1
SET "newname=%~nx1"
SET "newname=%newname:~5%"
RENAME %oldname% "%newname%"

 

Windows: remove prefixes from file names

Whenever I download any film songs from internet, the file name have a specific prefix (basically the website name, from where I downloaded the songs). To keep the file names shorter, I wish to remove the prefixes from all files. Doing this task manually consumes so much time, so here is the script to do it at one go.

File name is in format "[Site.name] Film Name - Song Number - Song Title.mp3". Save the following contents in a file (let say rename.bat) and run this file in windows command prompt. It will change all file names to "Film Name - Song Number - Song Title.mp3"

#################################
::rename.bat
#################################
@ECHO OFF
FOR %%F IN ("[*] *") DO CALL :renamer "%%F"
GOTO :EOF

:renamer
SET oldname=%1
SET "newname=%~nx1"
SET "newname=%newname:*] =%"
RENAME %oldname% "%newname%"