Batch file using For command

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.
Who is Participating?
bfasonConnect With a Mentor Commented:
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
Steve KnightIT ConsultancyCommented:
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?
Bill PrewCommented:
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

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Steve KnightIT ConsultancyCommented:
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?
mdennis22Author Commented:
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.
Bill PrewCommented:
So then, are you really just trying to rename the directory name, rather than move it?

Bill PrewCommented:
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.

mdennis22Author Commented:
Thanks to all.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.