fixunix
Tags Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Windows BAT File to Rename File depending if file exists in targetDirectory - MS-DOS

This is a discussion on Windows BAT File to Rename File depending if file exists in targetDirectory - MS-DOS ; 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 ...


Fix Unix > Microsoft Windows > MS-DOS > Windows BAT File to Rename File depending if file exists in targetDirectory

Reply
 
LinkBack Tools
  #1  
Old 01-10-2008, 02:25 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Windows BAT File to Rename File depending if file exists in targetDirectory

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

Reply With Quote
  #2  
Old 01-10-2008, 03:19 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory


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.




Reply With Quote
  #3  
Old 01-10-2008, 03:42 PM
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, 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

Reply With Quote
  #4  
Old 01-10-2008, 05:02 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists intarget Directory

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/
Reply With Quote
  #5  
Old 01-10-2008, 05:27 PM
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
  #6  
Old 01-10-2008, 06:45 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists intarget Directory

On Jan 10, 12:02 pm, 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 ...
>

{snip faulty code}
>
> 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/


I told you it was untested 8^}.

Here this is tested (a bit, anyway) ...

:: ========= start of procedure =========
@echo off
set dest=X:\Somewhere
if not '%1'=='' pushd "%~1"
for %%a in (???_?_??.txt) do (
if not exist %dest%\%%a (
move "%%a" "%dest%\%%a"
) else (
set newname=%%~na
call :Sub %%a
)
)
if not '%1'=='' popd
for %%a in (numb newname) do (set %%a=)
goto :eof

:Sub
set numb=1%newname:~-2%
set /a numb +=1
set newname=%newname:~0,-2%%numb:~-2%
if exist "%dest%\%newname%%~x1" goto :Sub
move "%1" "%dest%\%newname%%~x1"
:: ========= end of procedure =========

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Reply With Quote
  #7  
Old 01-11-2008, 12:36 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory

On Thu, 10 Jan 2008 10:45:58 -0800 (PST), Tom Lavedas wrote:

I think I've made it a bit more robust, handling & in the filenames etc, and
fixed a bug with long pathnames. The rest of my changes are idiosyncratic for
simplification - and I didn't test it.

:: ========= start of procedure =========
@echo off
setlocal
set "dest=X:\Somewhere"
pushd "%~1"
for %%a in (???_?_??.txt) do (
if not exist "%dest%\%%a" (
move "%%a" "%dest%\">nul
) else (
set "newname=%%~na"
call :Sub
)
)
popd
goto :eof

:Sub
set numb=1%newname:~-2%
set /a numb=numb+1
set "newname=%newname:~0,-2%%numb:~-2%"
if exist "%dest%\%newname%%~x1" goto :Sub
move "%1" "%dest%\">nul
:: ========= end of procedure =========

Reply With Quote
  #8  
Old 01-11-2008, 01:49 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory


wrote in message
news:39c112cc-cc01-4d71-a907-2a71df5ca5e2@z17g2000hsg.googlegroups.com...
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


----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]pushd c:\datasource
[4]copy c:\106x\zzz.zzz tes_z_00.txt
[5]copy c:\106x\zzz.zzz tes_z_01.txt
[6]for %%q in (???_?_??.txt) do (
[7]set yfp=%%q
[8]set yfp=!yfp:~0,6!
[9]for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist %%q if
not exist c:\destdir\!yfp!!yrn!.txt move %%q c:\destdir\!yfp!!yrn!.txt &echo
moved %%q to !yfp!!yrn!.txt
[10]if exist %%q echo Failed on %%q
[11])
[12]popd
[13]
------ 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

C:\106x... is my test directory.

%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

[4,5] are simply to generate files to move.

Does not verify that the filename format is as specified, only that the
fourth and sixth characters are underscores and the extension is ".txt".
Verifying that the first 3 characters and fifth are alphabetc and the
seventh and eight are numeric could be accomplished if required.



Reply With Quote
  #9  
Old 01-11-2008, 09:14 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists intarget Directory

On 11 Jan, 01:49, "billious" wrote:
> wrote in message
>
> news:39c112cc-cc01-4d71-a907-2a71df5ca5e2@z17g2000hsg.googlegroups.com...
> 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
>
> ----- batch begins -------
> [1]@echo off
> [2]setlocal enabledelayedexpansion
> [3]pushd c:\datasource
> [4]copy c:\106x\zzz.zzz tes_z_00.txt
> [5]copy c:\106x\zzz.zzz tes_z_01.txt
> [6]for %%q in (???_?_??.txt) do (
> [7]set yfp=%%q
> [8]set yfp=!yfp:~0,6!
> [9]for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist %%q if
> not exist c:\destdir\!yfp!!yrn!.txt move %%q c:\destdir\!yfp!!yrn!.txt &echo
> moved %%q to !yfp!!yrn!.txt
> [10]if exist %%q echo Failed on %%q
> [11])
> [12]popd
> [13]
> ------ 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
>
> C:\106x... is my test directory.
>
> %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
>
> [4,5] are simply to generate files to move.
>
> Does not verify that the filename format is as specified, only that the
> fourth and sixth characters are underscores and the extension is ".txt".
> Verifying that the first 3 characters and fifth are alphabetc and the
> seventh and eight are numeric could be accomplished if required.- Hide quoted text -
>
> - Show quoted text -


Hello,

I copied your code into notepad and saved as a batch file but it does
not seem to work.

Lines 4 and 5 confuse me,

> [4]copy c:\106x\zzz.zzz tes_z_00.txt
> [5]copy c:\106x\zzz.zzz tes_z_01.txt


I changed the directory to my directory but do I need to have a text
file in the directory called test_z_00.txt or a file called zzzz.zzzz.

Thanks,

Brett
Reply With Quote
  #10  
Old 01-11-2008, 09:40 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory


wrote in message
news:6b2adee0-60dd-45b9-b989-bbcd1ea45abd@e25g2000prg.googlegroups.com...
On 11 Jan, 01:49, "billious" wrote:
> wrote in message
>
> news:39c112cc-cc01-4d71-a907-2a71df5ca5e2@z17g2000hsg.googlegroups.com...
> 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
>
> ----- batch begins -------
> [1]@echo off
> [2]setlocal enabledelayedexpansion
> [3]pushd c:\datasource
> [4]copy c:\106x\zzz.zzz tes_z_00.txt
> [5]copy c:\106x\zzz.zzz tes_z_01.txt
> [6]for %%q in (???_?_??.txt) do (
> [7]set yfp=%%q
> [8]set yfp=!yfp:~0,6!
> [9]for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist %%q
> if
> not exist c:\destdir\!yfp!!yrn!.txt move %%q c:\destdir\!yfp!!yrn!.txt
> &echo
> moved %%q to !yfp!!yrn!.txt
> [10]if exist %%q echo Failed on %%q
> [11])
> [12]popd
> [13]
> ------ 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
>
> C:\106x... is my test directory.
>
> %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
>
> [4,5] are simply to generate files to move.
>
> Does not verify that the filename format is as specified, only that the
> fourth and sixth characters are underscores and the extension is ".txt".
> Verifying that the first 3 characters and fifth are alphabetc and the
> seventh and eight are numeric could be accomplished if required.- Hide
> quoted text -
>
> - Show quoted text -


Hello,

I copied your code into notepad and saved as a batch file but it does
not seem to work.

Lines 4 and 5 confuse me,

> [4]copy c:\106x\zzz.zzz tes_z_00.txt
> [5]copy c:\106x\zzz.zzz tes_z_01.txt


I changed the directory to my directory but do I need to have a text
file in the directory called test_z_00.txt or a file called zzzz.zzzz.

Thanks,

Brett

No.
Lines 4 and 5 are only to generate files for testing. I copy from my
"C:\106z\zzz.zzz" to "tes_z_00.txt" & "tes_z_01.txt" to create some files to
move. I then execute the batch several times to test that the files are
moved from tes_z_00.txt & tes_z_01.txt in the "datasource" directory to
tes_z_??.txt in the "destdir" directory. after 50 tests,
tes_z_00.txt..tes_z_99.txt will exist and the 'failed' message is produced.

Omit those lines. They are not relevant to your application.


Reply With Quote
  #11  
Old 01-11-2008, 11:31 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists intarget Directory

On 11 Jan, 09:40, "billious" wrote:
> wrote in message
>
> news:6b2adee0-60dd-45b9-b989-bbcd1ea45abd@e25g2000prg.googlegroups.com...
> On 11 Jan, 01:49, "billious" wrote:
>
>
>
>
>
> > wrote in message

>
> >news:39c112cc-cc01-4d71-a907-2a71df5ca5e2@z17g2000hsg.googlegroups.com...
> > 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

>
> > ----- batch begins -------
> > [1]@echo off
> > [2]setlocal enabledelayedexpansion
> > [3]pushd c:\datasource
> > [4]copy c:\106x\zzz.zzz tes_z_00.txt
> > [5]copy c:\106x\zzz.zzz tes_z_01.txt
> > [6]for %%q in (???_?_??.txt) do (
> > [7]set yfp=%%q
> > [8]set yfp=!yfp:~0,6!
> > [9]for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist %%q
> > if
> > not exist c:\destdir\!yfp!!yrn!.txt move %%q c:\destdir\!yfp!!yrn!.txt
> > &echo
> > moved %%q to !yfp!!yrn!.txt
> > [10]if exist %%q echo Failed on %%q
> > [11])
> > [12]popd
> > [13]
> > ------ 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

>
> > C:\106x... is my test directory.

>
> > %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

>
> > [4,5] are simply to generate files to move.

>
> > Does not verify that the filename format is as specified, only that the
> > fourth and sixth characters are underscores and the extension is ".txt".
> > Verifying that the first 3 characters and fifth are alphabetc and the
> > seventh and eight are numeric could be accomplished if required.- Hide
> > quoted text -

>
> > - Show quoted text -

>
> Hello,
>
> I copied your code into notepad and saved as a batch file but it does
> not seem to work.
>
> Lines 4 and 5 confuse me,
>
> > [4]copy c:\106x\zzz.zzz tes_z_00.txt
> > [5]copy c:\106x\zzz.zzz tes_z_01.txt

>
> I changed the directory to my directory but do I need to have a text
> file in the directory called test_z_00.txt or a file called zzzz.zzzz.
>
> Thanks,
>
> Brett
>
> No.
> Lines 4 and 5 are only to generate files for testing. I copy from my
> "C:\106z\zzz.zzz" to "tes_z_00.txt" & "tes_z_01.txt" to create some files to
> move. I then execute the batch several times to test that the files are
> moved from tes_z_00.txt & tes_z_01.txt in the "datasource" directory to
> tes_z_??.txt in the "destdir" directory. after 50 tests,
> tes_z_00.txt..tes_z_99.txt will exist and the 'failed' message is produced.
>
> Omit those lines. They are not relevant to your application.- Hide quoted text -
>
> - Show quoted text -


Hi,

I had another go but it still is not working. Please find below code
below I have copied into a txt file and saved as a bat file. The C:
\FXB file contains a file called "RKE_P_00.TXT".

Have I entered something wrong?

Thanks,

Brett

----- batch begins -------
@echo off
setlocal enabledelayedexpansion
pushd c:\fxb\
for %%q in (???_?_??.txt) do (
set yfp=%%q
set yfp=!yfp:~0,6!
for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist %%q
if not exist c:\FXB\ATC\!yfp!!yrn!.txt move %%q c:\FXB\ATC\!yfp!!
yrn!.txt &echo moved %%q to !yfp!!yrn!.txt
if exist %%q echo Failed on %%q
)
popd

------ batch ends --------
Reply With Quote
  #12  
Old 01-11-2008, 04:08 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory


wrote in message
news:e30adecf-8e18-4770-9e3c-5c00092af522@i72g2000hsd.googlegroups.com...
> On 11 Jan, 09:40, "billious" wrote:
>> wrote in message
>>
>> news:6b2adee0-60dd-45b9-b989-bbcd1ea45abd@e25g2000prg.googlegroups.com...
>> On 11 Jan, 01:49, "billious" wrote:
>>
>>
>>
>>
>>
>> > wrote in message

>>
>> >news:39c112cc-cc01-4d71-a907-2a71df5ca5e2@z17g2000hsg.googlegroups.com...
>> > 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.

>>

[snip]
>>
>> > ----- batch begins -------
>> > [1]@echo off
>> > [2]setlocal enabledelayedexpansion
>> > [3]pushd c:\datasource
>> > [4]copy c:\106x\zzz.zzz tes_z_00.txt
>> > [5]copy c:\106x\zzz.zzz tes_z_01.txt
>> > [6]for %%q in (???_?_??.txt) do (
>> > [7]set yfp=%%q
>> > [8]set yfp=!yfp:~0,6!
>> > [9]for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist
>> > %%q
>> > if
>> > not exist c:\destdir\!yfp!!yrn!.txt move %%q c:\destdir\!yfp!!yrn!.txt
>> > &echo
>> > moved %%q to !yfp!!yrn!.txt
>> > [10]if exist %%q echo Failed on %%q
>> > [11])
>> > [12]popd
>> > [13]
>> > ------ 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

>>
>> > C:\106x... is my test directory.

>>
>> > %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

>>
>> > [4,5] are simply to generate files to move.

>>
>> > Does not verify that the filename format is as specified, only that the
>> > fourth and sixth characters are underscores and the extension is
>> > ".txt".
>> > Verifying that the first 3 characters and fifth are alphabetc and the
>> > seventh and eight are numeric could be accomplished if required.- Hide
>> > quoted text -

>>
>> > - Show quoted text -

[snip - lines 4 and 5 are for testing only]
>
> Hi,
>
> I had another go but it still is not working. Please find below code
> below I have copied into a txt file and saved as a bat file. The C:
> \FXB file contains a file called "RKE_P_00.TXT".
>
> Have I entered something wrong?
>
> Thanks,
>
> Brett
>
> ----- batch begins -------
> @echo off
> setlocal enabledelayedexpansion
> pushd c:\fxb\
> for %%q in (???_?_??.txt) do (
> set yfp=%%q
> set yfp=!yfp:~0,6!
> for /l %%i in (100,1,199) do set yrn=%%i&set yrn=!yrn:~1!&if exist %%q
> if not exist c:\FXB\ATC\!yfp!!yrn!.txt move %%q c:\FXB\ATC\!yfp!!
> yrn!.txt &echo moved %%q to !yfp!!yrn!.txt
> if exist %%q echo Failed on %%q
> )
> popd
>
> ------ batch ends --------


It appears OK, BUT it's very difficult to tell where your line-breaks are.
In batch, this is critical. That is why I post with bracketed line numbers.

I created a file "C:\fxb\rke_p_00.txt" and a directory "C:\fxb\atc" and ran
this batch successfully exactly as you posted it - AFTER editing the lines
so that the line-breaks occurred as I'd originally posted. I also use EDIT
rather than NOTEPAD as NOTEPAD has a habit of formatting the file on the
assumption that it's a text file.

To access EDIT, simply type

EDIT batfilename.bat

at the prompt. It should be obvious how to use it.

"It's not working" isn't a helpful comment, unfortunately. Does it produce
any screen output at all? Does it report a syntax error, perhaps? Does it
report that it failed to move the file?

if you insert an extra line between [6] and [7] of my posted code,

echo processing %%q

then it should produce a report "processing filename" for each filename that
it finds matching the mask "???_?_??.txt" - that might aid in debugging.

Here's (approximately) the batch I use to bracket the batch file and load it
into NOTEPAD ready for cut-and-paste:

----- batch begins -------
[1]@echo off&setlocal
[2]set yfn=%1
[3]if not defined yfn echo Syntax : textify filename&echo (textifies
filename.bat to filename.txt)&goto :eof
[4]for %%i in ("%yfn%") do if /i "%%~xi"==".bat" set yfn=%%~dpni
[5]if not exist %yfn%.bat echo %yfn%.bat not found&goto :eof
[6]for %%i in ("%yfn%.bat") do set ytf="%temp%\%%~ni.txt"
[7]echo ----- batch begins ------->>%ytf%
[8]find /v /n "" <%yfn%.bat >>%ytf%
[9]echo ------ batch ends -------->>%ytf%
[10]start "" notepad %ytf%
[11]
------ 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

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof


Reply With Quote
  #13  
Old 01-24-2008, 04:03 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory

On Thu, 24 Jan 2008 07:49:02 -0800 (PST), brettmanning24@gmail.com wrote:

>On 11 Jan, 16:24, foxidrive wrote:
>> On Sat, 12 Jan 2008 00:08:24 +0800, "billious"
>> wrote:
>>
>> *----- batch begins -------
>> *@echo off
>> *setlocal enabledelayedexpansion
>> *pushd c:\fxb\
>> *for %%q in (???_?_??.txt) do (
>> *set yfp=%%q
>> *set yfp=!yfp:~0,6!
>> *for /l %%i in (100,1,199) do (
>> *set yrn=%%i
>> *set yrn=!yrn:~1!
>> *if exist %%q if not exist c:\FXB\ATC\!yfp!!yrn!.txt (
>> *move %%q c:\FXB\ATC\!yfp!! yrn!.txt


The line above looks to have a space in it that crept in during editing - it
should be as below:

move %%q c:\FXB\ATC\!yfp!!yrn!.txt

>> *echo moved %%q to !yfp!!yrn!.txt
>> *)
>> *)
>> *if exist %%q echo Failed on %%q & pause
>> *)
>> *popd
>>

>---------------
>
>Sorry for the delayed reply, I am still having problems with the
>script, please find below error received. I copied and pasted the
>script above and the format looks correct.


The unusual thing is there is another two spaces below that are not in the
script above.

Check the ends of these two lines in your script to see if there are any spaces
there.

>> *set yrn=%%i
>> *set yrn=!yrn:~1!


>The syntax of the command is incorrect.
>moved ATC_P_01.txt to ATC_P_ 00 .txt
>The syntax of the command is incorrect.
>moved ATC_P_01.txt to ATC_P_ 01 .txt
>The syntax of the command is incorrect.


Reply With Quote
  #14  
Old 01-24-2008, 04:18 PM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: Windows BAT File to Rename File depending if file exists in target Directory

brettmanning24@gmail.com wrote:
> On 11 Jan, 16:24, foxidrive wrote:
>> On Sat, 12 Jan 2008 00:08:24 +0800, "billious"
>> wrote:
>>
>> ----- batch begins -------
>> @echo off
>> setlocal enabledelayedexpansion
>> pushd c:\fxb\
>> for %%q in (???_?_??.txt) do (
>> set yfp=%%q
>> set yfp=!yfp:~0,6!
>> for /l %%i in (100,1,199) do (
>> set yrn=%%i
>> set yrn=!yrn:~1!
>> if exist %%q if not exist c:\FXB\ATC\!yfp!!yrn!.txt (
>> move %%q c:\FXB\ATC\!yfp!!yrn!.txt
>> echo moved %%q to !yfp!!yrn!.txt
>> )
>> )
>> if exist %%q echo Failed on %%q & pause
>> )
>> popd
>>
>> ------ batch ends --------
>>
>>> It appears OK, BUT it's very difficult to tell where your
>>> line-breaks are. In batch, this is critical. That is why I post
>>> with bracketed line numbers.

>>
>> With a little care and formatting there is seldom any need to post

>
> Sorry for the delayed reply, I am still having problems with the
> script, please find below error received. I copied and pasted the
> script above and the format looks correct.
>
> The syntax of the command is incorrect.
> moved ATC_P_01.txt to ATC_P_ 00 .txt
> The syntax of the command is incorrect.
> moved ATC_P_01.txt to ATC_P_ 01 .txt
> The syntax of the command is incorrect.
> moved ATC_P_01.txt to ATC_P_ 02 .txt
> The syntax of the command is incorrect.
> moved ATC_P_01.txt to ATC_P_ 03 .txt
> The syntax of the command is incorrect.
> moved ATC_P_01.txt to ATC_P_ 04 .txt
> The syntax of the command is incorrect.
> moved ATC_P_01.txt to ATC_P_ 05 .txt
> Press any key to continue . . .
>
> Any ideas?
>
> Thanks,
>
> Brett


Your output format shows that there is a space between the underscore and
the numeric group and two spaces between the numeric group and the ".txt"
for the line
echo moved %%q to !yfp!!yrn!.txt

This would indicate that transmission, cutting and pasting has added some
spaces - probably whilst trying to format the text as though it was a
natural language.

I would suggest that you use EDIT to edit the batch file. From the prompt,
simply execute
EDIT yourbatchfilename

and remove any stray spaces not evident in the original text. Pay special
attention to spaces before or after punctuation characters and symbols.

You could possibly make use of a few debug lines of the format
echo yrn=+!yrn!+

Put after a 'SET yrn=...' line, for instance - where yrn can be any variable
name (except the metasymbols used to control the loops - %%q for example,
which may be ECHOed with

echo +%%q+
)

Putting a "+" (or "#" or something similar) will show you whether and where
a space is being inserted. Dealing with it should be a matter of simply
removing the space using EDIT.

If lines end with a spce under EDIT, they're a little difficult to see.
simply put the cursor on the line and press the END key, which should move
the cursor to just after the last visible character. If it goes one more
character, then there is a terminal space on the line, which you can delete
using the backspace key.




Reply With Quote
Reply

Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows BAT File to Rename File Depending on Target Directory unix MS-DOS 2 01-12-2008 05:12 PM
Batch file to rename a file with time and date unix MS-DOS 55 12-02-2007 09:14 PM
Batch file rename question (consecutive file numbers) unix Unix 2 10-04-2007 04:23 AM
Cannot create a file when that file already exists unix Windows CE 0 10-01-2007 01:34 PM
how to rename multiple file name ddmmTAUD.A01 to ddmmA01TAUD.dbf in batch file? unix MS-DOS 2 08-23-2006 04:41 PM


All times are GMT. The time now is 08:26 AM.