Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Copy files toa folder, add suffix to prevent overwrite.  (Read 7708 times)

0 Members and 1 Guest are viewing this topic.

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Copy files toa folder, add suffix to prevent overwrite.
« on: July 29, 2017, 07:13:33 PM »
I have seen it done. Somehow there is a way of putting files in one folder and adding a number to the end of the name to prevent overwrite. But how do I tell a copy utility to do that? Or is there a special utility that will do that?

Example:
I search for files with a string. I find a lot with the same name. I want to copy them to a USB flash and do some more searching on the files. So,  has to be be a way to add a number suffix to prevent overwrite.
How can it be done?  ::)

Salmon Trout

  • Guest
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #1 on: July 30, 2017, 12:57:06 AM »
Windows 7 onwards can automatically add suffix numbers when you paste a file or files into a folder where the same file name exists. I mean using Windows Explorer.

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #2 on: July 30, 2017, 02:29:12 AM »
Hi  ;)
You don't provide any code, so here is a startup code for you to test and modify it for your puropse  : How to Copy (and increment) Multiple Instances of a File Using Batch File

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #3 on: July 30, 2017, 10:28:32 AM »
Thank you both. That was helpful.


Salmon Trout

  • Guest
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #4 on: July 30, 2017, 12:17:34 PM »
In Windows 7 or 10, if you are using Windows Explorer, try copying a test file from one folder and pasting into another empty folder. Then go back and do it again. When you click 'paste' the second time you will be offered 3 choices -

(1) Replace destination file (overwrites file that is already there)
(2) Skip copying (copies nothing, leaves alone file that is already there)


(3) Compare both files; you can then replace, skip or rename the newer file with a number

If you already have just New Text Document.txt, the next time you pasted a file with that same name, in the same folder, it will be pasted as New Text Document (2).txt

Try to paste in same folder, choose 3rd option:


Check both boxes:


Newer file is renamed:


This is how it looks in Windows 10, but Windows 7 is very similar. if you are doing lots of files, they will be renumbered consecutively.




« Last Edit: July 30, 2017, 12:54:54 PM by Salmon Trout »

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #5 on: July 30, 2017, 01:54:59 PM »
Thanks,
The graphical way is much easier that trying to make a batch script.
I think I saw somersetting like that before, but I con not remember how.  8)

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #6 on: August 05, 2017, 11:59:10 AM »
Hi  ;)
I made for this batch script to copy with incrementaion
Copy_TXT_Files.bat
Code: [Select]
@echo off
::***********************************************************************************
Mode con cols=80 lines=5 & color 9E
Title Searching for a connected USB drive .....
:Find_USB_Drive
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=2" ^|find /i ":"') do (Set MyUSB=%%i)
cls
setlocal ENABLEDELAYEDEXPANSION
set _drive=%MyUSB%
If Exist !_drive! (
cls
echo           #########################################################
echo                        Your usb key is connected as !_drive!
echo           #########################################################
echo Press any key for copying files on this drive !_drive!
pause>nul & goto Main
) ELSE (
cls
color 0C
echo.
echo           #########################################################
echo                         Your usb key is not detected
echo           #########################################################
echo.
pause>nul & exit
)
::***********************************************************************************
:Main
set "Pattern=*.txt"
Mode con cols=90 lines=5 & color 9E
Set "Location=%userprofile%\Desktop"
Set "Ext=TXT"
Set "Destination=!_drive!\Copy_%EXT%_Files"
Title Searching for "%Pattern%" files paths and copy them to "%Destination%
set "LogSearch=%~dp0%~n0.txt"
Set "CopyLog=%~dp0%~n0_CopyLog.txt"
If exist "%CopyLog%" Del "%CopyLog%"
Set Count=1
If exist "%LogSearch%" Del "%LogSearch%"
Cls
echo(
echo       --------------------------------------------------------------------------
echo              Please Wait a while ....... Searching is in progress .........
echo       --------------------------------------------------------------------------
   Where /R %Location% "%Pattern%" /F >>"%LogSearch%" 2>&1

If "%ErrorLevel%" EQU "1" (
Cls
echo(
echo       --------------------------------------------------------------------------
echo                         No file(s^) found with this Pattern
echo       --------------------------------------------------------------------------
)

If "%ErrorLevel%" EQU "0" (
If Not exist "%Destination%" MD "%Destination%"
Setlocal enableDelayedExpansion
@for /f "delims=" %%a in ('Type "%LogSearch%"') do (
If not exist "%Destination%\%%~nxa" (
@echo copying file %%a >> %CopyLog% 2>&1
@Copy "%%~a" "%Destination%" >> %CopyLog% 2>&1
) else (
Call :Increment_Copy "%%~a" "%Destination%"
)
)
)
Start "" "%LogSearch%"
Start "" "%CopyLog%"
If exist "%Destination%" Explorer "%Destination%"
exit
::**************************************************************************
:Increment_Copy <source> <Destination>
set "Source=%~1"
set "Destination=%~2"
set "Filename=%~n1"
if exist "%Destination%\%Filename%(%Count%)%~x1" set /a Count+=1 && goto Increment_Copy
@echo "%Source%" "%Destination%\%Filename%(%Count%)%~x1" >> %CopyLog% 2>&1
@copy "%Source%" "%Destination%\%Filename%(%Count%)%~x1" >> %CopyLog% 2>&1
set /a Count=1
exit /b
::**************************************************************************

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #7 on: August 08, 2017, 02:53:24 AM »
Hi  ;)
Here is another improved version for Incremental_Copy.bat : This script can search for any batch script *.bat located on your hard drive C:\ and copy them with incrementation, ie (To avoid overwritten copy) in other location that you can fix it by the script for example,  i choose E:\
If you want for example to backup all your *.vbs files, just change this variable set "Ext=VBS"
Code: [Select]
@echo off
Mode con cols=90 lines=5 & color 9E
set "Ext=BAT"
set "Pattern=*.%Ext%"
Set "Source=C:\"
Set "Destination=E:\Backup_Copy_%Ext%"
Title Searching for "%Pattern%" files paths and copy them to "%Destination%" by Hackoo 2017
set "LogSearch=%~dp0%~n0_SearchLog_%Ext%.txt"
Set "CopyLog=%~dp0%~n0_CopyLog_%Ext%.txt"
If exist "%CopyLog%" Del "%CopyLog%"
If exist "%LogSearch%" Del "%LogSearch%"
Cls
echo(
echo       --------------------------------------------------------------------------
echo              Please Wait a while ....... Searching is in progress .........
echo       --------------------------------------------------------------------------
   Where /R %Source% "%Pattern%" /F >>"%LogSearch%" 2>&1
If "%ErrorLevel%" EQU "1" (
Cls
echo(
echo       --------------------------------------------------------------------------
echo                         No file(s^) found with this Pattern
echo       --------------------------------------------------------------------------
)

If "%ErrorLevel%" EQU "0" (
If Not exist "%Destination%" MD "%Destination%"
Setlocal enableDelayedExpansion
@for /f "delims=" %%a in ('Type "%LogSearch%"') do (
Call :Incremental_Copy "%%~a" "%Destination%" "%CopyLog%"
)
)
Start "" "%LogSearch%"
Start "" "%CopyLog%"
If exist "%Destination%" Explorer "%Destination%"
exit
::**************************************************************************
:Incremental_Copy <Source> <Destination> <CopyLog>
set "Source=%~1"
set "Destination=%~2"
set "Filename=%~n1"
Set "CopyLog=%~3"
If Not Exist "%Destination%\%~nx1" (
Cls
echo(
echo       --------------------------------------------------------------------------
echo               Copying to "%Destination%\%~nx1" . . .
echo       --------------------------------------------------------------------------
(
@echo Copying "%Source%" "%Destination%\%~nx1"
@Copy /DVN "%Source%" "%Destination%\%~nx1"
) >> "%CopyLog%" 2>&1
) else (
If Exist "%Destination%\%Filename%(%Count%)%~x1" Set /a Count+=1 && goto Incremental_Copy
Cls
echo(
echo       --------------------------------------------------------------------------
echo               Copying to "%Destination%\%Filename%(%Count%)%~x1" . . .
echo       --------------------------------------------------------------------------
(
@echo Copying "%Source%" "%Destination%\%Filename%(%Count%)%~x1"
@copy /DVN "%Source%" "%Destination%\%Filename%(%Count%)%~x1"
)>> "%CopyLog%" 2>&1
Set /a "Count=1"
)
exit /b
::**************************************************************************
NB : Just change the Ext and and the drive of your destination variables :P

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #8 on: August 08, 2017, 09:20:04 AM »
Hackoo, Thanks.  :)

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #9 on: August 08, 2017, 12:54:00 PM »
Hackoo, Thanks.  :)
@Geek-9pm You are welcome  ;)
Don't forget the link to thank me  ;D
@+
Hackoo !

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #10 on: August 08, 2017, 04:41:17 PM »
Anyone who begs for thanx doesn't deserve them.
" Anyone who goes to a psychiatrist should have his head examined. "

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #11 on: August 08, 2017, 05:08:32 PM »
Beggars can't be choosers.  ;D
Quote
...widely held medieval opinion that if you asked for and received a gift you should be grateful for it. The 'gift horse' proverb was recorded first, in Heywood's 1546 version of A dialogue conteinyng the nomber in effect of all the prouerbes in the Englishe tongue.

'Beggars should not be choosers' didn't appear until the 1562 version of 'Proverbs'.

    Beggers should be no choosers, but yet they will:
    Who can bryng a begger from choyse to begge still?
http://www.phrases.org.uk/meanings/beggars-cant-be-choosers.html

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #12 on: August 08, 2017, 09:06:22 PM »
Anyone who begs for thanx doesn't deserve them.

And did you think this really in my case ? i spend my time to write a special code over the night to help him to achieve his aim, and you post me something that discourage me
Thank you  for your nice reply and for your encouragement

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #13 on: August 09, 2017, 10:18:18 AM »
You've been thanked for contributions...i was stating asking is a bit of a reach.

But if thats what's important...go for it.
" Anyone who goes to a psychiatrist should have his head examined. "

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Copy files toa folder, add suffix to prevent overwrite.
« Reply #14 on: August 09, 2017, 10:39:42 AM »
I did thank.