Page 1 of 1

Adding different version of same file

Posted: 29 Jun 2016 07:51
by peterb
Problem:

I have a file that copies files from one source drive/directory to a target drive/directory.

I would like, however, for the batch commands to copy the same files but, instead of replacing in the target drive/directory, to add the files with a number, so I can go back and retrieve changed information from previous files, if necessary. E.g. - I copy textfile.txt, then when I copy the file again, instead of replacing it, it leaves the original file in place, and copies the newer file with a _01 added, etc.

I am a neophyte at this, by the way.

textfile.txt
textfile_01.txt
textfile_02.txt

Many thanks in advance
Peter

Re: Adding different version of same file

Posted: 29 Jun 2016 13:01
by skaa
Something like this:

Code: Select all

@echo BEGIN-------------------------------------------------------------------------
@setlocal

@set snpSource=c:\business\Batch\Exp\expExp\From
@set snpTarget=c:\business\Batch\Exp\expExp\To

@for %%a in ("%snpSource%\*.*") do @call :fMove "%%a"
goto fMove_Ex
:fMove
@setlocal
set jf=0
set snp=%~1
set snfn=%~n1
set snfe=%~x1
:fMove_FRp
set snft=%snfn%%snfe%
set sjf=%jf%
if %jf% lss 10 set sjf=0%jf%
if %jf% gtr 0 set snft=%snfn%_%sjf%%snfe%
set snpt=%snpTarget%\%snft%
if not exist "%snpt%" goto fMove_FRpEx
set /a jf=%jf%+1
if %jf% geq 100 goto fMove_FRpExErr
goto fMove_FRp
:fMove_FRpExErr
echo ERROR for %snfn%%snfe%
:fMove_FRpEx
copy "%snp%" "%snpTarget%\%snft%"
:fMove_FRpErr
@endlocal
exit /b
:fMove_Ex

:mEx
@endlocal
@echo ---------------------------------------------------------------------------END

Re: Adding different version of same file

Posted: 29 Jun 2016 20:25
by foxidrive
peterb wrote:I have a file that copies files from one source drive/directory to a target drive/directory.

I would like [...] to add the files with a number, so I can go back and retrieve changed information from previous files, if necessary


This works to copy each set of files with a number that is +1 from the last highest number.
If you expect more than 99 sets of files then change the ~-2 to a ~-3 so that the numbers become a three digit number.


Code: Select all

@echo off
set "target=d:\target\folder"
set "source=c:\source\folder"

md "%target%" 2>nul
dir "%target%" /b /a-d>"%temp%\file-number-check.txt" 2>nul
set number=0

:loop
set /a number+=1
set "filenumber=0000%number%"
set "filenumber=%filenumber:~-2%"
find "_%filenumber%." <"%temp%\file-number-check.txt" >nul && goto :loop

for %%a in ("%source%\*.*")  do (
     echo copying "%%~nxa" to "%target%\%%~na_%filenumber%%%~xa"
     copy /b "%%a"  "%target%\%%~na_%filenumber%%%~xa" >nul
     )

del "%temp%\file-number-check.txt" 2>nul
pause