Solved

Rename File If Exists in SubDirectory [DOS Batch File]

Posted on 2013-01-29
15
2,516 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
Comment
Question by:Dare626
  • 8
  • 7
15 Comments
 
LVL 51

Expert Comment

by:Bill Prew
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

Open in new window

~bp
0
 

Author Comment

by:Dare626
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

by:Dare626
ID: 38833027
Here's the attachment.
Question-Illustrated.pdf
0
 
LVL 51

Expert Comment

by:Bill Prew
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

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

Last question what do these do '~nxA'
0
 
LVL 51

Expert Comment

by:Bill Prew
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
      )
    )
  )
)

Open in new window

~bp
0
 

Author Comment

by:Dare626
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 Results
@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
      )
    )
  )
)

Open in new window

all i did was update the BaseDir/DestDir. Thanks for the Remarks/Comments as you step through your code also.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 51

Expert Comment

by:Bill Prew
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"

Open in new window

0
 

Author Comment

by:Dare626
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 51

Expert Comment

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

~bp
0
 

Author Comment

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

Accepted Solution

by:
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

Open in new window

~bp
0
 

Author Comment

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

Author Closing Comment

by:Dare626
ID: 38836853
Bill,

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

Expert Comment

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

~bp
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now