Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

copy multiple files from multiple subdirectories to different location

Posted on 2011-02-18
9
Medium Priority
?
693 Views
Last Modified: 2012-06-21
URGENT, Please Help - I need to script (with my basic knowledge of cmd, bat scripting) a file copy from one server to another server.  I need to include part of the source's folder (directory) structure where the folder name changes as do the files.  See example below
(Source destination could be mapped drive or UNC) + (Ideally would like to supply the multiple directories & files (1DC\0000AD30.017) in source (txt) file)

Example:
Source
\\server1\directory1\directory2\1DC\0000AD30.017
\\server1\directory1\directory2\1DE\0000AD32.001
\\server1\directory1\directory2\1DE\0000AD32.002
\\server1\directory1\directory2\1E2\0000AD36.001
\\server1\directory1\directory2\1E2\0000AD36.002
\\server1\directory1\directory2\1E3\0000AD37.001
\\server1\directory1\directory2\1E4\0000AD38.001
\\server1\directory1\directory2\1E5\0000AD39.001
\\server1\directory1\directory2\1E6\0000AD3A.001

Destination:
\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DC\0000AD30.017
\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DE\0000AD32.001
\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DE\0000AD32.002
\\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E2\0000AD36.001
\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E2\0000AD36.002
\\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E3\0000AD37.001
\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E4\0000AD38.001
\\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E5\0000AD39.001
\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E6\0000AD3A.001
Any assistance will be most greatly appreciated. Cheers

0
Comment
Question by:flannk
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:ceesios
ID: 34931892
Hi there,

try this. You can leave out the domain part if you don't use it.
net use \\server1\ /user:domain\username password
net use \\server2\ /user:domain\username password

copy \\server1\directory1\directory2\1DC\0000AD30.017 \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DC\0000AD30.017
copy \\server1\directory1\directory2\1DE\0000AD32.001 \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DE\0000AD32.001
copy \\server1\directory1\directory2\1DE\0000AD32.002 \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DE\0000AD32.002
copy \\server1\directory1\directory2\1E2\0000AD36.001 \\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E2\0000AD36.001
copy \\server1\directory1\directory2\1E2\0000AD36.002 \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E2\0000AD36.002
copy \\server1\directory1\directory2\1E3\0000AD37.001 \\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E3\0000AD37.001
copy \\server1\directory1\directory2\1E4\0000AD38.001 \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E4\0000AD38.001
copy \\server1\directory1\directory2\1E5\0000AD39.001 \\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E5\0000AD39.001
copy \\server1\directory1\directory2\1E6\0000AD3A.001 \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1E6\0000AD3A.001

Open in new window

0
 

Author Comment

by:flannk
ID: 34932022
Thanks I will test.  One extra question thou, What would the switch be to create the  "1DC", "1DE", etc directory if it doesn't already exist in the destination?
0
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 34932299
MD is Command for create Directory

MD \\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\1DC

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 6

Expert Comment

by:Draxonic
ID: 34932333
Put your source files in inputfile.txt and run this is a batch file:

net use \\server1\ /user:domain\username password
net use \\server2\ /user:domain\username password

FOR /F "usebackq delims=\ tokens=1-5" %%i in (inputfile.txt) do (

xcopy "%%i\%%j\%%k\%%l\%%m" "\\server2\diffdirectory1\diffdirectory2\diffdirectory3\%%l\%%m"
)

Open in new window

0
 
LVL 6

Expert Comment

by:Draxonic
ID: 34932347
The xcopy command in the above script will create the destination path structure automatically.

It may need a little tweaking depending on the number of variables in your actual path. *shrug*

This may be overcomplicating it though. If you use ceesios script with xcopy instead of copy, it will create the destination structure but will need more work if the files names change all the time.
0
 

Author Comment

by:flannk
ID: 34932376
The copy command above won't work if it doesn't create the directories (like 1DC, 1DE, 1E2)   as I need the files in these directories and they may not already exist.
0
 
LVL 1

Expert Comment

by:ceesios
ID: 34932454
The xcopy command from Draxonic should fix that, just replace copy with xcopy.
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1500 total points
ID: 34934799
Sincce you have a single source and destination folder each, a more dynamic and less delicate approach can be chosen. You just need to provide source and destination (root) folders, and a list of files with full source path in c:\filelist.txt (or whichever file you want to use):
@echo off
setlocal EnableDelayedExpansion
set source=\\server1\directory1\directory2\
set dest=\\server2\diffdirectory1\diffdirectory2\diffdirectory3\diffdirectory4\
for /F "tokens=*" %%F in (c:\filelist.txt) do (
  set p=%%~F
  echo xcopy "%%~F" "!p:%source%=%dest%!"
)

Open in new window

That script will only simulate. To actually copy files remove the echo in front of xcopy.
0
 

Author Closing Comment

by:flannk
ID: 34986140
I had to alter slightly but simulate very helpful. thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Screencast - Getting to Know the Pipeline

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question