[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

DOS Get list of directories and pass each to a command line

hi chaps

I need to create a batch file that will pass the name of each folder in a given folder, in turn, as a switch for another command line app.

I have a folder called WORKING which has lots of folders. We need to run a command line app against each of these folders such as....

myapp.exe -action1 -patch = c:\working\folder1
myapp.exe -action1 -patch = c:\working\folder2
etc etc

the c:\working folder may have 100 folders in it of various names and each one needs to be inserted as a switch to the myapp line above and then that line actioned.

Does that make sense?

I can get the list of folders without all the DIR bumpf that goes with a usual DIR listing but then I'm struggling (as ever) on the loop to pass each folder to the command line above.

Any suggestions?

Olly
0
stonneway
Asked:
stonneway
  • 10
  • 7
  • 5
  • +3
4 Solutions
 
Krzysztof PytkoActive Directory EngineerCommented:
Have you tried this command ?

for /f %i in ('dir c:\working\ /b') do myapp.exe -action1 -patch = c:\working\%i

Regards,
Krzysztof
0
 
Krzysztof PytkoActive Directory EngineerCommented:
and for security in case that folder name contains space, use this syntax

for /f "tokens=*" %i in ('dir c:\working\ /b') do myapp.exe -action1 -patch = c:\working\%i

Krzysztof
0
 
Krzysztof PytkoActive Directory EngineerCommented:
in above syntax I also forgot about quotes, sorry. This is correct one

for /f "tokens=*" %i in ('dir c:\working\ /b') do myapp.exe -action1 -patch = "c:\working\%i"

Krzysztof
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Steve KnightIT ConsultancyCommented:
Make sure you add /ad to only return directories if using dir command, or can use for /d instead:

As you probably know if you use in a batch file use %% instead of %

@echo off
for /f "tokens=*"  %%a in ('dir /b /ad C:\working') do myapp.exe -action1 -patch = c:\working\%%~a

for /d %%a in (c:\working\*) do do myapp.exe -action1 -patch = %%~a

or

cd /d c:\working
for /d %%a in (*) do do myapp.exe -action1 -patch = c:\working\%%~a
0
 
paultomasiCommented:
The proper way to do this is as follows:
@echo off

for /d %%a in ("c:\working\*") do (
   myapp.exe -action1 -patch = "%%a\"
)

exit /b

Open in new window

Or you could end with just EXIT in which case theDOS (or CMD) window will close when the batch file ends.
0
 
Steve KnightIT ConsultancyCommented:
Erm Paul.... "Proper way".  I thought programming was an art :-)

Please explain how yours works any differently to this from my previous comment:

for /d %%a in (c:\working\*) do myapp.exe -action1 -patch = %%~a

If he needs quotes around the path for -patch they can be added, as can a final \ but that depends on his app...

Steve
0
 
stonnewayAuthor Commented:
Thanks chaps.

If i wanted to pipe the output of the "myapp.exe blah" line to a log file could I just add " >> c:\log.txt" at the end of the %%~a bit?

Olly
0
 
Bill PrewCommented:
==> If i wanted to pipe the output of the "myapp.exe blah" line to a log file could I just add
==> " >> c:\log.txt" at the end of the %%~a bit?

Yes, although a couple of notes.  You might want to also capture any error messages from the execution, so you can add this:

>> c:\log.txt 2>&1

In addition, if your log file name has spaces in it, then enclose in quotes, as in.

>> "c:\log.txt" 2>&1

Lastly, if you want to clear the log file at the top of the BAT file, in case it is left over from prior runs (assuming you don't want it to grow forever), then you can add a DEL like this:

if exist "c:\log.txt" del "c:\log.txt"

~bp
0
 
Steve KnightIT ConsultancyCommented:
or easier, redirect irt all:

(for ..... Do myapp.exe  ..... %%a
) >log.txt 2>&1

then that starts a new log each time and writes it once rather than lots of appends.

hth

steve
0
 
paultomasiCommented:
The proper way to add redirection is as follows:


for /d %%a in ("c:\working\*") do (
   myapp.exe -action1 -patch = "%%a\">>"log.txt"
)

0
 
Bill PrewCommented:
Cool, I did it the proper way...

~bp
0
 
Steve KnightIT ConsultancyCommented:
Being pedantic now Paul as usual.. you say "proper" again?

Well that was what Bill said .... and I use my suggested way if possible - why append to the file for each line when you can send the lot with one redirect...

(for /d %%a in ("c:\working\*") do myapp.exe -action1 -patch = "%%a\" ) >"log.txt" 2>&1

To the OP... anything between ( and ) is redirected to the file.  The 2>&1 bit redirects the errors back into the same stream.

Steve
0
 
Steve KnightIT ConsultancyCommented:
Oh and Paul.... the quesiton asker had also said add >> log.txt onto the end of the line too... why repeat?!

Steve
0
 
BillDLCommented:
Steve, the "proper" way to spell "quesiton" is "question", and I'm not entirely sure about the linguistic precision of using a ? along with a ! at the end of a sentence.  Not sure about the triple dot thing either ... hmmm ;-)
Whoops, I think the proper way to "emoticonize" (is that a proper word?) a wink is ;) not ;-)
0
 
Bill PrewCommented:
There's Bill!

~bp
0
 
BillDLCommented:
Hi Bill.  Glad I didn't post, cause I didn't want a telling off :)
0
 
Steve KnightIT ConsultancyCommented:
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.
I must write in sentences with proper grammar and punctuation, and program properly.

Can I go out to play now?
0
 
paultomasiCommented:
stonneway

In addition to my excellent solution (http:#37040295):

   for /d %%a in ("c:\working\*") do (
      myapp.exe -action1 -patch = "%%a\">>"log.txt"
   )

dragon-it (http:#37040322) raises awareness of error messeges with:

   >"log.txt" 2>&1


There are 4 places you can direct error messeges to:

(1) CON: The screen. This is the default location however, error messeges can also be forced here with:

   2>con


(2) NUL: This is the NUL device. Output to the NUL device ends up going nowhere. This is most commonly used when directing output away from the screen:

   2>nul


(3) STDOUT: To wherever standard outout is being sent (in this case, LOG.TXT, as shown by dragon-it http:#37040322):

   >log.txt 2>&1

However, also consider these alternatives:

   1>log.txt 2>&1

   >>log.txt 2>&1

   1>>log.txt 2>&1


(4) File: To a file (in this case, ERR.TXT):

   >log.txt 2>err.txt

   >>log.txt 2>err.txt

   >log.txt 2>>err.txt

   >>log.txt 2>>err.txt


   1>log.txt 2>err.txt

   1>>log.txt 2>err.txt

   1>log.txt 2>>err.txt

   1>>log.txt 2>>err.txt


NOTE:

   '1' explicitly implies standard output messeges

   '2' explicitly implies error messeges

   '>' sends to file (or device) overwriting previous output

   '>>' sends to file (or device) appending to previous output


Finally, be aware that some commands many send both standard messeges AND error messeges to the same output channel in which case seperating the two can be tricky and may be the subject of a future EE article.
 
I hope this has all been helpful to you.
0
 
Steve KnightIT ConsultancyCommented:
It is no wonder that sometimes questioners go away in a daze and don't select an answer... 20 or so posts to supply a one line batch file with five of us chipping in!
0
 
Bill PrewCommented:
And who says DOS scripting is dead! :-)

~bp
0
 
paultomasiCommented:
'Mucho apolojeeto'...

It was billprew (http:#37038136) who first raised awareness to error messeges - not dragon-it (http:#37040322) as I first thought :)
0
 
paultomasiCommented:
>> "Erm Paul.... "Proper way".  I thought programming was an art"

Of course it's an art... but some artists are better than others! And not all artists know how to hold a brush properly let alone paint a masterpiece!

>> "Please explain how yours works any differently..."

Mine works properly, doesn't it!?

>> "Cool, I did it the proper way..."

You must be an artist too then...

>> "Being pedantic now Paul as usual.. you say "proper" again?"

Yep, with a single 'P'... PROPER!

>> "Well that was what Bill said..."

That's 'cuz Bill's 'proper' too!

>> "And who says DOS scripting is dead!"

dragon-it, since posting my comment (http:#37037341), which I'm sure you'll agree has now been widely accepted amongst us as the proper solution to this question, not much else has really been added which would make a discernible difference to the outcome - would you not agree?

In addition, I have also shown how to properly redirect output to a file (http:#37040295) followed by an excellent (don't you agree) mini-write-up focusing on redirection (http:#37049731) which took me over an hour to compose - that's right Steve, "compose" - just as Beethoven composed his 5th symphony.

Yep, you've guessed it Steve, not only is programming an art and therefroe, we are we artists (some better than others dont't forget...) but we (or in this instance, 'I') can also be regarded as composers - often of great works of art! Our art being our DOS batch files. Our canvas being Notepad++.

Let me state at this point, some artists never progress further than just Notepad.

In fact, the word 'Maestro' might be a more proper description, do you not agree? Let's see how it fits:

   dragon-it
   billprew
   Maestro Paul Tomasi
   iSiek
   BillDL
   etc...

Yep. Proper!

And, my mini-write-up, an opus - in this instance Opus No. 37049731.

:)

PS. You can go out to play now!
0
 
Bill PrewCommented:
@stonneway

There has been a lot of extra information and discussion toward the end of this question, just wanted to touch base with you and see if there was anything that you still needed help or clarification with.

~bp
0
 
Steve KnightIT ConsultancyCommented:
agreed... The top 5-6 posts should do it, rest is just our waffle!
0
 
BillDLCommented:
My apologies to stonneway, Bill, and Steve for cluttering up the question with my superfluous comments.
0
 
Steve KnightIT ConsultancyCommented:
stonneway... are you still there?!
0
 
paultomasiCommented:
stonneway has goneway!
0
 
BillDLCommented:
No wonder. I can't blame him.  He was looking for an answer, got it within the first few comments, and was then subjected to a bunch of unnecessary jabber that amounted to bickering and jesting, and I'm referring to my own silly comments too.  Questions are for serious content with the occasion bit of off-topic banter; the lounge is for chatter and bickering.  As adults we should know better.
0
 
stonnewayAuthor Commented:
Thanks for all the (many) comments. :)
0
 
Steve KnightIT ConsultancyCommented:
Erm yes, we did waffle on a bit - sometimes just can't help responding to "injustice" ;)
0
 
paultomasiCommented:
'Injustice', Steve? Whatever do you mean?...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 7
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now