Copy a file even if it already exist with unique name!
Moderator: DosItHelp
Copy a file even if it already exist with unique name!
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?
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?
Re: Copy a file even if it already exist with unique name!
It's easier to add a date stamp to the filename. Is that something you would consider?
temp 2015-01-22_06.23.txt
temp 2015-01-22_06.23.txt
Re: Copy a file even if it already exist with unique name!
Sorry i am not looking to add date time stamp. I need to increment the file sequentially if they already exist.
Re: Copy a file even if it already exist with unique name!
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 ...
Re: Copy a file even if it already exist with unique name!
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.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 ...
Re: Copy a file even if it already exist with unique name!
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.
Otherwise I would suggest you invest in a Concurrent Versioning System that all parties involved have access to.
Re: Copy a file even if it already exist with unique name!
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
Re: Copy a file even if it already exist with unique name!
here is my solution...
usage
copy_v4.bat
after 1st run
after 2nd run
after 3rd run
after deleting "F:\Test Zone!!\temp(1).txt" and 4th run
enjoy
_
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.
Re: Copy a file even if it already exist with unique name!
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
Re: Copy a file even if it already exist with unique name!
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.
But you could use the Creation date to force it back into order.
But maybe a better solution would be to just use an IF statement to compare all possibilities.
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.
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.
Re: Copy a file even if it already exist with unique name!
Antonio, your code relies on the files remaining static, is that right?
I made this comment earlier - the OP didn't reply though.
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...
Re: Copy a file even if it already exist with unique name!
Thanks everyone for your reply...
Re: Copy a file even if it already exist with unique name!
sam14189 wrote:Thanks everyone for your reply...
I hope you understand all the possible things that could go wrong.
Re: Copy a file even if it already exist with unique name!
i improved my copy.bat (copy_v2.bat) with tests for %2, <source_path\filename.ext> and <destiantion_path>
_
_
Re: Copy a file even if it already exist with unique name!
Yes I will look for some alternative approach.thanks for the help