[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copy subdirectory files without directory structure

Posted on 2010-11-08
12
Medium Priority
?
983 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

830 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