Batch file to copy files from one folder to another folder

gaanalola

New member
Local time
9:06 AM
Messages
5
Hi,

Sorry to create a new thread if it is already exists, If my requirement has an answer already in forum please help me to find as i couldn't find yet.

I have network storage drive in which my onsite team will place files regularly, We need to move those files from one folder to another folder. for this we are using a bat file now. The bat file has below code,
Code:
Move C:\Media\All_Files\All_Completed\*.*  D:\Pri_Files\Desk2\
The problem with this code is,

  • We need to run it manually for every time.
  • It is copying every file from source folder to destination folder
  • It is creating duplicate files in destination folder
  • need to check manually for new files every time
Can anyone suggest me a good way to run this job automatically and copies only new files from that source file to destination.

It would be great that we can make the script to run automatically for every 5 or 10 minutes to check for new files in source folder.

The source folder has also sub folders sometimes, but we just need to copy only files(PDF) from that folder.

If there are any ongoing copying files, the job should run for completed files or after copying completed.

Please help me to solve this, Thanks in advance.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Microsoft Windows 7 Ultimate 64-bit 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i7-3770S CPU @ 3.10GHz
Motherboard
Intel Corporation DH77KC
Memory
16.00 GB
Graphics Card(s)
NVIDIA NVS 300
Sound Card
(1) NVIDIA High Definition Audio (2) NVIDIA High Definitio
Monitor(s) Displays
1 Dell Monitor
Screen Resolution
1920 x 1080 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
ST2000DM001-1CH164 ATA Device
Keyboard
Microsoft
Mouse
Dell
Internet Speed
20 Mbps
Antivirus
McAfee
Browser
Chrome
Robocopy whichh is buit into windows jsuttype robocopy at a cmd promt has more options is fast only copies changed built for servers you can get a free gui to make life simpler as it writes the code for you Utility Spotlight: Robocopy GUI.

You could write the code then put in a wait 10mins then start again
 

My Computer

Computer type
PC/Desktop
OS
win 8 32 bit
Use a robocpy .bat file in a scheduled task.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Golden Mk. I.4
OS
Windows 10 Pro x64 ; Xubuntu x64
CPU
Intel i7 860 @ 2.80 GHz O/C'ed to 4.0GHz
Motherboard
Gigabyte P55A-UD3R Rev.1. Award BIOS F13
Memory
16GB Corsair Vengance DDR3 @ 661 MHz Dual Channel (9-9-9-24)
Graphics Card(s)
EVGA NVidia GTX 560 1024MB
Sound Card
Realtek Integrated
Monitor(s) Displays
Dual Samsung SyncMaster 2494HS
Screen Resolution
1920*1080 and 1920*1080
Hard Drives
1*Samsung 840 EVO 120GB SSD;
1*OCZ Vertex 2 60GB SSD;
2*Samsung F3 SpinPoint 1TB in RAID0;
1*Samsung F1 SpinPoint 1TB;
2*Western Digital 1TB External USB 3.0
1*Western Digital 500GB External USB 3.0
1*Seagate 500GB External USB 2.0
PSU
Thermaltake ToughPower QFan 750W
Case
Thermaltake Element S VK60001W2Z
Cooling
Corsair H60 Water Cooling, 2*230mm and 2*80mm case fans
Keyboard
Logitech G110
Mouse
Logitech MX518
Hi Golden,

Before posting this thread, I came to this forum because of your tutorial on RoboCopy. If you don't mind can you please provide code for my requirement? Thanks in advance.

Use a robocpy .bat file in a scheduled task.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Microsoft Windows 7 Ultimate 64-bit 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i7-3770S CPU @ 3.10GHz
Motherboard
Intel Corporation DH77KC
Memory
16.00 GB
Graphics Card(s)
NVIDIA NVS 300
Sound Card
(1) NVIDIA High Definition Audio (2) NVIDIA High Definitio
Monitor(s) Displays
1 Dell Monitor
Screen Resolution
1920 x 1080 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
ST2000DM001-1CH164 ATA Device
Keyboard
Microsoft
Mouse
Dell
Internet Speed
20 Mbps
Antivirus
McAfee
Browser
Chrome
robocopy C:\Media\All_Files\All_Completed\ D:\Pri_Files\Desk2\ /MIR /FFT /Z /XA:H /W:5

Explanation of the switches used:

  • /MIR specifies that Robocopy should mirror the source directory and the destination directory. Note that this will delete files at the destination if they were deleted at the source.
  • /FFT uses fat file timing instead of NTFS. This means the granularity is a bit less precise. For across-network share operations this seems to be much more reliable - just don't rely on the file timings to be completely precise to the second.
  • /Z ensures Robocopy can resume the transfer of a large file in mid-file instead of restarting.
  • /XA:H makes Robocopy ignore hidden files, usually these will be system files that we're not interested in.
  • /W:5 reduces the wait time between failures to 5 seconds instead of the 30 second default.


A lot simpler and foolproof way is to install google drive or onedrive save file to the folder login on both pc to the drive with same name as you save a file it will apear on th eother without you doing anything same if you change a file
 

My Computer

Computer type
PC/Desktop
OS
win 8 32 bit
If I installed a new program every time I wanted to do something, I'd have to buy a larger hard drive just to hole them all.

I do pretty much what the OP wants to do, using what's already built into Windows (all versions).

XCOPY is a very powerful copy command and it's been around since DOS days. I used it then and I use it now. I have 12 computers and xcopy is on every one of them, so it's there any time I need it to write a quick batch file or to do a One-Time copy.

I do all my data backups to flash drives, external 1TB hard drive, etc., using a simple batch file and XCOPY.

For instance, to backup everything in "My Documents" folder to a like folder on my External Hard drive:

XCOPY /D /S /C /I /H /R /K /Y /J "C:\Documents and Settings\Joe\Documents\*.*" "E:\Documents\My Documents"

Xcopy is one of the most versatile commands in the DOS library. Getting to know it and how to use it
can save you a lot of time and energy, now and in the future. Using the various switches after the command, allows you to customize the copy operation to do exactly what you want it to do.

You can copy the following and paste it into Wordpad and print it out for future use:

Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
[/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B]
[/EXCLUDE:file1[+file2][+file3]...]

source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set,
doesn't change the attribute.
/M Copies only files with the archive attribute set,
turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/V Verifies the size of each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/G Allows the copying of encrypted files to destination that does
not support encryption.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attributes.
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/B Copies the Symbolic Link itself versus the target of the link.
/J Copies using unbuffered I/O. Recommended for very large files.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

Cheers Mate!
TechnoMage :cool:



 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Various
OS
Win 7 Pro, SP1, x86, Win-11/Pro/64
CPU
AMD
Motherboard
Various
Memory
8GB Crucial
Graphics Card(s)
Various
Sound Card
OnBoard
Monitor(s) Displays
Acer 21.5"
Hard Drives
Crucial SSD, 500 GB
PSU
OEM
Case
SFF Slim Line Case
Cooling
OEM
Keyboard
eMachines
Mouse
Logitech Wireless
Internet Speed
varies
Antivirus
Windows Defender/Super Anti-Spyware
Browser
Firefox
To use XCOPY for your situation, include the following commands in your batch file:

XCOPY /S /M C:\Media\All_Files\All_Completed\*.* D:\Pri_Files\Desk2
ECHO Hit CTRL-C to stop copying the files.
sleep 300 > nul
goto start

-or-

XCOPY /S /M C:\Media\All_Files\All_Completed\*.* D:\Pri_Files\Desk2
ECHO Hit CTRL-C to stop copying the files.
timeout /t 300 > nul
goto start

Explanation:
sleep 300 > nul causes a pause of 300 seconds, or 5 minutes. The "nul" prevents anything from being echoed to the screen. You could use "timeout /t 300" rather than "sleep 300". (Sometimes one or the other won't work on your system; pick the one that works on your system.)

goto start makes the batch file start over and run again. If you want to copy the files regularly, say every five minutes, use "sleep 300" or "timeout /t 300" followed by "goto start". It will run every five minutes if you do that.

The ECHO command simply lets you know that you can break out of the batch file by hitting CTRL-C. That's all it does; it has no effect on the actual copy process. You can omit this command if you want to.

/S causes XCOPY to copy all subdirectories as well as the main directory.

/M causes XCOPY to copy all files which have been changed since the last copy. To do this, it checks the archive bit on each file. If the archive bit is turned on, it will copy the file then turn the archive bit off, effectively marking that file as copied. That file won't be copied again unless it is changed again, which will turn on the archive bit, effectively marking that file as needing to be copied.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell
OS
Linux Mint 18.2 xfce 64-bit (VMWare host) / Windows 8.1 Pro 32-bit (VMWare guest)
CPU
Haswell
Memory
4 GB
Monitor(s) Displays
Acer 23"
Screen Resolution
1920 x 1080
Hard Drives
Two hard drives, 1TB each: One for Linux, one for my data.
Keyboard
IBM Model M
Antivirus
Sophos (Linux), Trend Micro (Windows)
Browser
Firefox, Opera
Other Info
I use Samba to share my data drive with the other computers at my house and with my guest session in VMWare Workstation Player.
If I installed a new program every time I wanted to do something, I'd have to buy a larger hard drive just to hole them all.

I do pretty much what the OP wants to do, using what's already built into Windows (all versions).
Fun fact - robocopy has been built into Windows staring with Vista. Being only a few kb in size, it hardly requires the purchase of a large hard drive. It is a far superior copying method than xcopy (it was designed to replace xcopy in fact) since it has the ability to overcome network interrupts and re-copy files, amongst a host of other things.

In his initial post, the OP made it clear he was copying across a network, so the safest option in this case would be robocopy.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Golden Mk. I.4
OS
Windows 10 Pro x64 ; Xubuntu x64
CPU
Intel i7 860 @ 2.80 GHz O/C'ed to 4.0GHz
Motherboard
Gigabyte P55A-UD3R Rev.1. Award BIOS F13
Memory
16GB Corsair Vengance DDR3 @ 661 MHz Dual Channel (9-9-9-24)
Graphics Card(s)
EVGA NVidia GTX 560 1024MB
Sound Card
Realtek Integrated
Monitor(s) Displays
Dual Samsung SyncMaster 2494HS
Screen Resolution
1920*1080 and 1920*1080
Hard Drives
1*Samsung 840 EVO 120GB SSD;
1*OCZ Vertex 2 60GB SSD;
2*Samsung F3 SpinPoint 1TB in RAID0;
1*Samsung F1 SpinPoint 1TB;
2*Western Digital 1TB External USB 3.0
1*Western Digital 500GB External USB 3.0
1*Seagate 500GB External USB 2.0
PSU
Thermaltake ToughPower QFan 750W
Case
Thermaltake Element S VK60001W2Z
Cooling
Corsair H60 Water Cooling, 2*230mm and 2*80mm case fans
Keyboard
Logitech G110
Mouse
Logitech MX518
Thanks bro, however, the download link is not working now. Can you please check it?

Robocopy whichh is buit into windows jsuttype robocopy at a cmd promt has more options is fast only copies changed built for servers you can get a free gui to make life simpler as it writes the code for you Utility Spotlight: Robocopy GUI.

You could write the code then put in a wait 10mins then start again
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Microsoft Windows 7 Ultimate 64-bit 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i7-3770S CPU @ 3.10GHz
Motherboard
Intel Corporation DH77KC
Memory
16.00 GB
Graphics Card(s)
NVIDIA NVS 300
Sound Card
(1) NVIDIA High Definition Audio (2) NVIDIA High Definitio
Monitor(s) Displays
1 Dell Monitor
Screen Resolution
1920 x 1080 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
ST2000DM001-1CH164 ATA Device
Keyboard
Microsoft
Mouse
Dell
Internet Speed
20 Mbps
Antivirus
McAfee
Browser
Chrome
Thank you for trying to help me on this, But below code may not suitable for my requirement.

My Requirement is,

  • I just need to copy only files(PDF) from that directory, Source folder has also sub folders. so don't want to touch those Folders data.
  • The copy should done for new files only that are placed recently, to eliminate duplicate files in destination folder.
  • this should be run for every 5 or 10 min to check for new files.
Thanks in advance.:)

robocopy C:\Media\All_Files\All_Completed\ D:\Pri_Files\Desk2\ /MIR /FFT /Z /XA:H /W:5

Explanation of the switches used:

  • /MIR specifies that Robocopy should mirror the source directory and the destination directory. Note that this will delete files at the destination if they were deleted at the source.
  • /FFT uses fat file timing instead of NTFS. This means the granularity is a bit less precise. For across-network share operations this seems to be much more reliable - just don't rely on the file timings to be completely precise to the second.
  • /Z ensures Robocopy can resume the transfer of a large file in mid-file instead of restarting.
  • /XA:H makes Robocopy ignore hidden files, usually these will be system files that we're not interested in.
  • /W:5 reduces the wait time between failures to 5 seconds instead of the 30 second default.


A lot simpler and foolproof way is to install google drive or onedrive save file to the folder login on both pc to the drive with same name as you save a file it will apear on th eother without you doing anything same if you change a file
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Microsoft Windows 7 Ultimate 64-bit 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i7-3770S CPU @ 3.10GHz
Motherboard
Intel Corporation DH77KC
Memory
16.00 GB
Graphics Card(s)
NVIDIA NVS 300
Sound Card
(1) NVIDIA High Definition Audio (2) NVIDIA High Definitio
Monitor(s) Displays
1 Dell Monitor
Screen Resolution
1920 x 1080 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
ST2000DM001-1CH164 ATA Device
Keyboard
Microsoft
Mouse
Dell
Internet Speed
20 Mbps
Antivirus
McAfee
Browser
Chrome
The link to the GUI work correctly it's at the top utility spotlight
 

My Computer

Computer type
PC/Desktop
OS
win 8 32 bit
Back
Top