I am trying to use XCopy to backup files from an old machine to a server folder. So I set up a batch file which copies each day the new files from the machine to the backup server using the following code, (just a part of the whole code):
set /p LastSuccessfulBackup=< C:/backup/LastSuccessfulBackup.txt
set datestr=%date:~-4,4%%date:~3,2%%date:~-10,2%
xcopy "\\machine\srcfolder" "\\server\destfold\"!datestr! /s /y /i /c /d:!LastSuccessfulBackup!
So the idea is to check whether there are changed files in the srcfolder using /d and copy them to a newly generated timestamp-folder if there are changed files. It's kind of an incremental backup.
My problem is now, that I would like to have the whole folder copied and not only the changed files. Otherwise I get day by day just a backup of some few files but never the whole src-folder content.
That means that I need kind of a check, whether there are changed files and if so, copy the whole srcfolder to the backup-destination timestamp-folder.
Related
I'm making a system that our employees need to store files to that are ment to be processed. This process however deletes the source files and I want to make a backup prior to these files being processed.
The files need to be stored in an automatically created folder with name being today's date. The files vary in filetype.
The first part, creating a folder with today's date, I managed to figure out. The second part, copying files from a general folder to this newly created folder is something I can't get to work.
This is the code I have so far
#echo off
set date="%date:~-10,2%-%date:~-7,2%-%date:~-4,4%"
set "source=C:\b2g\2021\general"
set "destination=C:\b2g\2021\%date%"
mkdir "C:\b2g\2021\%date%"
robocopy "%source%" "%destination%" /mir /minage:%X%
exit /b
This is creating the folder, but is not copying any files to that folder.
any help is very kindly apreciated
Everyday at 1 am some zip folders will come in on one server(let us say A). I need to copy the files to another server(let us say B). After copying the files to server B, then after some time the copied files will moves to another destination. For the next day the previously copied files should not come with the files from today.
Now, the problem is, it is working fine in my Local PC .. It is not working in the server.
I don't have knowledge of batch file programming. Please let me know if there is a way to accomplish this.
My code:
#echo off
forfiles /s /p "\\10.35.0.44\Da\Ra\Folder" /m "*.zip" /d +"02/04/2019" /c "cmd /c move #path \\10.35.0.44\Da\sai"
pause
It is the throwing following error:
Error:UNC paths <\\machine\share> are not supported
Because of this I've used pushD()popD. The error was not there but the files are not being copied.
New to batch scripting..
I want to copy files from one folder(A) to another folder(B) continuously. The other software "moves" files from folder B. My script with Xcopy is continuously copying files from A to B. But When the copied files are moved from B, script is copying again the same files to B. Script should copy files from A to B only once.
You could have shown us the script and made things easier.
From the prompt, execute
xcopy /?
to show xcopy options.
I'd suggest the /d option would do what you need.
Every file has an archive attribute. This attribute was created to determine when a file needs to be copied (well, more or less). By default, files are generated with this attribute set and any change to the file will set it again.
xcopy includes two switches: /a and /m that handle this attribute.
/a tells xcopy to only copy files that have the archive attribute set
/m tells xcopy to only copy files that have the archive attribute set and clear the attribute
You should try something like xcopy /m "c:\sourceA\*" "c:\targetB" This will copy files with the archive attribute set and remove the attribute from the archive.
Good day. I am trying to write my first batch file. I finally got it do to the most basic of tasks, and have read much here that helped including tutorials. I have not yet found an article to help me put what I'm looking for together. My search-fu is weak.
I want my batch file to copy all files every 30 minutes. However, if it comes to a file that has been updated, I want it to copy but not replace the existing in the destination. I'm assuming it would just add the (1) or 'copy' to the file name. I'm not sure if I used the proper switches or need to change them based on what I'm looking for. I think they're good, but I digress.
Here is the batch that I created without the logic:
REM Backup files 30 minutes
#echo off
:start
XCOPY "C:\source directory" "C:\target directory" /d /c /s /r /y /i
TIMEOUT /t 1800 /nobreak
goto start
Normal behaviour is to overwrite the file, or to fail (depending on the action). The (1) or (copy) postfixes are special features of Windows Explorer (the shell). If you want similar behaviour in your script, you'll have to implement it yourself.
The easiest thing (also to prevent a lot of (1), (2) .. (N) files, is to create separate folders. You can create a folder with a timestamp and copy all modified files to it.
To detect which files are modified, you might use the Archive flag of the files. When a file is modified, its Archive flag is set. XCopy has the possibility to copy only those files which have the flag set. Actually the main purpose of this flag is to determine modified files (or at least, files you want to archive).
So my suggestion:
Create a folder with a timestamp in the name. You may want to use the answer to this question for that.
Use XCopy with the /M parameter. /M copies only files with the Archive attribute, and clears the attribute afterwards.
Try to delete the directory using rd or rmdir, but without the /S parameter. It will fail if it contains files, but this way you will prevent a lot of empty directories.
Before implementing this, make sure that the process which modifies the files, also sets the Archive attribute again. It should do that automatically, but it can't hurt to test this.
-edit- Per request an example:
REM Backup files 30 minutes
#echo off
:start
REM generate a timestamp value that can be used as a file name.
setlocal TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
REM Not needed, I think: Creating the directory yourself.
REM md "C:\target directory\%TIMESTAMP%"
REM /S for recursive. /I should create target directory? /M for backup mode.
XCOPY "C:\source directory" "C:\target directory\%TIMESTAMP%" /M /S /I
REM Not needed, I think: Removing the directory (if it's empty).
REM rd "C:\target directory\%TIMESTAMP%"
TIMEOUT /t 1800 /nobreak
goto start
I think /I already solves the other issues. It assumes that target is a directory if it doesn't exist. That makes me assume that it will in fact create that directory if it doesn't exist, and probably only if there is anything to copy. If that is right, you also won't have to remove the directory if it's empty.
Considering cleanup: Remember that this method (using /M) only copies files that have changed. If you cleanup old directories, you should make sure to copy the entire folder first. The timestamped folders will contain only the modified files (incremental backup), so if you clean up old ones, your backup/copy won't be complete anymore!
Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.