Rename files in multiple folders with incmnt

June 18, 2010 at 02:40:04
Specs: Windows XP
Hello, I am hoping to find a script that will allow the following to be completed:

Look in multiple subfolders at the files contained within, query the filename to see if it meets a certain naming convention eg filename%.ext, if a file does not meet the convention then rename it with an incremented number that is 1 value higher than the file with the highest number in that folder... Eg:

Root folder: C:\test
Look in all subfolders: C:\test\sub1, C:\test\sub2, C:\test\sub3 etc
Query existing files: So if 'filename1.ext', 'filename2.ext' exist leave as they are, but if 'filenameotherdif.ext' exists then rename it to 'filename3.ext' (the 3 value being 1 greater than the last filename in the folder with the correct convention.

I hope that makes sense? Thanks


See More: Rename files in multiple folders with incmnt

Report •


#1
June 18, 2010 at 08:31:49
Should the renamed files keep their original basename -
orig#.ext - or should they be named filename#.ext literally?

Report •

#2
June 18, 2010 at 09:05:35
They need to be renamed literally.

It will be a number of folders that will contain files named eg.
filename1.ext, filename2.ext etc. Each month an app will export
a new file to each folder with a random filename.

These new files then need to be renamed to match the
convention of the other files, incrementing the number by 1..
Thanks.


Report •

#3
June 18, 2010 at 10:08:30
Is the Extension always the same "ext"? If so we can search on that. We need a pattern that is always constant to help you.

Report •

Related Solutions

#4
June 18, 2010 at 10:53:24
Yes the extension will ways be the same.

Report •

#5
June 18, 2010 at 12:12:38
OK, thanks. Try the following:

@echo off

setlocal  EnableDelayedExpansion

for /f "delims=" %%a in ('dir /s /b /a:-d c:\test') do (
  echo %%~na| findstr /r /c:"[^0-9]$"> nul
  if !errorlevel! neq 1 (
    call :GetNewFileName "%%a" newname
    ren "%%a" "!newname!"
  )
)
goto :eof

:GetNewFileName (orig_byval, new_byref)
set n=0
:loop
set /a n+=1
if exist "%~p1filename%n%%~x1" goto loop
set %2=filename%n%%~x1


Report •

#6
June 18, 2010 at 12:53:37
<Deleted>

Report •

#7
June 21, 2010 at 01:07:12
MarkLS, this works like a charm, Thank You!

Could I ask for another bit of guidance, it's probably very simple...

Before running the srcipt you have given me above, I have a script that will create folder names based on the filenames and then move the files into that folder. The script is as follows:

@echo off
for /f "delims=" %%a in ('dir /b /a-d') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
if exist "%%a" move "%%~na.*" "%%~na"
)
)

What I need to do now, is have this script ignore one filename in the root folder that I am running it from?

Thanks


Report •

#8
June 21, 2010 at 02:31:10
I don't see anything wrong with the code in response 7 so
it's probably the code that I posted that's interfering with
things. Here's an updated version which should fix it.

@echo off

setlocal  EnableDelayedExpansion

set RootDir=c:\test\
for /f "delims=" %%a in ('dir /s /b /a:-d "%RootDir%"') do (
  set IsBat=false
  if /i "%%~dpa" equ "%RootDir%" if /i "%%~xa" equ ".bat" (
    set IsBat=true
  )
  if "!IsBat!" neq "true" (
    echo %%~na| findstr /r /c:"[^0-9]$"> nul
    if !errorlevel! neq 1 (
      call :GetNewFileName "%%a" newname
      ren "%%a" "!newname!"
    )
  )
)
goto :eof

:GetNewFileName (orig_byval, new_byref)
set n=0
:loop
set /a n+=1
if exist "%~dp1filename%n%%~x1" goto loop
set %2=filename%n%%~x1


Report •

#9
June 21, 2010 at 03:38:53
Sorry I think I may have confused things, I have the 2 scripts saved as:

c:\test\createmove.bat (my script for creating / moving folders / files as in response 7)
c:\test\rename.bat (your script for renaming files)

I want to run createmove.bat first and then run rename.bat.

The only issue I have is that when I run createmove.bat it creates a new sub folder (rename) and drops the rename.bat file into it.

So.. I need to add a line to createmove.bat that will ignore rename.bat or indeed all files of .bat extension..

Thank you for helping a newb!


Report •

#10
June 21, 2010 at 04:56:22
Is it OK to exclude all files with .bat extension from processing
in root directory? (BTW, I wouldn't recommend using 'rename'
for the filename as it's already taken by the system.)

Report •

#11
June 21, 2010 at 05:02:04
Excluding all files with .bat extension in the root directory would be fine..

Noted, at the moment this is a test environment, once in production the filenames will be more specific..

Thanks again.


Report •

#12
June 21, 2010 at 08:49:02
OK, you need to use the code which I have just re-edited
in response #8 for the renaming script and you need to
change the third line of the code in response #7 to:
if /i "%%~xa" neq ".bat" (
which should take care of the move script, hopefully.

Report •

#13
June 22, 2010 at 00:30:57
Perfect.. Thank you very much!

Report •

#14
June 22, 2010 at 07:45:09
OK could I be really cheeky, the requirements have changed slightly.

After renaming the files need to have the convention YYYY Q# eg 2010 Q1.ext

I've amended the naming convention in the script as follows:

:GetNewFileName (orig_byval, new_byref)
set n=0
:loop
set /a n+=1
if exist "%~dp12010 Q%n%%~x1" goto loop
set %2=2010 Q%n%%~x1

However, as this new convention relates to the year/quarter number I need the Q number never to exceed 4, and when the 2010 Q4.ext file exists, the next file created needs to be 2011 Q1.ext etc

Thanks.


Report •

#15
June 22, 2010 at 12:15:27
The following should work but it would probably be better
coded by extracting the current year from the %date%
variable of the system that it's running on depending on how
it's formatted.

:GetNewFileName (orig_byval, new_byref)
set year=2010
set %2=
:loop
for /l %%n in (1, 1, 4) do (
  if not defined %2 (
    if not exist "%~dp1!year! Q%%n%~x1" set %2=!year! Q%%n%~x1
  )
)
if not defined %2 set /a year+=1&goto loop


Report •

#16
June 23, 2010 at 00:47:14
Once again. Thank you..

Report •


Ask Question