Using variable file name

F

Frank

I am trying to find a way to copy a file to a specific
folder and not overwrite the existing file using either
batch file or scripting.

For ex.

copy test.txt c:\backup\test(variable).txt


any suggestions?
 
P

Phil Robyn

Frank said:
I am trying to find a way to copy a file to a specific
folder and not overwrite the existing file using either
batch file or scripting.

For ex.

copy test.txt c:\backup\test(variable).txt


any suggestions?

Just make sure that the value of '(variable)' is unique.
Maybe a combination of the date and the time? ;-)
 
G

Guest

Just make sure that the value of '(variable)' is unique.
Maybe a combination of the date and the time? ;-)


That's exactly what I want to do. But how do you add a
variable to the filename from a batch file?
 
P

Phil Robyn [MVP]

That's exactly what I want to do. But how do you add a
variable to the filename from a batch file?

- - - - - - - - - - begin screen capture Win2000 - - - - - - - - - -
c:\cmd>demo\DateAndTimeInFileName
copy test.txt c:\backup\test20040310_11-14-41.txt

c:\cmd>rlist demo\DateAndTimeInFileName.cmd
=====begin c:\cmd\demo\DateAndTimeInFileName.cmd ====================
1. @echo off
2. set ymd=%date:~10,4%%date:~4,2%%date:~7,2%
3. set hms=%time:~0,2%-%time:~3,2%-%time:~6,2%
4. set hms=%hms: =0%
5. echo copy test.txt c:\backup\test%ymd%_%hms%.txt
=====end c:\cmd\demo\DateAndTimeInFileName.cmd ====================
- - - - - - - - - - end screen capture Win2000 - - - - - - - - - -

If this appears to work for you, remove the word 'echo' from line 5
to actually do the copying.
 
M

Matthias Tacke

Frank said:
I am trying to find a way to copy a file to a specific
folder and not overwrite the existing file using either
batch file or scripting.

For ex.

copy test.txt c:\backup\test(variable).txt


any suggestions?

Should (variable) be a counter to increase in case the file exists?

::CopyCount.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal enableextensions
if [%1]==[] goto :Usage
if [%2]==[] goto :Usage
set Cnt=1
set DestAttr=%~a2
if [%DestAttr:~0,1%]==[d] (
set Dest=%~f2\%~n1
set ext=%~x1
) else (
set Dest=%~dp2%~n2
set ext=%~x2
)
if NOT exist "%dest%%ext%" (
copy %1 %Dest%%ext%
) else (
:loop
if exist "%Dest%(%Cnt%)%ext%" set /A Cnt+=1&goto :loop
copy %1 "%Dest%(%Cnt%)%ext%"
)
goto :EOF
:Usage
echo/Usage: %~n0 Source Dest (Dest may be file or folder)
echo/
echo/if Dest exists a number in parenthes is appended.
echo/if Dest with number exists the number is increased.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 
G

Guest

WOW! I am amazed! Thanks, I will try it.

Thanks again.

-----Original Message-----
Frank said:
I am trying to find a way to copy a file to a specific
folder and not overwrite the existing file using either
batch file or scripting.

For ex.

copy test.txt c:\backup\test(variable).txt


any suggestions?

Should (variable) be a counter to increase in case the file exists?

::CopyCount.cmd::::::::::::::::::::::::::::::::::::::::::: ::::::::::::
@echo off & setlocal enableextensions
if [%1]==[] goto :Usage
if [%2]==[] goto :Usage
set Cnt=1
set DestAttr=%~a2
if [%DestAttr:~0,1%]==[d] (
set Dest=%~f2\%~n1
set ext=%~x1
) else (
set Dest=%~dp2%~n2
set ext=%~x2
)
if NOT exist "%dest%%ext%" (
copy %1 %Dest%%ext%
) else (
:loop
if exist "%Dest%(%Cnt%)%ext%" set /A Cnt+=1&goto :loop
copy %1 "%Dest%(%Cnt%)%ext%"
)
goto :EOF
:Usage
echo/Usage: %~n0 Source Dest (Dest may be file or folder)
echo/
echo/if Dest exists a number in parenthes is appended.
echo/if Dest with number exists the number is increased.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::

HTH
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
.
 
M

Matthias Tacke

:

Uups, missed quotes one time.
Frank said:
I am trying to find a way to copy a file to a specific
folder and not overwrite the existing file using either
batch file or scripting.

For ex.

copy test.txt c:\backup\test(variable).txt


any suggestions?

Should (variable) be a counter to increase in case the file exists?

::CopyCount.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal enableextensions
if [%1]==[] goto :Usage
if [%2]==[] goto :Usage
set Cnt=1
set DestAttr=%~a2
if [%DestAttr:~0,1%]==[d] (
set Dest=%~f2\%~n1
set ext=%~x1
) else (
set Dest=%~dp2%~n2
set ext=%~x2
)
if NOT exist "%dest%%ext%" (
copy %1 %Dest%%ext%

copy %1 "%Dest%%ext%"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top