Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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 58

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?
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


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 58

Expert Comment

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

LVL 58

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A procedure for exporting installed hotfix details of remote computers using powershell
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 single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

604 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