Batch File, Move and Rename

223 Views Asked by At

I have a folder on my desktop which contains five sub folders, each of these sub folders holds about 1000 Pdf files. All of which have a 6 digit name (e.g 567788.pdf).

I would like to copy all the files to another folder and if they have the same name, instead of overwriting the file, I would like it to add something to the name if it is a duplicate.

e.g 567788.pdf -> 567788 (1).pdf

Could this be done using a batch file?

1

There are 1 best solutions below

0
On

For examlpe, try this:

find old_dir -type f |while read f ; do old_name=`basename $f`; if [ ! -e new_dir/$old_name ] ; then cp $f new_dir ; else i=1 ; while true ; do new_name="$old_name ($i)" ; if [ ! -e new_dir/$new_name ] ; then cp $f new_dir/$new_name ; break ; else i=`expr $i + 1` fi; done; fi; done

Duplicate filenames will receive suffixes (1), (2) etc.