Copy a file even if it already exist with unique name!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sam14189
Posts: 31
Joined: 22 Jan 2015 02:57

Copy a file even if it already exist with unique name!

#1 Post by sam14189 » 22 Jan 2015 03:19

Hi All,
I have a requirement in which i need to copy a file to destination folder. If the file already exist then i want another copy of file to be created. For e.g. - I have source file at C:\Data\temp.txt and want to copy to C:\ABC. Now if temp.txt is already present at C:\ABC then it should be copied as temp(2).txt. Again if run the script this time the file should be copied as temp(3).txt and so on.. Is there any way to do this?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy a file even if it already exist with unique name!

#2 Post by foxidrive » 22 Jan 2015 03:36

It's easier to add a date stamp to the filename. Is that something you would consider?

temp 2015-01-22_06.23.txt

sam14189
Posts: 31
Joined: 22 Jan 2015 02:57

Re: Copy a file even if it already exist with unique name!

#3 Post by sam14189 » 22 Jan 2015 03:39

Sorry i am not looking to add date time stamp. I need to increment the file sequentially if they already exist.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy a file even if it already exist with unique name!

#4 Post by foxidrive » 22 Jan 2015 03:49

sam14189 wrote:Sorry i am not looking to add date time stamp. I need to increment the file sequentially if they already exist.


What is the reason that you need a numeral?

One problem with a simple unpadded numeral is that the files will not sort properly in a command line ...

sam14189
Posts: 31
Joined: 22 Jan 2015 02:57

Re: Copy a file even if it already exist with unique name!

#5 Post by sam14189 » 22 Jan 2015 03:59

foxidrive wrote:
sam14189 wrote:Sorry i am not looking to add date time stamp. I need to increment the file sequentially if they already exist.


What is the reason that you need a numeral?

One problem with a simple unpadded numeral is that the files will not sort properly in a command line ...
The reason i am looking to add numeral is we need to check how many copies of files were received on server and in case they asked us to check 5th copy of file then it would be easier for us if we have file in destination folder as temp(5).txt or temp_5.txt.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Copy a file even if it already exist with unique name!

#6 Post by Squashman » 22 Jan 2015 05:55

I do data automation for a living and all files we get from clients are date and time stamped in the file name. These are multi-billion dollar companies we do work for. It is by far the the most error free way of doing something like this.

Otherwise I would suggest you invest in a Concurrent Versioning System that all parties involved have access to.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy a file even if it already exist with unique name!

#7 Post by foxidrive » 22 Jan 2015 15:24

sam14189 wrote:The reason i am looking to add numeral is we need to check how many copies of files were received on server and in case they asked us to check 5th copy of file then it would be easier for us if we have file in destination folder as temp(5).txt or temp_5.txt.


What will happen to all these files? Will any of them ever be removed?
if you remove say file 5 then the code will need extra checking to bypass number 5 and continue to the highest numeral used.

Without the actual rules for the task you may come back and say it doesn't work because of x y and z.

See here: viewtopic.php?f=3&t=6108

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: Copy a file even if it already exist with unique name!

#8 Post by catalinnc » 23 Jan 2015 18:04

here is my solution...

usage

Code: Select all

copy.bat "F:\!!_S T O R E_!!\temp.txt" ""F:\Test Zone!!\"


copy_v4.bat

Code: Select all

@echo off & cls

setlocal disabledelayedexpansion

if "%~2" == "" endlocal & echo(usage: copy.bat ^<source_path\filename.ext^> ^<destiantion_path^> & echo( & pause & goto :eof
if not exist "%~1" endlocal & echo(invalid ^<source_path\filename.ext^> &echo( & echo("%~1" file not found & echo( & pause & goto :eof
if exist "%~1\" endlocal & echo(invalid ^<source_path\filename.ext^> &echo( & echo("%~1" is a folder & echo( & pause & goto :eof
if not exist "%~2" endlocal & echo(invalid ^<destiantion_path^> &echo( & echo("%~2" folder not found & echo( & pause & goto :eof
if not exist "%~2\" endlocal & echo(invalid ^<destiantion_path^> &echo( & echo("%~2" is a file & echo( & pause & goto :eof

set "_path_source=%~1"

set "_path_destination=%~2"
if "%_path_destination:~-1%" == "\" set "_path_destination=%_path_destination:~0,-1%"

set "_file_name=%~n1"

set "_file_extension=%~x1"

set "_max_copy_number=256"

set "_founded=false"

echo(copy "%_file_name%%_file_extension%"
echo(
echo(from: "%_path_source%"
echo(

for /l %%F in (%_max_copy_number%,-1,1) do (

setlocal enabledelayedexpansion

if "!_founded!" == "false" (endlocal

if not exist "%_path_destination%\%_file_name%(%%F)%_file_extension%" (

set "_copy_number=%%F"

) else (if %%F equ %_max_copy_number% (

cls & echo("%_file_name%(%%F)%_file_extension%" already exist & echo( & echo(increase variable _max_copy_number value beyond %_max_copy_number% and retry & echo(

echo(dir /b "%_path_destination%\%_file_name%*" & dir /b "%_path_destination%\%_file_name%*" & echo( & echo( & pause & goto :eof

)

set "_founded=true")

) else endlocal

)

if exist "%_path_destination%\%_file_name%%_file_extension%" set "_founded=true"

if "%_founded%" == "false" (

copy /b "%_path_source%" "%_path_destination%\%_file_name%%_file_extension%" > nul

echo(  to: "%_path_destination%\%_file_name%%_file_extension%"

) else (

copy /b "%_path_source%" "%_path_destination%\%_file_name%(%_copy_number%)%_file_extension%" > nul

echo(  to: "%_path_destination%\%_file_name%(%_copy_number%)%_file_extension%")

echo(
echo(
echo(dir /b "%_path_destination%\%_file_name%*"
dir /b "%_path_destination%\%_file_name%*"
echo(
echo(


endlocal

pause

exit /b


after 1st run

Code: Select all

copy "temp.txt"

from: "F:\8\!!_S T O R E_!!\temp.txt"

  to: "F:\Test Zone!!\temp.txt"


dir /b "F:\Test Zone!!\temp*"
temp.txt


Press any key to continue . . .


after 2nd run

Code: Select all

copy "temp.txt"

from: "F:\8\!!_S T O R E_!!\temp.txt"

  to: "F:\Test Zone!!\temp(1).txt"


dir /b "F:\Test Zone!!\temp*"
temp(1).txt
temp.txt


Press any key to continue . . .


after 3rd run

Code: Select all

copy "temp.txt"

from: "F:\8\!!_S T O R E_!!\temp.txt"

  to: "F:\Test Zone!!\temp(2).txt"


dir /b "F:\Test Zone!!\temp*"
temp(1).txt
temp(2).txt
temp.txt


Press any key to continue . . .


after deleting "F:\Test Zone!!\temp(1).txt" and 4th run

Code: Select all

copy "temp.txt"

from: "F:\8\!!_S T O R E_!!\temp.txt"

  to: "F:\Test Zone!!\temp(3).txt"


dir /b "F:\Test Zone!!\temp*"
temp(2).txt
temp(3).txt
temp.txt


Press any key to continue . . .


enjoy
_
Last edited by catalinnc on 29 Jan 2015 17:02, edited 3 times in total.

Aacini
Expert
Posts: 1926
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Copy a file even if it already exist with unique name!

#9 Post by Aacini » 24 Jan 2015 12:45

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem CopyNew.bat - Antonio Perez Ayala

if "%~2" equ "" echo CopyNew sourceFile destinationFolder & goto :EOF
if not exist %1 echo Source file not found & goto :EOF
if not exist %2 echo Destination folder not found & goto :EOF

set "newName=%~1"
if not exist "%~2\%newName%" goto copyNew
   set "newNum=2"
   for /F "tokens=2 delims=()" %%n in ('dir /B /O-D "%~2\%~N1(*)%~X1" 2^>NUL') do set /A newNum=%%n+1 & goto continue
   :continue
   set "newName=%~N1(%newNum%)%~X1"
:copyNew
ECHO copy %1 "%~2\%newName%"
copy %1 "%~2\%newName%"

Output example:

Code: Select all

C:\> dir /B
CopyNew.bat

C:\> md folder

C:\> CopyNew.bat CopyNew.bat folder
copy CopyNew.bat "folder\CopyNew.bat"
        1 archivo(s) copiado(s).

C:\> CopyNew.bat CopyNew.bat folder
copy CopyNew.bat "folder\CopyNew(2).bat"
        1 archivo(s) copiado(s).

C:\> CopyNew.bat CopyNew.bat folder
copy CopyNew.bat "folder\CopyNew(3).bat"
        1 archivo(s) copiado(s).

C:\> dir /B folder
CopyNew(2).bat
CopyNew(3).bat
CopyNew.bat

C:\> del "folder\CopyNew(2).bat"

C:\> CopyNew.bat CopyNew.bat folder
copy CopyNew.bat "folder\CopyNew(4).bat"
        1 archivo(s) copiado(s).

C:\> dir /B folder
CopyNew(3).bat
CopyNew(4).bat
CopyNew.bat

Antonio

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Copy a file even if it already exist with unique name!

#10 Post by Squashman » 24 Jan 2015 17:12

Hi Antonio,
I like your code. Short and sweet.
But can we really assume that the file with the newest time stamp has the highest number?
What happens when some one edits file 2 of 4.

Code: Select all

C:\BatchFiles\COPY>dir /B /O-D file(*).txt
file(4).txt
file(3).txt
file(2).txt
file(1).txt

C:\BatchFiles\COPY>echo blah>>file(2).txt

C:\BatchFiles\COPY>dir /B /O-D file(*).txt
file(2).txt
file(4).txt
file(3).txt
file(1).txt


But you could use the Creation date to force it back into order.

Code: Select all

C:\BatchFiles\COPY>dir /B /O-D /TC file(*).txt
file(4).txt
file(3).txt
file(2).txt
file(1).txt

C:\BatchFiles\COPY>


But maybe a better solution would be to just use an IF statement to compare all possibilities.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem CopyNew.bat - Antonio Perez Ayala

if "%~2" equ "" echo CopyNew sourceFile destinationFolder & goto :EOF
if not exist %1 echo Source file not found & goto :EOF
if not exist %2 echo Destination folder not found & goto :EOF

set "newName=%~1"
if not exist "%~2\%newName%" goto copyNew

set "newNum=1"
for /F "tokens=2 delims=()" %%n in ('dir /B /O-D "%~2\%~N1(*)%~X1" 2^>NUL') do (
   IF %%n GTR !newNum! SET newNum=%%n
)
set /A newNum+=1

:continue
set "newName=%~N1(%newNum%)%~X1"

:copyNew
ECHO copy %1 "%~2\%newName%"
copy %1 "%~2\%newName%


Of course there is only so much Human Error checking you can do. Some idiot could put the letter A inside the parentheses and A would become the largest value. If someone deletes the base file name from the backup folder the next time that file comes in, it would become the first base file name with no number.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy a file even if it already exist with unique name!

#11 Post by foxidrive » 25 Jan 2015 05:30

Antonio, your code relies on the files remaining static, is that right?

I made this comment earlier - the OP didn't reply though.

foxidrive wrote:What will happen to all these files? Will any of them ever be removed?
if you remove say file 5 then the code will need extra checking...

sam14189
Posts: 31
Joined: 22 Jan 2015 02:57

Re: Copy a file even if it already exist with unique name!

#12 Post by sam14189 » 25 Jan 2015 13:12

Thanks everyone for your reply...

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Copy a file even if it already exist with unique name!

#13 Post by Squashman » 25 Jan 2015 15:11

sam14189 wrote:Thanks everyone for your reply...

I hope you understand all the possible things that could go wrong.

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: Copy a file even if it already exist with unique name!

#14 Post by catalinnc » 25 Jan 2015 15:13

i improved my copy.bat (copy_v2.bat) with tests for %2, <source_path\filename.ext> and <destiantion_path>
_

sam14189
Posts: 31
Joined: 22 Jan 2015 02:57

Re: Copy a file even if it already exist with unique name!

#15 Post by sam14189 » 26 Jan 2015 06:54

Yes I will look for some alternative approach.thanks for the help

Post Reply