Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy subdirectory files without directory structure

Posted on 2010-11-08
12
Medium Priority
?
981 Views
Last Modified: 2012-05-10
This is much similar to this question: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21689294.html

However, I 1. Do not understand how that script works, and 2. Need to make sure it will only copy newer files.

Here's the situation.

We have a complex directory structure on FileServer. We need to copy everything from file server "S:\filefolder" and all subdirectories to our WebServer, but all under the root "Y:\"

The users work on the file server, but need the customers to be able to view the images on the web server. So they're currently doing their work and then copying it manually. They would like this automated.

I need to copy all new files within all subdirectories to a single destination folder without the directory structure.

Also, if there was a way for a script/program to 'watch' for changes/new files to be added and run the script upon that change, that would be incredible.

Thanks

*Note* Both servers are running W2k3
0
Comment
Question by:FloydTheDuck
[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
  • 6
  • 5
12 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 34084604
what to do if files from different subdirectories have the same name?
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 34085358
From the accepted solution I would change the copy to xcopy and add the /d to only copy if newer. Also put a \*.* at the end of the destination directory.

@echo off

setlocal

REM ** Would change these as needed
set srcDir=c:\yoursourcedir
set destDir=c:\yourdestinationdir

for /f "delims=" %%a in ('dir "%srcDir%" /s /b 2^>NUL') do xcopy /d "%%a" "%destDir%\*.*"

0
 
LVL 1

Author Comment

by:FloydTheDuck
ID: 34085412
The new file will overwrite the old. If they ever have the same name then it will only be an updated version of the file, regardless of folder location.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:FloydTheDuck
ID: 34085432
Amazing: Can you explain what this line of code does/means? I'd like to have an understanding of each parameter so I can troubleshoot if there are errors.

for /f "delims=" %%a in ('dir "%srcDir%" /s /b 2^>NUL') do xcopy /d "%%a" "%destDir%\*.*"
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 34087646
The for command just puts all the output into a variable in this case %%a. The "delims=" option takes the entire line in 1 variable.

The output is the DIR "%scrDIR%" /s /b. Which is all files including subdirectories in bare format.

if the %scrDir% directory does not exist or there are no files in this directory the 2^>NUL suppresses these errors.

Does that help? Or was there something specific?
0
 
LVL 1

Author Comment

by:FloydTheDuck
ID: 34087822
Thanks, that helps a lot. What is the "/f" for? I saw another script similar to this, can you tell me how they differ?

for /r "S:\FILES\subfolders\stuff\" %d (*) do copy "%d" "Y:\"

compared to

for /f "delims=" %%a in ('dir "%srcDir%" /s /b 2^>NUL') do xcopy /d "%%a" "%destDir%\*.*"

If you prefer to point me to a tutorial article explaining all this, that's fine too... I just couldn't find one when I googled.

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 34090793
Basically the same.

for /r "S:\FILES\subfolders\stuff\" %%d (*) do xcopy /d "%%d" "Y:\"

for syntax can be found on the command prompt with "for /?"

/f could be file-set, string or command.

There's not that much help on the internet. You'll find much more within EE.

0
 
LVL 1

Author Comment

by:FloydTheDuck
ID: 34120143
I used the following script and it did not work.



@echo off

setlocal

REM ** Would change these as needed
set srcDir=S:\files\stuff\morestuff\subfolder
set destDir=Y:\

for /f "delims=" %%a in ('dir "%srcDir%" /s /b 2^>NUL') do xcopy /d "%%a" "%destDir%\*.*"
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 34123189
Hmm. That's pretty weird.

Let's try getting some output.

@echo off

setlocal

REM ** Would change these as needed
set srcDir=S:\files\stuff\morestuff\subfolder
set destDir=Y:\

ECHO Here's what's in the directory.
dir "%srcDir%" /s /b 2>NUL
ECHO Files in "%srcDir%"
PAUSE

ECHO Here's what's in the directory using FOR
for /f "delims=" %%a in ('dir "%srcDir%" /s /b 2^>NUL') do echo "%%a"
ECHO Files in "%srcDir%" using FOR
PAUSE


for /f "delims=" %%a in ('dir "%srcDir%" /s /b 2^>NUL') do xcopy /d "%%a" "%destDir%\*.*"
0
 
LVL 1

Author Comment

by:FloydTheDuck
ID: 34135798
Here's the output:

C:\>REM ** Would change these as needed

C:\>set srcDir="S:\FILES\place\another subfolder\IMAGES\2011"

C:\>set destDir=S:\test

C:\>ECHO Here's what's in the directory.
Here's what's in the directory.

C:\>dir ""S:\FILES\place\another subfolder\IMAGES\2011"" /s /b  2>NUL

C:\>ECHO Files in ""S:\FILES\place\another subfolder\IMAGES\2011""
Files in ""S:\FILES\place\another subfolder\IMAGES\2011""

C:\>PAUSE
Press any key to continue . . .

C:\>ECHO Here's what's in the directory using FOR
Here's what's in the directory using FOR

C:\>for /F "delims=" %a in ('dir ""S:\FILES\place\another subfolder\IMAGES\20
11"" /s /b 2>NUL') do echo "%a"

C:\>ECHO Files in ""S:\FILES\place\another subfolder\IMAGES\2011"" using FOR

Files in ""S:\FILES\place\another subfolder\IMAGES\2011"" using FOR

C:\>PAUSE
Press any key to continue . . .
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 34137170
Oh your set srcDIR= has double quotes they are not necessary on this line since we are adding them to the commands to copy and dir.
0
 
LVL 1

Author Closing Comment

by:FloydTheDuck
ID: 34137683
That did the trick! I was thinking I needed quotes on srcDir because of the space in the folder name.

Thanks for your help
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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