Solved

Rename File If Exists in SubDirectory [DOS Batch File]

Posted on 2013-01-29
2,569 Views
1 Endorsement
Last Modified: 2013-01-30
Looking for a batch file that when i execute every Night would to transfer (MOVE Command) all files in Adam’s Folder to his Sub-Directory named Archive, if a file name already exists in the Archive id like to append a sequence number to the file that is in the archive folder for example (fileaaa_1.pdf) because there’s a file in Adam’s folder named fileaaa.pdf.Then move the fileaaa.pdf file into the archive folder.Your help would be highly appreciated. I've also illustrated my problem for clarifications.
Visio-Drawing3.pdf
1
Question by:Dare626
• 8
• 7
15 Comments

LVL 53

Expert Comment

ID: 38832723
This should do what you described.  Save as a BAT file and adjust the SET lines near the top for your folders.

@echo off
setlocal EnableDelayedExpansion

REM Define from and to folders for file moves
set BaseDir=C:\EE\EE28012821\Base
set DestDir=C:\EE\EE28012821\Base\Archive

REM Process all files in from folder
for %%A in ("%BaseDir%\*.*") do (
REM See if it exists in dest folder already
if exist "%DestDir%\%%~nxA" (
REM If so, then get the next available seq number to appenmd to the file name
call :NextSeq "%%~nxA"
REM Move and rename the file with the next seq number
move "%%~A" "%DestDir%\%%~nA_!Seq!%%~xA"
) else (
REM Doesn't exist yet, so just move with the same name
move "%%~A" "%DestDir%"
)
)

REM Small subroutine to find the next available seq number for a file name
:NextSeq
set Seq=0
REM Loop until we get to the seq number that doesn't exist
:NextSeqLoop
set /A Seq+=1
if exist "%DestDir%\%~n1_!Seq!%~x1" goto :NextSeqLoop
exit /b

~bp
0

Author Comment

ID: 38833025
Hi Bill,

First of all thanks for your time in writing that code for me i appreciate it and it works exactly how i wanted...i just have a last question which i believe is better asked visually than in words, I'd like your opinion on it...I've illustrated it on the attached PDF.

Again Thanks Bill.
0

Author Comment

ID: 38833027
Here's the attachment.
Question-Illustrated.pdf
0

LVL 53

Expert Comment

ID: 38833074
So, do you want to process all "users" under just the Field folder, or both the Field and In-House folders?  Are there any others?

(this should be fairly easily to do once I make sure I know exactly what you want)

~bp
0

Author Comment

ID: 38833216
Hi Bill...
Yes process all folders within ROOT please. I've attached a picture of this.
Thanks Bill....

Last question what do these do '~nxA'
0

LVL 53

Expert Comment

ID: 38833566
Okay, this will process the entire folder structure.  It assumes all the Archive folders already exist, but if that isn't right we can check for them, and/or create them automatically.

You can get some info on the "modifiers" to the FOR loop variables by doing this at a command line:

FOR /?

of this page:

http://www.robvanderwoude.com/ntfor.php

@echo off
setlocal EnableDelayedExpansion

REM Define from and to folders for file moves
set BaseDir=C:\Root
set DestDir=Archive

REM Process first level folders
for /D %%A in ("%BaseDir%\*.*") do (
echo "%%~A"
REM Process second level folders
for /D %%B in ("%%~A\*.*") do (
echo "%%~B"
REM Process all files in folder
for %%C in ("%%~B\*.*") do (
REM See if it exists in dest folder already
if exist "%%~B\%DestDir%\%%~nxC" (
REM If so, then get the next available seq number to appenmd to the file name
call :NextSeq "%%~B\%DestDir%\%%~nxC"
REM Move and rename the file with the next seq number
echo "%%~C" -^> "%%~B\%DestDir%\%%~nC_!Seq!%%~xC"
move "%%~C" "%%~B\%DestDir%\%%~nC_!Seq!%%~xC" >NUL
) else (
REM Doesn't exist yet, so just move with the same name
echo "%%~C" -^> "%%~B\%DestDir%\"
move "%%~C" "%%~B\%DestDir%\" >NUL
)
)
)
)

~bp
0

Author Comment

ID: 38833616
Thank you Bill...i appreciate it ...i ran it and works just fine just one small thing the sequence number is not being populated instead it adds the _(underscore) at the end of the file name Example
@echo off
setlocal EnableDelayedExpansion

REM Define from and to folders for file moves
set BaseDir=Q:\
set DestDir=Archive

REM Process first level folders
for /D %%A in ("%BaseDir%\*.*") do (
echo "%%~A"
REM Process second level folders
for /D %%B in ("%%~A\*.*") do (
echo "%%~B"
REM Process all files in folder
for %%C in ("%%~B\*.*") do (
REM See if it exists in dest folder already
if exist "%%~B\%DestDir%\%%~nxC" (
REM If so, then get the next available seq number to appenmd to the file name
call :NextSeq "%%~B\%DestDir%\%%~nxC"
REM Move and rename the file with the next seq number
echo "%%~C" -^> "%%~B\%DestDir%\%%~nC_!Seq!%%~xC"
move "%%~C" "%%~B\%DestDir%\%%~nC_!Seq!%%~xC" >NUL
) else (
REM Doesn't exist yet, so just move with the same name
echo "%%~C" -^> "%%~B\%DestDir%\"
move "%%~C" "%%~B\%DestDir%\" >NUL
)
)
)
)

all i did was update the BaseDir/DestDir. Thanks for the Remarks/Comments as you step through your code also.
0

LVL 53

Expert Comment

ID: 38833629
Hmmm, odd, I ran it here before posting and got the following results, seemed to name the files correctly so I think that logic is working.

Actually, it may be because your base dir ends with a backslash while the script assumes it doesn't and adds one after it.  Try removing that and see if that helps.

~bp

C:\ee\EE28012821>EE28012821.bat
"C:\EE\EE28012821\Base\d1"
"C:\EE\EE28012821\Base\d1\u1"
"C:\EE\EE28012821\Base\d1\u1\f11a.txt" -> "C:\EE\EE28012821\Base\d1\u1\Archive\"
"C:\EE\EE28012821\Base\d1\u1\f11b.txt" -> "C:\EE\EE28012821\Base\d1\u1\Archive\"
"C:\EE\EE28012821\Base\d1\u2"
"C:\EE\EE28012821\Base\d1\u2\f12a.txt" -> "C:\EE\EE28012821\Base\d1\u2\Archive\"
"C:\EE\EE28012821\Base\d1\u2\f12b.txt" -> "C:\EE\EE28012821\Base\d1\u2\Archive\"
"C:\EE\EE28012821\Base\d2"
"C:\EE\EE28012821\Base\d2\u3"
"C:\EE\EE28012821\Base\d2\u3\f23a.txt" -> "C:\EE\EE28012821\Base\d2\u3\Archive\"
"C:\EE\EE28012821\Base\d2\u3\f23b.txt" -> "C:\EE\EE28012821\Base\d2\u3\Archive\"
"C:\EE\EE28012821\Base\d2\u4"
"C:\EE\EE28012821\Base\d2\u4\f24a.txt" -> "C:\EE\EE28012821\Base\d2\u4\Archive\"
"C:\EE\EE28012821\Base\d2\u4\f24b.txt" -> "C:\EE\EE28012821\Base\d2\u4\Archive\"

C:\ee\EE28012821>EE28012821.bat
"C:\EE\EE28012821\Base\d1"
"C:\EE\EE28012821\Base\d1\u1"
"C:\EE\EE28012821\Base\d1\u1\f11a.txt" -> "C:\EE\EE28012821\Base\d1\u1\Archive\f11a_1.txt"
"C:\EE\EE28012821\Base\d1\u1\f11b.txt" -> "C:\EE\EE28012821\Base\d1\u1\Archive\f11b_1.txt"
"C:\EE\EE28012821\Base\d1\u2"
"C:\EE\EE28012821\Base\d1\u2\f12a.txt" -> "C:\EE\EE28012821\Base\d1\u2\Archive\f12a_1.txt"
"C:\EE\EE28012821\Base\d1\u2\f12b.txt" -> "C:\EE\EE28012821\Base\d1\u2\Archive\f12b_1.txt"
"C:\EE\EE28012821\Base\d2"
"C:\EE\EE28012821\Base\d2\u3"
"C:\EE\EE28012821\Base\d2\u3\f23a.txt" -> "C:\EE\EE28012821\Base\d2\u3\Archive\f23a_1.txt"
"C:\EE\EE28012821\Base\d2\u3\f23b.txt" -> "C:\EE\EE28012821\Base\d2\u3\Archive\f23b_1.txt"
"C:\EE\EE28012821\Base\d2\u4"
"C:\EE\EE28012821\Base\d2\u4\f24a.txt" -> "C:\EE\EE28012821\Base\d2\u4\Archive\f24a_1.txt"
"C:\EE\EE28012821\Base\d2\u4\f24b.txt" -> "C:\EE\EE28012821\Base\d2\u4\Archive\f24b_1.txt"

C:\ee\EE28012821>EE28012821.bat
"C:\EE\EE28012821\Base\d1"
"C:\EE\EE28012821\Base\d1\u1"
"C:\EE\EE28012821\Base\d1\u1\f11a.txt" -> "C:\EE\EE28012821\Base\d1\u1\Archive\f11a_2.txt"
"C:\EE\EE28012821\Base\d1\u1\f11b.txt" -> "C:\EE\EE28012821\Base\d1\u1\Archive\f11b_2.txt"
"C:\EE\EE28012821\Base\d1\u2"
"C:\EE\EE28012821\Base\d1\u2\f12a.txt" -> "C:\EE\EE28012821\Base\d1\u2\Archive\f12a_2.txt"
"C:\EE\EE28012821\Base\d1\u2\f12b.txt" -> "C:\EE\EE28012821\Base\d1\u2\Archive\f12b_2.txt"
"C:\EE\EE28012821\Base\d2"
"C:\EE\EE28012821\Base\d2\u3"
"C:\EE\EE28012821\Base\d2\u3\f23a.txt" -> "C:\EE\EE28012821\Base\d2\u3\Archive\f23a_2.txt"
"C:\EE\EE28012821\Base\d2\u3\f23b.txt" -> "C:\EE\EE28012821\Base\d2\u3\Archive\f23b_2.txt"
"C:\EE\EE28012821\Base\d2\u4"
"C:\EE\EE28012821\Base\d2\u4\f24a.txt" -> "C:\EE\EE28012821\Base\d2\u4\Archive\f24a_2.txt"
"C:\EE\EE28012821\Base\d2\u4\f24b.txt" -> "C:\EE\EE28012821\Base\d2\u4\Archive\f24b_2.txt"

0

Author Comment

ID: 38833686
i removed the backslash from the BaseDir and it did not append the number, the logic is working right because it traverses all folders and moves them appropriately but the sequence number is being 'cut-short' sort of speak...the original first code you posted did add the sequence number when it transfered it over i tested that one, perhaps one of the for loops is causing some problem? By the way all Folders contain a 'Archive' so there's no reason to check if it exists. I'm baffled as you are...but a little more because i'm trying to understand the code.
0

LVL 53

Expert Comment

ID: 38833708
When it ran, what was displayed to the console?  Can you post that here?

~bp
0

Author Comment

ID: 38833712
i entered pause at the end of the script but it ignores it...and window closes fast
0

LVL 53

Accepted Solution

Bill Prew earned 500 total points
ID: 38833721
Sorry, I see it now, I accidentally didn't copy up the new full script, try this.

@echo off
setlocal EnableDelayedExpansion

REM Define from and to folders for file moves
set BaseDir=Q:
set DestDir=Archive

REM Process first level folders
for /D %%A in ("%BaseDir%\*.*") do (
echo "%%~A"
REM Process second level folders
for /D %%B in ("%%~A\*.*") do (
echo "%%~B"
REM Process all files in folder
for %%C in ("%%~B\*.*") do (
REM See if it exists in dest folder already
if exist "%%~B\%DestDir%\%%~nxC" (
REM If so, then get the next available seq number to appenmd to the file name
call :NextSeq "%%~B\%DestDir%\%%~nxC"
REM Move and rename the file with the next seq number
echo "%%~C" -^> "%%~B\%DestDir%\%%~nC_!Seq!%%~xC"
move "%%~C" "%%~B\%DestDir%\%%~nC_!Seq!%%~xC" >NUL
) else (
REM Doesn't exist yet, so just move with the same name
echo "%%~C" -^> "%%~B\%DestDir%\"
move "%%~C" "%%~B\%DestDir%\" >NUL
)
)
)
)

REM Small subroutine to find the next available seq number for a file name
:NextSeq
set Seq=0
REM Loop until we get to the seq number that doesn't exist
:NextSeqLoop
set /A Seq+=1
if exist "%~dp1%~n1_!Seq!%~x1" goto :NextSeqLoop
exit /b

~bp
0

Author Comment

ID: 38833749
Thanks bill ill try it first thing tomorrow I appreciate it
0

Author Closing Comment

ID: 38836853
Bill,

Worked perfectly, thanks for your time and intellect. Saved me alot of manual work.
0

LVL 53

Expert Comment

ID: 38836873
Welcome, glad that helped, thanks for the feedback.

~bp
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you unable to synchronize your OST (Offline Storage Table) file with Microsoft Exchange Server? Is your OST file exceeding 2 GB size limit? In Microsoft Outlook 2002 and earlier versions, there is a 2 GB size limit for the OST file. If the file …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.