I'm trying to create a batch file to automate a process: i need to check into a folder if files name have "A-" prefix If not, i need to add it. I can name the folder like the prefix if this can help!
If it doesn't have to be a batch file, then you could use ReNamer. You can save your renaming rules as a preset and use it to rename your files/folders via command line or scheduled tasks, automatically.
Once you save your rules in as a preset (e.g.
A-Preset) then you can use it for automatic renaming via command line:
ReNamer.exe /rename "A-Prefix" C:\Test
The rules above simply remove existing
A- prefix and then add it back in. Files which already have
A- prefix will remain unchanged after a renaming operation.
Using a GUI to configure renaming rules and test them may be easier than having to tinker with complex batch files.
This is simple with JREN.BAT - a regular expression renaming utility. JREN is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file is required. Full documentation is embedded within the utility - use
JREN /? from the command prompt to get help. (
jren /?? for paged help)
To conditionally add the
A- prefix to all files within the current folder:
call jren "^" "A-" /fx "A-*"
The first argument matches the beginning of the file name. The second argument "replaces" the match with "A-" (Inserts "A-" at the beginning). The /FX option excludes all files that already begin with "A-"
You can specify a different folder by adding the
/P "yourPath" option.
You can have the prefix match the parent folder name by adding a FOR loop. For example, suppose you have a catalog of mp3 files within "c:\music" Each folder under "\music" represents an album, and each mp3 within an album folder is a song on that album. You could use the following to prefix each song with the album name, only if it is not there already:
@echo off for /d /r "c:\music" %%F in (*) do call jren "^" "%%~nxF-" /p "%%F" /fm *.mp3 /fx "%%~nxF-*"