Solved

Copy subdirectory files without directory structure

Posted on 2010-11-08
12
968 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:sedgwick
ID: 34084604
what to do if files from different subdirectories have the same name?
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 500 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

20 Experts available now in Live!

Get 1:1 Help Now