Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ROBOCOPY TO SEARCH IN SUBFOLDERS

Posted on 2006-11-21
15
Medium Priority
?
2,271 Views
Last Modified: 2012-06-21
Hi,
I have a code that copies only .xls files from one location to another without actually copying the folders. I need this to be modified so that The source looks for all the subfolders under it for .xls files and not only look just the root folder.
eg: if I have C:/Source/AllFiles/excel1.xls and C:Source/excel2.xls then if I give C:/Source as my source then it must copy both excel1 and excel 2 files and not just excel1.

Please assist.
Thanks
0
Comment
Question by:zubairvp123
[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
  • 9
  • 6
15 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 17986649
Use /s parameter...

robocopy c:\source d:\destination *.xls /s
0
 

Author Comment

by:zubairvp123
ID: 17993145
/s parameters include subfolders but do not search in subfolders. I dont want any folders copied but only files in the subfolders.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 200 total points
ID: 17993305
Oops - sorry about that.

Try this from a command prompt:

cd source
for /r %a in (*.xls) do robocopy %~dpsa c:\destination *.xls

should accomplish what you want...if you want it in a batch file,
repeat the above two lines, but double up on the % signs...
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:zubairvp123
ID: 17993383
Oh My God!!! This looks for all the files in my C drive. I guess my question is not clear.
If I have a directory C:/Source/Source1/Source2/*.xls
then In my batch file if I specify my source as C:/Source then it must look for C/Source and further down till it gets a .xls file. It shouldnt look in my entire C: but The source+ subfolders.
I'm sure you know the answer...it's just the way I put the question. Sorry about that.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17993400
No, I understood.
That's why it's necessary to use the
 cd source
command first.

This starts you out in the source folder.  It'll then scan through each subfolder and copy *.xls to your destination folder.
Obviously if you have the same file name in seperate folders, the results will be different...
0
 

Author Comment

by:zubairvp123
ID: 17993533
Thanks, can I ask...
what does %~dpsa  do?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17993547
the for loop is looking in all subfolders for the specification you asked for, *.xls
This loop returns the full path and file name...all we need is the folder name, since that's what robocopy's gonna want.
So, I used that 'filter' to strip off the file name.  It takes c:\source\file.xls and only returns the (d)rive and (p)ath and shows it in its (s)hort format (LongFo~1 instead of Long Folder Name - so we don't have to contend with spaces).
0
 

Author Comment

by:zubairvp123
ID: 17993592
Wow!!! You are a genious!!! Thanks a bunch! Looks like it's working now. Let me do a few days of test on it so that I know whether it's tracking the modified and new files.
For now all the points to you.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17993594
Happy to have helped. :^)
0
 

Author Comment

by:zubairvp123
ID: 17994722
Sirbounty,
I'd tested with files in my C:/ but my actual operations are to be performed on files on the network (Shared LAN).
When I put the path Z:/ it by default takes files on my desktop.

I thought that robocopy could copy files from the lan aswell.
Please correct me if I'm wrong.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17994968
Sure it will...that could be considered it's primary purpose.

Show me what you're running...
0
 

Author Comment

by:zubairvp123
ID: 18000875
@echo off
set source1=I:\DTF\00 DTF Daily Report Drop Box\Daily Activity Report
set source2=C:\Source
set dest=C:\dest1
set n=7
cd "%source1%"
for /r %%a in (*DDR*.xls) do robocopy %%~dpsa %dest% *DDR*.xls /copy:DAT /MAXAGE:%n% /M /log+log.txt

cd "%source2%"
for /r %%a in (*DDR*.xls) do robocopy %%~dpsa %dest% *DDR*.xls /copy:DAT /MAXAGE:%n% /M >>log.txt

Sirbounty, It takes the files from source2 but not source1.
Also it would be great if you could show me how to have log.txt files that get generated showing which all files were successfully copied and which files were skipped.
0
 

Author Comment

by:zubairvp123
ID: 18000917
I just realized that /log+log.txt
 is invalid. Even if I delete it no files are picked up from this source1.
0
 

Author Comment

by:zubairvp123
ID: 18001706
Someone...please help! It must be a small tweek required somewhere which I'm not aware of....
Please....
0
 

Author Comment

by:zubairvp123
ID: 18013724
Any updates? Please...
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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