[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need a program to recursively run a command on dir & subdir

Posted on 2010-01-07
14
Medium Priority
?
471 Views
Last Modified: 2012-05-08
I need a windows/dos based program to run a exe I have on files within a dir. Can somebody point the easiest way to do that?
0
Comment
Question by:rheasam
  • 7
  • 5
  • 2
14 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 750 total points
ID: 26204956
Easiest is to use a for command.  You can get a dir command's output and use it to run it on each of those, o use the /r option to recurse down all files, or /d to show all files in a dir.  I find it easiest to use a dir command, e.g.

@Echo off
for /f "tokens=*" %%a in ('dir /b /a-d /s c:\startdir\*.*') do (
 echo Dir is %%a to use in your command
)

You can do different dir commands, e.g.

dir /b /ad /s   does directories only not files, the one above does all files in all subdirs.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26204962
for /? has lots of info. on the options btw.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26204978
And I should explain.. save that as whatever.cmd, or you can run the for command from a cmd.exe prompt but hen you have to remove one of the % signs to make each %%a %a, i.e.

for /f "tokens=*" %a in ('dir /b /a-d /s c:\startdir\*.*') do echo Dir is %a to use in your command
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:ahmad2121
ID: 26205072
\ is the directory to work in
change dir to the exe filename you want to execute

example: if you run the script from the C:\ root, then

FOR /D /r %%G in ("messthesefiles") DO c:\progra~1\myapp\myapp.exe "%%~pG"

will run your app and supply it with arguments from every folder inside "mess these files". You can use wildcards too.

FOR /D /r %%G in ("user_*") DO c:\progra~1\myapp\myapp.exe "%%~pG"
FOR /D /r %%G in (\) DO dir "%%~pG"

Open in new window

0
 

Author Comment

by:rheasam
ID: 26205115
Ok,, I am little confused. I need to run touch.exe from C:\temp.
What exactly shou;d be the command. I need to recursively run this on all files within temp.
0
 
LVL 6

Assisted Solution

by:ahmad2121
ahmad2121 earned 750 total points
ID: 26205132
create a bat file (recurse.bat for example) inside the temp folder, copy the line below into it, save and run.

This will provide touch.exe with every folder under temp.

Also I made a mistake in my previous script. For some reason you cannot specify a path it just runs through every folder. Don't worry about that though, just do this for now.
FOR /D /r %%G in (\) DO touch.exe "%%~pG"

Open in new window

0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 26205161
you might want to do this. will make it easier to read.
@echo off
FOR /D /r %%G in ("asdfasdfasdf") DO (
	echo Touching "%%~pG"...
	dir "%%~pG"
	echo Done
)

Open in new window

0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 26205170
or in your case..
@echo off
FOR /D /r %%G in (\) DO (
        echo Touching "%%~pG"...
        touch.exe "%%~pG"
        echo Done
)

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26205691
@Echo off
for /f "tokens=*" %%a in ('dir /b /a-d /s c:\temp\*.*') do (
 touch "%%a"
)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26205736
or

@echo off
cd /d c:\temp
for /r %%a in (*.*) do touch "%%a"


Steve
0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 26212232
First he says directories, then he says files. My guess is touch.exe takes in directories but manipulates files.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26272537
AFAIK touch.exe normally works on a specific file, possibly can be run on wildcards, i.e. tun touch *.* for each subdir, may even have a "/s" subidrs switch I guess depending on the implementation, as there are many different people's versions.

Steve
0
 

Author Closing Comment

by:rheasam
ID: 31674279
worked partially
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26279538
Why the b grade without any feedback, in what way did it not work?!

Steve
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

830 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