[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Rename File If Exists in SubDirectory [DOS Batch File]

Posted on 2013-01-29
15
Medium Priority
?
2,749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 58

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 58

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 58

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
 
LVL 58

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 58

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 58

Accepted Solution

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

Expert Comment

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

~bp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

656 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