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

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?
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!


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 54

Expert Comment

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

LVL 54

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Scenario:  You do full backups to a internal hard drive in either product (SBS or Server 2008).  All goes well for a very long time.  One day, backups begin to fail with a message that the disk is full.  Your disk contains many, many more backups th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

730 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