Automatically copy files without overwriting, but create numbered files instead

I need to copy files at regular intervals, for example, once an hour, so I tried to configure the xcopy package so that it would copy the files that it needed to copy to another folder. Now that he is copying, he is overwriting files that are not what he should do.

When the file is copied, it should create a new file instead with a name like File.txt, file-COPY1.txt, file-COPY2.txt, or something like that.

How to do it?

Thanks in advance.

+4
source share
4 answers

You can create separate files for each run by simply adding a timestamp to the file name. Something like that:

XCOPY "File.txt" "[TargetDir]\File1_%time:~0,2%_%time:~3,2%_%time:~6,2%.txt" 

This resolves the file name, which reads as File1_11_30_05.txt , given that the copy operation occurs at 11:30:05. The %time:~0,2% extracts 2 digits from the time string stored in the %time% variable.

In addition, you can also add a date in the same way. You can use the %date% variable for this purpose.

If you really need a template, for example File-COPY1.txt , File-COPY2.txt , etc. It takes a bit more work. Let us know if the timestamp approach is not enough.

+2
source

This batch accepts the name of two directories, relative or explicit, and copies all the files in the first directory to the second directory, but adds a - # between the file name and the extension (where # is the number of copies of the file in the second directory). Therefore, if only one file ( MISC.txt ) was copied from dir1 to dir2, but it was copied 4 times, dir2 will contain 4 files: ( MISC-1.txt MISC-2.txt MISC-3.txt and MISC-4.txt )

To use zcopy.bat , call it like this: zcopy fromDir toDir

zcopy.bat

 @echo off if "%1"=="" goto :eof if "%1"=="/?" type %~dpnx0 & goto :eof if "%2"=="" goto :eof if not exist %2 md %2 setlocal enableDelayedExpansion for %%x in (%1\*.*) do ( set this= set count=1 if "%%~xx"=="" ( set backstop=. ) else ( set backstop= ) for /f %%y in ('dir /b /a:-d /o:n "%2\%%~nx-*%%~xx!backstop!" 2^>nul') do ( set this=%%~nxy ) if not "!this!"=="" ( set count=!this:%%~nx-=! if "%%~xx"=="" ( set /a count=!count!+1 ) else ( set /a count=!count:%%~xx=! + 1 ) ) set target=%%~nx-!count!%%~xx echo copy "%%x" "%2\!target!" copy "%%x" "%2\!target!" > nul 2>&1 ) 

If directory %2 does not exist, the above code will create it.

+1
source

I use this to back up files daily.

  set aa=%date:~4,2%%date:~7,2%%date:~12,2% copy filename.txt c:\backupfolder\filename%aa%.txt 

Hope this helps. Scott ...

+1
source

iterates the file name, that is, a new name is specified each time. you can just add numeric numbers.
use one counter, each time increasing it; incremented counter concatenates with fileName

0
source

Source: https://habr.com/ru/post/1435463/


All Articles