Batch file using For command

Posted on 2013-01-28
Medium Priority
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
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
  • 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 56

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?
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


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.
LVL 56

Expert Comment

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

LVL 56

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 2000 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
OfficeMate Freezes on login or does not load after login credentials are input.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Suggested Courses

764 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