Batch file using For command

Posted on 2013-01-28
Last Modified: 2013-01-31
I am trying to create a batch file that will search a directory for a specific subdirectory then move the subdirectory and all files within to different folder.

I attempted the command below and I have not been able to make this work.  

FOR /R <filepath1> %%G IN (1-1049780) DO "robocopy.exe /move %%G <filepath2>

Note: 1-1049789 is the directory name that I wish to move.  I am using excel and the concatenate function to create a batch file that has the above command for over 20k entries.  I will have a batch file that has 20k entries that will need to run.  I wanted to attempt querying a file, but was not sure of the proper syntax for the command.  

Can anyone confirm that the directory name in parenthesis is going to be read as a folder name and not a range of values?

There may be a better way to do this using VBscript, and I am open to any suggestions that you have.

Thanks in advance for any assistance you can provide.
Question by:mdennis22
  • 3
  • 2
  • 2
  • +1
LVL 43

Expert Comment

by:Steve Knight
ID: 38829967
can you give a  example of the filepath1 and filepath2 entries, and is this basically a case of reading directories from a and sending them to b, for instance, i.e. where does the excel sheet data come into it?
LVL 51

Expert Comment

by:Bill Prew
ID: 38830741
I think this should do the job. I'm assuming you have the options on robocopy you want/need, I didn't address those, just the "how do I find the folder" part of the question:

for /F "tokens=*" %%G in ('dir /b /ad /s "C:\Dir1\1-1049780"') do robocopy.exe /move "%%~G" "C:\Dir2"

Open in new window

LVL 43

Expert Comment

by:Steve Knight
ID: 38830767
Bill - I was thinking more of the fact that the spreadsheet quite possibly just holds a list of all the directories under certain structure etc. as 20k entries sounds a lot to manually maintain?

Author Comment

ID: 38831669
The directories match a list of invoice numbers that I have in a spreadsheet.  There are a couple of directories which are all subfolders of F:\karmak\rush, my delima is that the folder with the invoice name could be in "f:\karak\rush\invoicenumber"  or "F:\karmak\rush\match\<recieved date>\<invoice number>\" or "F:\karmak\rush\nomatch\<received date>\<invoice number>"  

The main problem is our import process had a glitch and removed the leading two digits for about 20k invoices before we realized we had a problem and fixed it.  

I am having to manipulate the actual invoice numbers to make them the truncated numbers and add the hypen in as that is the way we receive them but don't index them that way in our core system.  

By the way the directory that I am looking for also contains a pdf file with the same name.  

I would have included all of the above last night, but yesterday was a very long day  and I was getting frustrated.  

Bill i fthis changes anything that you were attempting let me know.  One other thing the invoice directory may or may not exist depending on if we have received the printed invoice or not.
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

LVL 51

Expert Comment

by:Bill Prew
ID: 38831918
So then, are you really just trying to rename the directory name, rather than move it?

LVL 51

Expert Comment

by:Bill Prew
ID: 38831979
Also, what logic are you applying to rename the invoice number?  If it's a standard set of rules we could do this whole thing in a script perhaps, just driven by a list of the faulty invoice numbers.

LVL 14

Accepted Solution

bfason earned 500 total points
ID: 38833426
It sounds like you need a 2 step process. One find the path for directories you wish to move then use the path to move selected directory to new location.

Assumming the reference file is a csv with dir name and location to move to named source1.csv


echo off
For /f "tokens=1,2 delims=," %%a in (source1.csv) do for /f %%c in ('dir /b /s /a:d %%a') do echo %%c,%%b\%%a >>source2.csv

This gives you a new source2.csv which should be something like


 after you have inspected source2.csv for acurracy you can then run another for command to move the files

For /f "tokens=1,2 delims=," %%a in (source2.csv) do robocopy.exe /move %%a %%b

If you are feeling incredibly brave and trust an internet stranger you can put both commands in batch1....don't do it :)

Hope this helps

Author Closing Comment

ID: 38839875
Thanks to all.

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

757 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

17 Experts available now in Live!

Get 1:1 Help Now