Solved

Copy subdirectory files without directory structure

Posted on 2010-11-08
12
971 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make an ADE file by code? 11 87
Message Logon Windows 2012 R2 Server 16 26
Can anyone derive why this VBS doesn't work? 4 24
powershell script 9 25
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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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