Solved

Rename File If Exists in SubDirectory [DOS Batch File]

Posted on 2013-01-29
15
2,533 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 52

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 52

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 52

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 52

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 52

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 52

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 52

Expert Comment

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

~bp
0

Featured Post

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)

Question has a verified solution.

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

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

943 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

5 Experts available now in Live!

Get 1:1 Help Now