How do I copy several files into several directories using relative paths?

I would like to create a batch file that would copy file a,file b, file c from a usb stick drive to my hard drive using these folders on my 2nd drive partition using relative paths:  directory a ,directory b and directory z .    Then copy the file zebra.dll and zebra.exe to directory z,  Can someone show me how this is done>
John MarshallAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
I don't really understand what you mean by "relative paths"?

Are the files in the root of the USB drive?

How will the BAT script be run, will it need to determine what the USB drive is, or will it always be the same?

What directory does file c go to?

Missus Miss_SellaneusCommented:
just use the XCOPY command.

xcopy /s /e *.* <backuppath>

ie: copy /s /e d:\*.* c:\usbstickbackup

type xcopy /? in cmd prompt to see all the switches.
NVITEnd-user supportCommented:
My guess...
Note: drive u: = USB drive. Yours may differ. See Windows Explorer

copy u:\filea.* c:\directory_a
copy u:\fileb.* c:\directory_c
copy u:\filec.* c:\directory_z
copy u:\zebra.dll c:\directory_z
copy u:\zebra.exe c:\directory_z

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

John MarshallAuthor Commented:
I'm building a batch file that can do what I said above automatically.  If the drive letters change it will still work.  Or is there a way that I can make sure that the stick drive always has the same letter ?  Also what if  the drive letters change?
Missus Miss_SellaneusCommented:
use xcopy like above and you only need one line in your batch file. You will have to change the drive letters, or else you can just modify your batch file to accept the drive letters as parameters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Missus Miss_SellaneusCommented:
something like this:
set USBdrive=%1
set Destination=%2
xcopy /s /e %USBdrive% %Destination%

and if the batch file is named copyUSB.bat, your USB stick is F: and your destination is c:\backupUSB\

call it like this:
copyUSB F:\ c:\backupUSB
John MarshallAuthor Commented:
What if the USB drive changes letters?
What if you have one file to copy in several places?

How about if the destinations were on different drives?

Such as

Missus Miss_SellaneusCommented:
If you have destinations on multiple drives at the same time, you'll need to use multiple parameters to do it. If it's only one destination each time you can do it like I said above:

usb drive is F: and destination is c:\backup

copyUSB F:\ c:\backup

usb drive is g: and destination is d:\usbfiles

If it's just a few destinations, each from different starting folders on the USB drive, you can run the batch file more than once, sending different parameters each time.

copyUSB G:\folderA d:\usbfiles
copyUSB F:\folderB c:\backup


You have asked two different questions.  You originally asked how to copy files from the flash drive to folders "on my 2nd drive partition".  Assuming the 2nd partition to be D and the flash drive to be F you wanted to copy "F:\a*.*" to "D:\Folder_A\a*.*" and so on.  Before answering any of the clarification questions asked by Bill Prew, you then posed the question about what would happen if the destination folders were on different drives.  That's two completely different scenarios that would need various tests to be performed before copying anything.  There is no such thing as a clever batch file that can guess things like that.  You have to create a series of tests that check for the existence of folders in the destination paths.

One of the issues you are concerned about is changing drive letters of the Flash drive.  The easiest way to overcome that, without resorting to 3rd-party utilities, VB Scripts, or WMIC commands that find the drive letter by verifying the type of device, is to run the batch file from the root of the Flash drive itself.  That way you wouldn't have to specify a source path.

I am wondering what the exact purpose of this batch file is.  Clearly it's a backup of some kind, but if this is simply to synchronise folders between flash drive and the same PC each time, there are some good free sync utilities.   Don't expect to have a batch file that springs into life and runs when the computer detects the insertion of the flash drive.  Autorun is usually disabled by default on newer operating systems for security reasons due to viruses that use the autorun feature to spread from removable drives to hard drive.

It sounds to me as if you want this batch file to run on ANY computer where the drive letter assignments will differ.
Is this the case?
If so, then the batch file is going to need to run tests to establish whether the destination folders exist on named drive letters.  The easiest way to check is to use the   IF EXIST "D:\Folder_A" DO SOMETHING   test.  Generally speaking it is not a good idea to create new folders for purposes like this directly in the root of the C: Drive, but there would be no problem doing so in the roots of other partitions or separate drives.

Would these destination folders always be in the root of the destination drives, or might they be deeper into sub-folders?

Bill Prew asked whether ALL of the a, b, c, .... z files will be in the root of the flash drive.
That is an important aspect that also needs to be considered.

It is hard to write a batch file based only on a generalised concept.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.