Solved

copy multiple files from multiple subdirectories to different location

Posted on 2011-02-18
9
689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

 
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 69

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…

733 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