Solved

copy multiple files from multiple subdirectories to different location

Posted on 2011-02-18
9
684 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 68

Accepted Solution

by:
Qlemo earned 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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 video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now