Windows XP – Batch file script to copy all files

by admin on August 4, 2006 · 38 comments

in News, Scripts, Windows Server, Windows XP

This is a small script I use to copy file quickly from source to destination. This script should work under Windows XP, 2000, 2003, 98/95 etc.

Syntax
Script accept two command line argument as follows:
mycopy.bat {source} {destination}

Where,
mycopy.bat - This script copies files and directories, including subdirectories using Windows xcopy program. You must pass following two arguments/parameters
source – Use to specify the location and names of the file/directory you want to copy.
destination - Use to specify the destination of the files you want to copy.

How do I use this script?

For example copy all *.doc file from C:\DATA to \\SERVER\BACKUP directory, you need to execute script as follows:
Cd c:\data

mycopy.bat *.doc \\SERVER\BACKUP

OR copy all files from c:\data to z:\

mycopy.bat c:\data\** z:\

mycopy.bat source code

@rem BATCH FILE
@echo off
if "%1" == "" goto error1
if "%2" == "" goto error2
@rem * Copy from source to destination including subdirs and hidden
@rem * File
xcopy "%1" "%2" /S /E /H
goto endofprogram
:error1
echo You must provide source
echo Syntax:
echo %0 source destination
goto endofprogram
:error2
echo You must provide destination
echo Syntax:
echo %0 source destination
goto endofprogram
:endofprogram

Where,

  • Line # 03 and 04 make sure we pass the command line argument.
  • Line # 08 - If both command line arguments (source and destination) are provided it will copy files using XCOPY program.

{ 38 comments… read them below or add one }

1 suresh August 23, 2007 at 5:48 pm

Can you tell me the windows script that shows how to tranfer files from one server to the other. I want to schedule a script that picks up all files and folders every 15 mins from one server and puts on to other server.. it would be very helpful if you provide me that.. thank you

Reply

2 Anonymous September 19, 2012 at 10:04 am

Hi Vivek

now i have the one software, it will automatic generate Everyday one Backup Like File Name TM Backup 6.A3 followed by Backup date and time,( TM Backup 6.A3 2012-05-18 16.10-1 )
i want need to copy this file to another network drive location on every day
how to write batch file to find the latest Backup file with Date and time
please check and come back to me.

thanks
Vivekanandan

Reply

3 Roberto June 26, 2008 at 11:56 pm

I am trying to use the script above and I cannot understand where the destination and source network paths go. I will please appreciate if you could reply to my email as soon as possible.

Ex: if I want to copy files from “\\servername1\c:\Books” to “\\servername1\c:\Books” how I do that?

Roberto

Reply

4 mathew August 24, 2009 at 11:29 am

i will be glad if i know how to used it

Reply

5 sathish November 15, 2008 at 12:50 am

hi,
I jst wanted to copy all files from local folder d:\test\ to my network drive(server). i need a simple script to do that. then i can use scheduler to run it. need help
thanks

Reply

6 Anonymous October 28, 2010 at 8:00 am

that would be a ftp command, with a put command

Reply

7 gibit November 19, 2008 at 2:39 pm

Hi vivek,
thanks for the script, i’m using it

only i’d like it to overwrite files without asking, what should i add to the script?

thx
gibit

Reply

8 Anonymous October 28, 2010 at 8:01 am

just do a del command to delete the =files in the destination folder before you copy them over

Reply

9 dinosaur411 February 17, 2009 at 3:20 pm

use the /-Y switch.
or check type xcopy /? for other options

Reply

10 jammerone April 6, 2009 at 1:20 pm

Hello…. can i have your inputs on this…

I like to copy some .tib files from one server to another. both are in the same network but one is joined to the domain. how to can i include the authentication in the script? e.g. copy all .tib files from drive of Server1 (workgroup) to another drive of server2 (joined to the domain)

Thanks

Reply

11 rgoodwin55 May 14, 2009 at 7:14 pm

Thanks for the script. It works great.

Reply

12 Jimson July 13, 2009 at 12:30 pm

Is there any revision for this script so that it will copy only the files that are created and modified within a specified date and time range.

Thanks

Reply

13 Nick August 1, 2009 at 6:08 am

I’m looking for a script which will allow me to plug my flash drive into my laptop and automatically/ instantly copy files from different locations on my laptop into seperate folders on the flash drive.

Reply

14 sruthi August 26, 2009 at 9:26 am

Hi,

I m looking for a dos script to copy all the files from source to dest based on date range. I’m using windows server 2003.

Reply

15 Jure September 2, 2009 at 1:17 pm

Does not work if copying files with too long names or copying more than 37 thousand files.

Reply

16 Calvin Foo September 9, 2009 at 10:50 pm

How do I copy everything including the subdirectories? I can’t seems to do it with xcopy

Reply

17 Calvin Foo September 9, 2009 at 11:13 pm

Sorry, just found out my error. Thanks

Reply

18 george October 1, 2009 at 5:44 pm

where do i save the script in order to use it?

Reply

19 Bob January 17, 2010 at 2:38 am

I want to copy files but keep a second copy of it using a a
batch without oveririting

Reply

20 ummar farook salem avs engineering March 28, 2010 at 9:52 am

it was so nice thank you we want from basic tutorials

Reply

21 Prakash March 29, 2010 at 3:37 am

Hi Guys,

Any one know how to copy a files from one windows server to another. I have a requirement like this, Need to check the file name for date part, if the date_part is greater than sysdate then move the file to destination.

Please let me know the scripting part, which will be much helpfull for me.

Note: I’m very new to this environment. Please help

Reply

22 Anonymous March 29, 2010 at 1:52 pm

use robocopy.exe is more powerfull than xcopy.

Reply

23 Annon May 6, 2010 at 2:10 pm

hi I have a list of files that I want to copy from a multiple set of folders to another single folder. I have all the path info in a notepad file. Is there a script that can do this?

So for example:

File a is in folder 1

File b is in folder 2

Want to move file a and file b to folder 3

Many thanks

Reply

24 bmw May 27, 2010 at 9:06 pm

I need the user enter the date and use this input date to copy the file which created on that date. I got the error message is “Invalid switch – /D:

My code is like that:
set /p copyDate=Please input the date as mm-dd-yyyy.
echo You enter %copyDate%
set source=c:\csw
set target=U:\\test
xcopy “%source%” “%target%” /D: %copyDate%

Would you help me?

Reply

25 Huldu August 2, 2010 at 1:03 pm

Okay, i havent found a way to copy a specific file from a specific directory to a *new* location while *maintaining* the directory structure. It has annoyed the hell out of me that no tool supports this. Yeah, ive tried xcopy, robocopy and some other tools none has worked. It has to be command line based.

Reply

26 Clarify This Doubt For Me August 8, 2010 at 4:50 pm

Actually i want to get clarified one thing..while copying any files or something does the system copy in the form of o’s and 1′s or it just changes the internal link or address.

Please clear this info for me..

If it is copied in the form of 0′s and 1′s then can anyone give the script to do that so that i have a new idea of developing a software…..

Reply

27 sriramsubbiah November 11, 2010 at 11:58 am

Question :

I have file list in excel sheet, that list of files I need to copy one folder from server. since the files are in server with same name. please help me. the files are in word format.but the list in excel.

awaiting your reply.

Thanks,
S. Sriram.

Reply

28 rob_ot February 22, 2011 at 4:28 am

Thanks…very helpful :)

Reply

29 Amit February 24, 2011 at 8:18 am

Can someone help in writing a windows script to get the latest file in the network folder and copy to another location?

Reply

30 Sam March 29, 2011 at 1:30 pm

Hi,
We have to add some word Template files In every users within organisation.we have a setup of near about 70 users. i have created 1 folder to root in ‘D’ drive
& pasted all file whithin it .file extention is” .dot” overall 8 files over there.the source path for file is D:\teplates\ & destination path would be
C:\Documents and Settings\\application Data\Microsoft\Templates
this batch file should passed each user name. so plese guide me how i will create a script for the same.
thanks in Advance.

Reply

31 Prasad October 25, 2011 at 4:24 am

I am looking a batch file code which will copy file from Computer A to Computer B both on diffrent subnet. Can any one help me how to do this?

Reply

32 Jeet February 9, 2012 at 10:24 pm

Hi I’m trying to write a script from one server to another in which i want to copy a monthly file that we get every month to proceed automatically

Reply

33 JayGuy March 27, 2012 at 8:03 pm

I’m using batch file call robocopy, you can also try xxcopy

Reply

34 Raj April 10, 2012 at 8:56 am

Thanks for the script… helpful

Reply

35 Snooze May 4, 2012 at 3:05 pm

Is there a way to automatically copy a newly added file in one folder to another folder? For example, as soon as I send a file to folder-A, it automatically makes a copy of it in folder-B.

Reply

36 Mannan August 15, 2012 at 7:12 am

Good.it is useful….

Reply

37 phattana November 27, 2012 at 9:38 am

Can you tell me
How can create a script automatically copies a file from USB drives to my pc when insert ?

Reply

38 phattana November 27, 2012 at 9:39 am

Can you tell me
How can create a script automatically copies a file from USB drives to my pc when insert ?
bat file or java ……ok

Reply

Leave a Comment


× six = 24

Previous post:

Next post: