When copying/moving, appending " (2)" to filename when the target file already exist.

Oct 20, 2017
31
0
Netherlands
Question: When a filename in the target folder already exists, is there a simple solution, like appending "(2)" to the filename, before copying/moving the file to the target directory?
 
There is no easy solution. You could turn on clobber mode and COPY will prompt you before overwriting an existing file.
Otherwise you'll have to write a custom COPY script that checks for an existing file and formats a new name based on the existing name.
This isn't checked, but should be close. You'll probably need to do something with quotes too if there are any spaces.
CustomCopy.btm:
Code:
set dest=%2
iff ISFILE %dest then
  do i = 1 to 65535
    if ISFILE %@PATH[%2]%@NAME[%2](%i).%EXT[%2] ITERATE
    set dest=%@PATH[%2]%@NAME[%2](%i).%EXT[%2]
    LEAVE
  enddo
endiff
copy %1 %dest
 
A full blown COPY command would need to check for a destination directory, include lists, regular expressions, wild cards, etc. So an ideal solution would have to be done internally.
 
Question: When a filename in the target folder already exists, is there a simple solution, like appending "(2)" to the filename, before copying/moving the file to the target directory?

Take a look at the @VERSION function in TCC help, I think it will do what you want. For example, if two directories DIR1 and DIR2 both contain a file called TESTFILE.TXT:

Code:
d:\>dir /b dir1
testfile.txt

d:\>dir /b dir2
testfile.txt

Then the following command will successfully copy TESTFILE.TXT from DIR1 to DIR2, appending a version number to the end of the filename:

Code:
d:\>copy \dir1\testfile.txt %@version[\dir2\testfile.txt,_]
D:\dir1\testfile.txt => D:\dir2\testfile.txt_1

     1 file copied

d:\>dir /b dir2
testfile.txt
testfile.txt_1
 
"Extension" is merely a convention. You never know if last dot in a file name actually designates an "extension".
 

Similar threads