Dare626
asked on
Rename File If Exists in SubDirectory [DOS Batch File]
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
Visio-Drawing3.pdf
ASKER
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.
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.
ASKER
Here's the attachment.
Question-Illustrated.pdf
Question-Illustrated.pdf
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
(this should be fairly easily to do once I make sure I know exactly what you want)
~bp
ASKER
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
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
ASKER
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.
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
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"
ASKER
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.
When it ran, what was displayed to the console? Can you post that here?
~bp
~bp
ASKER
i entered pause at the end of the script but it ignores it...and window closes fast
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks bill ill try it first thing tomorrow I appreciate it
ASKER
Bill,
Worked perfectly, thanks for your time and intellect. Saved me alot of manual work.
Worked perfectly, thanks for your time and intellect. Saved me alot of manual work.
Welcome, glad that helped, thanks for the feedback.
~bp
~bp
Open in new window
~bp