Computer Hope
Microsoft => Microsoft DOS => Topic started by: Geek-9pm 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? ::)
-
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.
-
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 (https://stackoverflow.com/questions/28697436/how-to-copy-and-increment-multiple-instances-of-a-file-using-batch-file?answertab=active#tab-top)
-
Thank you both. That was helpful.
-
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:
(https://i.imgbox.com/mvP9RsjA.jpg)
Check both boxes:
(https://i.imgbox.com/OSvySr3K.jpg)
Newer file is renamed:
(https://i.imgbox.com/xYTesaiJ.jpg)
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.
-
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)
-
Hi ;)
I made for this batch script to copy with incrementaion
Copy_TXT_Files.bat
@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
::**************************************************************************
-
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"
@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
-
Hackoo, Thanks. :)
-
Hackoo, Thanks. :)
@Geek-9pm You are welcome ;)
Don't forget the link to thank me ;D
@+
Hackoo !
-
Anyone who begs for thanx doesn't deserve them.
-
Beggars can't be choosers. ;D
...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
-
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
-
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.
-
I did thank.
-
I did thank.
Sometimes I wish we could "unthank" but that might lead to the kind of mischief which spoils a forum.