Page 1 of 1

File versioning

Posted: 01 Jun 2017 05:02
by uday
Hello Everyone,

I am very new to dos scripting.I have a requirement to keep the files with version like following in destination folder.When ever the file modified and saved in source folder to be copied over-to destination folder with incremental of version file name. If file A gets updated three times in source folder then destination folder should have the files like FileA-1.rpd,FileA-2.rpd and FileA-3.rpd.


Source folder Destination folder
FileA.rpd FileA-1.rpd
FileB.rpd FileB-1.rpd
Filec.rpd Filec-1.rpd

whenever they
updated FileA-2.rpd
FileB-2.rpd
Filec-2.rpd



..etc

Thanks in advance

Re: File versioning

Posted: 05 Jun 2017 07:18
by pieh-ejdsch
Hello ,
A versioning over an ongoing numbering is only a good idea, if I can separate this numbering exactly.
In the end, this only corresponds to one ID.
I can only offer you a version with timestamp.
like this
With this information it is understandable at which time this version was created.
When sorting by filename, the most recent (last edited) version is either up or down.
Not like an SVN but similar.
You have to adjust the variables accordingly.

Code: Select all

@echo off
setlocal
set prompt=$g$s
set       "folderOriginal=d:\WorkingDir"
set        "filesOriginal=*"
set "folderBackupVersions=d:\WorkingDirBackup.(Versions)"
set         "nameVersions=.(v-timeStamp)"
set      "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"
call :TAB
set "timeStamp=."
set "rCopyCmd= /njh /ts /fp /ns /nc /np /ndl /njs "
for %%F in ("%folderOriginal%\*"
)do (
  set "timeStampFileName="
  set "toVersion="
  for /f "tokens=2,3delims=%TAB%" %%A in ('
    ( robocopy /L "%folderBackupVersions%" ".. versions Listing only..\\" ^
       "%%~nF%nameVersions:timeStamp=*%%%~xF" %rCopyCmd% ^& ^
      robocopy /L "%%~dpF\" ".. original List only ..\\" "%%~nxF" %rCopyCmd% ^)^|sort
  ')do (
    set "timeStampFileName=%%A*%%~dpB"
    if /i "%%~dpB"=="%folderBackupVersions%\" ( set "toVersion="
    )else set "toVersion=1"
  )
  if defined toVersion call :getCurrent.timestamp
  if defined timeStampFileName (
    setlocal enabledelayedexpansion
    for /f "tokens=1-3delims=*" %%S in ("%nameVersions:timeStamp=!timeStamp!%*!timeStampFileName!"
    )do (
      endlocal
      if NOT defined toVersion ( echo  %%T  %%~nxF  - No Backup necessary.
      )else ( robocopy "%%~dpF\" "%folderBackupVersions%" "%%~nxF" %rCopyCmd%
        ren "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
        >>"%fileLogVersions%" ( if NOT errorlevel 1 (
            echo %%S -^> %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
          )else echo ERROR -^>  %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
        )
      )
    )
  )
)
pause
exit /b

:TAB
for /f "delims= " %%T in ('robocopy /L . . /njh /njs') do set "TAB=%%T"
 rem END TAB
exit /b

:getCurrent.timestamp
 rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L  /njh "\|" .^|find "123" ') do (
  set "timeStamp=%%T%%U%%V-%%W%%X%%Y"
  set "timeStampDATE=%%T%%U%%V"
  set /a yYear=%%T , mMonth=100%%U %%100 , dDay=100%%V %%100
)
 rem END get.currentTimestamp
exit /b

Phil

Re: File versioning

Posted: 14 Jul 2017 11:50
by pieh-ejdsch
Hello,
Since the previous versioning system was not quite as wanted - as I (I have actually tried times with my data), I have looked over again and a little around it. The result is not only a prototype, but an improved helpful tool.

Have fun with it.

Code: Select all

@echo off
setlocal
set prompt=$g$s

:: This is a versioning system
:: Transfer of none or one or more parameters (folders / files)
:: A folder is created on the same level as the original folder.
:: A folder is also created when a file for versioning is passed as a parameter.
:: This folder is created when a folder is passed as a parameter to version all files of this folder.
:: Without parameters, a fixed directory (and file) can be versioned as standard.
:: A log file is maintained in the versioning folder.
:: Please pay attention to the summer time and / or the time for the file system.
:: The variable rCopyCMD is used to pass other Robocopy options.
:: The versioned file gets the current time stamp as a version feature.

set       "folderOriginal=d:\WorkingDir"

::::::::::::::::::::::::::::::::::::::::::::::
set        "filesOriginal=*"
set        "folderVersions=.Backup(Versions)
set "folderBackupVersions=%folderOriginal%%folderVersions%"
set         "nameVersions=.(v-timeStamp)"
set      "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"

:getAllParameters
if :%1 equ : goto :EndParameter
if exist %1\ (
  set       "FolderOriginal=%~1"
  set "folderBackupVersions=%~1%folderVersions%"
  set        "filesOriginal=*"
) else (
  set       "FolderOriginal=%~dp1"
  for %%i in ("%~dp1\.") do set "folderBackupVersions=%%~fi%folderVersions%"
  set        "filesOriginal=%~nx1"
)
set "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"

:EndParameter

call :TAB
set "timeStamp=."

set "rCopyCmd= /njh /ts /fp /ns /nc /np /ndl /njs "

for %%F in ("%folderOriginal%\%filesOriginal%"
) do (
  set "timeStampFileName="
  set "versionTimeStamp="
  for /f "tokens=2,3delims=%TAB%" %%A in ('
    robocopy /L "%folderBackupVersions%" ".. versions Listing only..\\" ^
     "%%~nF%nameVersions:timeStamp=*%%%~xF" %rCopyCmd% ^|sort ^& ^
    robocopy /L "%%~dpF\" ".. original List only ..\\" "%%~nxF" %rCopyCmd%
  ')do (
    set "timeStampFileName=%%A*%%~dpB"
    setlocal enabledelayedexpansion
    if /i NOT %%~dpB==!folderBackupVersions!\ if %%A gtr !versionTimeStamp! (
      call :getCurrent.timestamp
      for /f "tokens=1-3delims=*" %%S in ("%nameVersions:timeStamp=!timeStamp!%*!timeStampFileName!"
      ) do (
        endlocal
        robocopy "%%~dpF\" "%folderBackupVersions%" "%%~nxF" %rCopyCmd%
        ren "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
        >>"%fileLogVersions%" ( if NOT errorlevel 1 (
            echo %%S -^> %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
          ) else echo ERROR -^>  %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
        )
      )
    ) else endlocal &echo  %%A  %%~nxF  - No Backup necessary.
    if .==.!! endlocal
    set "versionTimeStamp=%%A"
  )
)
if NOT :%2==: shift & goto :getAllParameters
pause
exit /b

:TAB
for /f "delims= " %%T in ('robocopy /L . . /njh /njs') do set "TAB=%%T"
 rem END TAB
exit /b

:getCurrent.timestamp
 rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L  /njh "\|" .^|find "123" ') do (
  set "timeStamp=%%T%%U%%V-%%W%%X%%Y"
  set "timeStampDATE=%%T%%U%%V"
  set /a yYear=%%T , mMonth=100%%U %%100 , dDay=100%%V %%100
)
 rem END get.currentTimestamp
exit /b


Phil