1.

Solve : Multiple commands once per directory?

Answer» HEY all,

I have multiple commands I want to execute. I want to execute these commands once for every directory, starting at the location of the batch file, and walking down the directory tree. It's important that all commands complete for a sole directory before moving on to the next. This, essentially creating a loop at each new directory. Hope that makes sense.

For simplicity's sake, lets just call the commands; command a and command b

Thanks for the help.
@echo off
for /f "tokens=1*" %%a in ('dir /a:d ..\ /b /s') do (
cd "%%a %%b"
attrib +s +h
)



use for /f , the above when put in a batch file will use for /f and dir
to search out all directorys starting at the drive root ..\ , the loop uses cd to change to the current directory, then uses attrib to add attributes to everything in that folder, the folder path is stored as a variable , %%a and %%b , %%a %%b combined is the full folder path, if you use %%a or %%b individually you will not get the folder path, for example %%a=C:\documents %%a %%b=C:\document and settings\program files , so for this reason its important to add quotations around %%a %%b so that the full path will be used in certain commands ,its also important to notice that "%%a %%b" doesnt include the final slash needed, so if you WANTED to copy a file .. copy "%%a %%b\file.txt"Thanks diablo416.

That solved my problem and I learned a couple of tricks in the process. Thanks again.Crap, ran into a problem USING the code. Here's a snippet.
Code: [Select]@echo off
setlocal enabledelayedexpansion

for /f "tokens=1*" %%a in ('dir /a:d /s /b') do (
cd "%%a %%b"
set group.counter=1
: group.loop
for /f "tokens=!group.counter! delims=," %%c in ("%group.string%") do set group.name=%%c
)

pause
It produces this:
Code: [Select]!group.counter! delims=," was unexpected at this timeMultiple times depending on how many sub-directories there are, which is expected.
What's wrong?Well I solved the problem by putting quotes around the [tokens=] in the second [for /f] command.

I don't understand why they would have to be there, if the [group.counter] variable is always a value containing no spaces....continuing the conversation with myself

The quotes around [tokens=] did not work after all, tokens simply got set to [""], der

It seems that no matter what I do, a variable that is set within the first [for /f], cannot be called within the second.

Code: [Select]for /f "tokens=1*" %%a in ('dir /a:d /s /b') do (
set var=1
for /f "tokens=!var! delims=," %%c in (%some.var%) do set group.name=%%c
)
If I use [!var!] then I get:Code: [Select]!var! delims=," was unexpected at this time It doesn't seem the [!!] are processed.

If I use [%var%] then I get:Code: [Select] delims=," was unexpected at this time Note the leading space. The variable value was not transferred, leaving an empty [tokens=], hence that error.

Can someone please try this, or tell me why this does not work?

Thanksk i fixed the errors , but i cant figure out what this batch is suppost to do?
there must be more to it then this?


@echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=," %%a in ('dir /a:d /s /b') do (
cd "%%a %%b"
call set group.counter=1
for /f "tokens=%!group.counter!%*" %%c in ("%group.string%") do set group.name=%%c
echo %group.string%
)
pause
Thanks again diablo416

There is more to the batch, much more, but there is no need to add confusion. The problem exists in this small snippet of code, which I simplified slightly in my last post. Try running the code below, both the WAY it is, and what you had DONE when you made [!var!] --> [%!var!%]. They both return errors. The task here to accomplish seems simple; set a variable within the first [for /f], then call it within the second [for /f]. If you leave echo on, you can see the variable is never transferred into the second [for /f]. I don't know what I'm doing wrong or if it just does not ever work.

Code: [Select]for /f "tokens=1*" %%a in ('dir /a:d /s /b') do (
set var=1
for /f "tokens=!var! delims=," %%c in (%some.var%) do set group.name=%%c
)This, on the other hand, works fine. Take the variable outside of the [for /f] and it works. It's not where I need it however. I've decided this cannot work, unless someone can tell me otherwise.

Code: [Select]setlocal enabledelayedexpansion

set var=1
for /f "tokens=1*" %%a in ('dir /a:d /s /b') do (
for /f "tokens=%var% delims=," %%c in (%some.var%) do set group.name=%%c
)

pause


Discussion

No Comment Found