hey guys, hope someone can help me. I want to create a script that can copy files from one folder to another. So the source folder will have files that are updated versions of files in the destination folder.
Its pretty simple to just do the robocopy command but what I want to do is a little different. I don't want to override files in the destination folder. What I want to do is to be able to back up the file in the destination folder that is bout to be overwritten to a different folder
So if Destination folder has files 1 though 10 and the Source folder has File 2,3,4. the robocopy would typically take the files 2,3,4 from source and copy them to destination folder replacing 2,3,4 in destination.
I want it to somehow follow this logic. "if the files with same name exist in the destination folder; first backup the files from destination folder to a different folder and only then copy files from source to destination. I also want the source files to be deleted after they are copied to destination
OR maybe there is a different option? something where robocopy will copy file from source folder to destination folder without overwriting the already existing file and after it copies the file there will be a next line of code that will look at the destination folder and move the older file to a different folder?
Please someone help. Thank you
robocopy help please
Moderator: DosItHelp
if statements in batch files
I'm trying to write a batch file using robocopy and if statements.
because I have a lot of files and there is never a specific file name its a little tricky.
a little background on what Im doing so it make a little more sense.
I have a Folder that is located on a shared drive/server, this Folder has files necessary for a certain software. These files are configurable and I or other people in the group can open these files and modify them as needed. Well the thing is I do not want them to save it back to this Folder on the shared driver/server before they are approved.
So what I do is require them to save it to a "Need Approval" folder on their local machine.
I need to make a script that would look at that "Need Approval" folder on each local machine and robocopy files that are in there to "Need Approval" folder on a Shared drive/server
This way I can go to "Need Approval" folder on the shared drive/server and look at files that need to be reviewed and approved. So when I review and approve the files I will move them to "Approved" folder on the Shared drive/server
So, now I need a script to also be able to take files from the "Approved" folder and move them that folder I mentioned in very beginning. That folder that has all the files for the software. This is the main Folder that can not afford to be messed up. which is why I need this whole process.
Here is a catch though. When moving files from "Approved" folder which could be anywhere from 1 file to many I do not want them to just overwrite files in the destination folder (the main important folder)
What I need is an if statement to do something like this.... or maybe some other type of solution
if a file that is about to be copied exists in destination, back it up first to a different folder and then copy.
else if the file that is about to be copied doesn't exist in the destination then go ahead and copy the file
I also need the files to not be replaced with older date files. I want the files to only be copied if there was a change in the files, such as size or date.
because I have a lot of files and there is never a specific file name its a little tricky.
a little background on what Im doing so it make a little more sense.
I have a Folder that is located on a shared drive/server, this Folder has files necessary for a certain software. These files are configurable and I or other people in the group can open these files and modify them as needed. Well the thing is I do not want them to save it back to this Folder on the shared driver/server before they are approved.
So what I do is require them to save it to a "Need Approval" folder on their local machine.
I need to make a script that would look at that "Need Approval" folder on each local machine and robocopy files that are in there to "Need Approval" folder on a Shared drive/server
This way I can go to "Need Approval" folder on the shared drive/server and look at files that need to be reviewed and approved. So when I review and approve the files I will move them to "Approved" folder on the Shared drive/server
So, now I need a script to also be able to take files from the "Approved" folder and move them that folder I mentioned in very beginning. That folder that has all the files for the software. This is the main Folder that can not afford to be messed up. which is why I need this whole process.
Here is a catch though. When moving files from "Approved" folder which could be anywhere from 1 file to many I do not want them to just overwrite files in the destination folder (the main important folder)
What I need is an if statement to do something like this.... or maybe some other type of solution
if a file that is about to be copied exists in destination, back it up first to a different folder and then copy.
else if the file that is about to be copied doesn't exist in the destination then go ahead and copy the file
I also need the files to not be replaced with older date files. I want the files to only be copied if there was a change in the files, such as size or date.
Re: robocopy help please
Damn Millennials...
Please try a few different things and come back with your results.
Please try a few different things and come back with your results.
-
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
Re: robocopy help please
Hi,
I have to admit it sounds pretty undecided what you want exactly now.
I do not want to advertise, but maybe this can help you.
viewtopic.php?t=7903#p52948
I have to admit it sounds pretty undecided what you want exactly now.
I do not want to advertise, but maybe this can help you.
viewtopic.php?t=7903#p52948
-
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
Re: robocopy help please
hello miket11,
I scrubbed something together.
You can use it to transfer your files.
Several backups can be created one behind the other.
No existing data in the original folder will be overwritten - if at least in the first backup this file was not moved.
If you want to move these files correctly, I refer to the MoveintTree.
I scrubbed something together.
You can use it to transfer your files.
Several backups can be created one behind the other.
No existing data in the original folder will be overwritten - if at least in the first backup this file was not moved.
If you want to move these files correctly, I refer to the MoveintTree.
Code: Select all
@echo off
setlocal
set prompt=$g$s
:: pulls from a folder (o) the files into the backup (b) [backup-n]
:: which have been inserted or edited at another folder (i).
:: copies new and edited files to the folder (o)
:: No unsaved files will be overwritten in (o).
rem folder (o)
set "originalFolder=D:\cmd-test\original"
rem folder (i)
set "workingFolder=D:\cmd-test\work"
rem folder (b)
set "backupFolder=%originalFolder%.Backup"
set "file=*.*"
set "recursive=/S"
set "backupSet=2"
::-------------------------------------------------------------------
set "fullpath= /njh /njs /ts /fp /ndl /ns /nc /np "
set "editedFile= /xC /xO /xL /xX "
set "newFile= /xC /xO /xN /xX "
set "copyToCycle="
for /l %%n in (1 1 %backupSet%) do call set copyToCycle=%%copyToCycle%%"%backupFolder%-%%n"
:newBackup
set cycle=0
call :BackupCycle x "%workingFolder%" "%originalFolder%" %copyToCycle%
echo done
ping localhost -n 10 >nul
cls
goto :newBackup
exit /b
:BackupCycle
setlocal
set /a cycle+=1
for /l %%c in (1 1 %cycle%) do shift
if not exist %1 echo %1 not found &exit /b 1
rem atribbutes are removed
if exist %2 attrib -a -i "%~2\%file%" /s
for /f "tokens=1-3delims=*" %%i in (" %~1 ---> *%~2 ---> *%~3") do title %%~nxi %%~nxj
:mark edited files
rem Edited files get attributes in the software folder
robocopy /r:2 /w:1 %recursive% %1 %2 "%file%" %fullpath% %editedFile% /copy:a /A+:an
set /a "noCopy=(%errorlevel% -1) %%2"
if NOT errorlevel 16 if %nocopy% equ 0 ( echo --- found edited files at "%~nx1"
set CopyFiles= / edited
if errorlevel 8 echo ------ some files could not be detected.
set "editedFile=/iA:an"
if "%~4" neq "" call :BackupCycle %*
) else ( echo --- No edited files
set "CopyFiles="
if %cycle% equ 1 (goto :workingFiles) else exit /b
)
:move marked files
robocopy /r:2 /w:1 /mov %recursive% %2 %3 "%file%" %fullpath% /xO /is /iA:an
set /a "noCopy=(%errorlevel% -1) %%2"
if NOT errorlevel 16 if %nocopy% equ 0 ( echo --- files were backed up to "%~nx3"
if errorlevel 8 echo ------ some files could not be detected.
) else echo --- no file saved
if %cycle% neq 1 exit /b
:copy edited files
:workingFiles
robocopy /r:2 /w:1 %recursive% %1 %2 "%file%" %fullpath% %newFile% /A-:an
set /a "noCopy=(%errorlevel% -1) %%2"
if NOT errorlevel 16 if %nocopy% equ 0 ( echo --- new %CopyFiles%files were copied to "%~nx2"
if errorlevel 8 echo ------ some files could not be detected.
) else echo --- no new %CopyFiles%files
exit /b