We help IT Professionals succeed at work.

DOS Batch File: Loop through directory.

Bydlo52
Bydlo52 asked
on
42,284 Views
Last Modified: 2009-06-30
Hi All,

I'm not going to pretend that I know much about DOS batch files other than fairly simply operations. Here's the situation at hand: I need a file that will loop through a directory and for each file in that directory, call a Java jar file. We've got a "dev" and a "production" folder for our javascript files, and we just want a quick script that will loop through the "dev" folder, compress each one and copy the output to the "production" folder. Pseudocode is:

for each(file in directory)
     // $inFile will be the current file in the loop. Something like: "C:\scripts\dev\myfile.js"
     // $outFile will be something like: "C:\scripts\production\myfile.js"
     java -jar "myjavafile.jar" $inFile $outFile
end for each

Thanks for the help!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
Try

for /f %%a in ('dir /b *.js') do java -jar "%%a" $inFile $outFile
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks sirbounty.  I've just about got it up and running. My fault for not specifying the problem exactly enough, but is there a way to concatenate %1 and %2 in :process, such that p1 is the full path to the JS file?
CERTIFIED EXPERT
Top Expert 2007

Commented:
Not sure I follow - but you can just remove the "%%a" from the call: process line if you want the full path...

%%~dpa
is the (d) drive, (p)ath of the (a) file...

if that doesn't help work it out - let me know and I'll refine it for you..
Great! Just what I needed to loop through some files.
But it seems like the label being called needs to be the last in the batch file, or else it will just continue on executing the remainder of the lines in the script.

I have a fairly complex batch script, but adding the label to the very end, and making sure I exit the script before that label, it works like a charm!
CERTIFIED EXPERT
Top Expert 2007

Commented:
if you have multiple labels, place a
goto :eof
just before each one to ensure that it doesn't run unexpectedly...
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.