Link to home
Start Free TrialLog in
Avatar of FloydTheDuck
FloydTheDuck

asked on

Copy subdirectory files without directory structure

This is much similar to this question: https://www.experts-exchange.com/questions/21689294/Copy-files-without-directory-structure.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
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

what to do if files from different subdirectories have the same name?
ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FloydTheDuck
FloydTheDuck

ASKER

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.
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%\*.*"
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?
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.

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.

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%\*.*"
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%\*.*"
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 . . .
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.
That did the trick! I was thinking I needed quotes on srcDir because of the space in the folder name.

Thanks for your help