Increment Prefix on files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
scottynz
Posts: 4
Joined: 30 May 2016 17:17

Increment Prefix on files

#1 Post by scottynz » 30 May 2016 17:47

I need to periodically rename some files with a prefix number that increments. Example:
1000_FileA.mp3
1001_FileB.mp3
1002_FileC.mp3

When then files are moved and the next lot comes into the directory. I need it to pick up where it left off...
1003_FileA.mp3
1004_FileB.mp3 .... and so on.

It would be great if the batch file could reference a file for this prefix.. Like '1000.PRE', Which shows what the current prefix is and can be edited manually if necessary..
I know how to do that part (See below) but not the file renaming based on this number. Any help appreciated!

for /f "tokens=* delims= " %%a in ('dir /b C:\TEST\*.PRE') do (
set "STR=%%a"
)
SET PREFIX=%STR:~0,4%
SET COUNTER=%PREFIX%
SET /A "NEWPREFIX=COUNTER+=1
REN %PREFIX%.PRE %NEWPREFIX%.PRE

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Increment Prefix on files

#2 Post by elzooilogico » 31 May 2016 04:24

If I understand you, something like this may help :wink:

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

if not exist "index.pre" >"index.pre" echo/1000

set/p lastIndex=<index.pre

for /f "tokens=*" %%f in ('dir /b /a-d-h *.*') do (
  set/a lastIndex+=1
  ren %%f !lastIndex!_%%f
  move !lastIndex!_%%f %whatever_dir_you_want%
)

>"index.pre" echo/%lastIndex%

Endlocal
exit/B 0

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Increment Prefix on files

#3 Post by douglas.swehla » 01 Jun 2016 11:25

I agree with elzooilogico's approach of using the content of the .pre file, rather than the name. A few comments:

You'll probably want to quote the filename variables, just to be safe.

You can use DIR /O to control the order in which files are processed (alphabetically, by age, etc.).

As written, the batch assumes that it is in the same directory as the target files. Since it's listing all files (dir *.*), this means that both the batch file itself and the index.pre files will be renamed and moved, after which processing will fail, and the index.pre file will never be updated. To address this, you will want to either
  • keep the batch elsewhere and specify the target directory (dir "...\target\*.*"),
  • specify the file types you're looking for (dir *.mp3),
  • filter out types you're not looking for (dir *.* ^| findstr /e /l /i /v .bat ^| findstr /e /l /i /v .pre), or
  • add conditional processing to exempt the batch and .pre files from being renamed (if not "%%f" equ "%~nx0" if not "%%f" equ "index.pre" rename & move)
.

scottynz
Posts: 4
Joined: 30 May 2016 17:17

Re: Increment Prefix on files

#4 Post by scottynz » 01 Jun 2016 13:12

:D You nailed it!
Thank you so much elzooilogico.

scottynz
Posts: 4
Joined: 30 May 2016 17:17

Re: Increment Prefix on files

#5 Post by scottynz » 02 Jun 2016 16:58

Thanks and good thoughts on that douglas.swehla
Any thought on how i could apply this to files in sub directories as well?

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Increment Prefix on files

#6 Post by douglas.swehla » 02 Jun 2016 19:16

scottynz wrote:Thanks and good thoughts on that douglas.swehla
Any thought on how i could apply this to files in sub directories as well?


You're welcome, Scotty.

Yes, this can be adapted to subdirectories. The solution will depend on how deep into the folder hierarchy you want to go.

If you want to process the complete directory tree, just add the /S switch to the DIR command. If there exists a file "C:\path\to\target\sub1\sub2\...\sub87\MySong.mp3", then DIR /S will find it.

If you want to go just one level down, then we'll need to wrap the existing code in an outer FOR loop that first finds all the subdirectories in the target directory:

Code: Select all

for /f "tokens=1 delims=" %%F in ('dir /ad /b "C:\path\to\target\"') do (rem Process files in directory "%%~dpF".)


It's possible to set up a hot folder for this sort of thing, that will automatically process any qualifying file placed in it, removing the need for any human intervention at all. To know whether that's a good fit for you, though we need more information. Please describe the existing process (who, what, when, where, how, why), including any manual steps, and how much control you have over it.

scottynz
Posts: 4
Joined: 30 May 2016 17:17

Re: Increment Prefix on files

#7 Post by scottynz » 02 Jun 2016 19:33

/S didn't seem to do it.

It's for a non-profit radio station.
At the moment i download all the radio shows automatically with some software. Lets say 'ShowA'
Any new files are then filtered and copied to this folder called C:\Rename\ShowA
Where this batch file adds the prefix to all the new files for ShowA.
<where i struggle with this now is that Sometimes the new shows come in a subfolder call 2016\Jan or something like that.>

Once the prefix is added for each file, it is then moved into the C:\Play\ShowA folder where the Radio software will play the file with the Prefix number for the current day. Today's prefix is 1123.

My intention is to have the thing working fully automatically so that we can run it in some more remote locations without human intervention.

Hot folder sounds cool, but not sure what it is.

Post Reply