Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file to copy files to another location?

Tags:

batch-file

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?

like image 331
Elliott Avatar asked Feb 03 '10 20:02

Elliott


People also ask

How do I copy and paste in a batch file?

If you want to copy files from one place to another in a batch file, you can have cmd copy and paste the file. Use the command simply called "Copy." Generally, put the word copy on one line, followed by the original file and where you want it copied, such as "copy C:\Example\Example. txt C:\Example2\Example2. txt."

How do you copy a file from a folder to another in CMD?

Highlight the files you want to copy. Press the keyboard shortcut Command + C . Move to the location you want to move the files and press Command + V to copy the files.


4 Answers

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.

like image 173
Marco Demaio Avatar answered Oct 24 '22 13:10

Marco Demaio


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

like image 43
Rubens Farias Avatar answered Oct 24 '22 14:10

Rubens Farias


@echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
like image 42
Mohammed Salem Avatar answered Oct 24 '22 14:10

Mohammed Salem


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.

like image 37
John Knoeller Avatar answered Oct 24 '22 15:10

John Knoeller