View Single Post
  #5  
Old 01-10-2008, 05:27 PM
unix unix is offline
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists intarget Directory

On 10 Jan, 17:02, Tom Lavedas wrote:
> On Jan 10, 10:42 am, brettmannin...@gmail.com wrote:
>
>
>
>
>
> > On 10 Jan, 15:19, "billious" wrote:

>
> > > wrote in message

>
> > >news:820472c5-13c4-4253-88f0-616e809a7bbd@i3g2000hsf.googlegroups.com....

>
> > > > Hello,

>
> > > > I would like to create a BATCH file which will move and rename a file
> > > > depending if a file already is present on the target directory. *For
> > > > example

>
> > > > Directory 1 contains a file called "TEST_00.TXT".

>
> > > > Directory 2 contains a file called "TEST_00.TXT" already.

>
> > > > TEST_00.TXT from directory 1 needs to be moved to directory 2 and the
> > > > filename needs to be incremented by 1 if a file with the same name
> > > > already exists. *In this example the file would be moved to directory
> > > > 2 and the file renamed by the batch to "TEST_01.TXT".

>
> > > > Is this possible?

>
> > > > Thanks,

>
> > > > Brett

>
> > > Yes, it's possible.

>
> > > Rather than engage in a game of twenty questions, how about saying what you
> > > really want to do? That way, we can cut to the chase.

>
> > > For instance, what is the format of the name of the file you want to be
> > > moved? Is it constant? Does it always contain a string of digits? Is the
> > > digit string always of a particular length? Does it always appear jt
> > > immediately prior to the extension? Can the filename contain characters
> > > other than alphamerics and $#-@_ - especially spaces? Any unusual characters
> > > in the filename? Do you want the leading zeroes to be preserved? What
> > > happens when (in your example) TEST_00.TXT .. TEST_99.TXT already exist?
> > > Wouldn't it be easier to rename the file to "YYYYMMDDHHMMSS_TEST_00.TXT"
> > > instead, preserving the original name and presenting the name-sorted list in
> > > date/time order? Perhaps "TEST_00_YYYYMMDDHHSS.TXT" or
> > > "TEST_00.TXT.YYYYMMDDHHSS" would be more suitable? - Or any of these
> > > combinations, with YYYYMMDDHHSS replaced by a sequential generation-number?

>
> > > Since you're posting from XP, do you want an DOS/9x solution or an NT-class
> > > solution? The command-set for NT is far more powerful and discussed in
> > > alt.msdos.batch.nt by preference.

>
> > > Here's an NT+ job that will do what you've asked, but I don't believe it'll
> > > do what you actually want.

>
> > > ----- batch begins -------
> > > [1]@echo off
> > > [2]setlocal enabledelayedexpansion
> > > [3]copy zzz.zzz test_00.txt
> > > [4]for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist
> > > test_00.txt if not exist c:\destdir\test_!yrn!.txt move test_00.txt
> > > c:\destdir\test_!yrn!.txt &echo moved to test_!yrn!.txt
> > > [5]if exist test_00.txt echo Failed
> > > ------ batch ends --------

>
> > > Lines start [number] - any lines not starting [number] have been wrapped and
> > > should be rejoined. The [number] that starts the line should be removed

>
> > > %varname% will be evaluated as the value of VARNAME at the time that the
> > > line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
> > > !varname! to be evaluated as the CURRENT value of VARNAME - that is, as
> > > modified by the operation of the FOR

>
> > > [3] is simply to generate a new file to move, for testing.- Hide quoted text -

>
> > > - Show quoted text -

>
> > Hello,

>
> > Thank you for your help, I will try an answer your questions.

>
> > The format of the name of the file will always be 3 characters (text)
> > then an underscore, 1 character (txt) underscore, always 2 numbers,
> > always .TXT. *Some examples "ROB_P_00.TXT", "DRF_R_00.TXT".

>
> > What happens when (in your example) TEST_00.TXT .. TEST_99.TXT already
> > exist? *This is not an issue, files copied to the directory are moved
> > out a few times a day, the most number of the files the directory will
> > contain is 5.

>
> > We cannot use the timestamp and I would like an NT solution. *Your
> > example works really well if the file is always named TEST_00.TXT but
> > as the filename changes depending on the user is it possible not to
> > hardcode the filename?

>
> > Thanks,

>
> > Brett

>
> Here is my put (untested) based on your description ...
>
> @echo off
> set dest=X:\Somewhere
> if '%1'=='' pushd "%~1"
> for %%a in (???_?_??.txt) do (
> if not exist %%a (
> move "%%a" "%dest%\%%a"
> ) else (
> set newname=%%a
> set numb=1%newname:~-2%
> set numb +=1
> move "%%a" "%dest%\%newname:~0,-2%%numb:~-2%"
> )
> )
> if '%1'=='' popd
>
> It takes an optional input parameter that names the source folder.
> The target is presumed to be fixed so that it can be hard coded in
> place of X:\Somewhere. *If both source and destination are fixed,
> change the IF...PUSHD line to be ...
>
> * pushd Y:\Somewhereelse
>
> and the last line to just POPD.
>
> Tom Lavedas
> ===========http://members.cox.net/tglbatch/wsh/- Hide quoted text -
>
> - Show quoted text -


Hi Tom.

I copied your code into a BAT file but it did not seem to work. The
destination is fixed so I changed this to C:\Test. Do any of the
lines have to be wrapped and rejoined?

Thanks for your help.
Reply With Quote