Click here for important information about

Why does my code it do it twice?

June 20, 2010 at 15:02:50
Specs: Windows 2003
Hello guys

I'm trying to make a batch, the objective is to run several .exe (patches) that are saved on different folders, all folders named MSXXXXXXX

The folder structure is:

- root (from where the bat will run
------file 2
and so on

This is what i have so far:
@Echo off

set actdir=%~dp0%
echo Running from %actdir%

for /f %%i in ('dir MS* /a:D /b "%actdir%"') do (
set sub=%%i
call :infold "!actdir!" "!sub!"

Echo Done!

for /f "tokens=* delims= " %%a in ('dir /s /b "%~1%~2"') do (
Echo Installing %%a
"%%a" /quiet /noreboot
================END CODE============

The problem i have is that
1. It starts fine and goes trough all the folders named MSXXXX as it is supposed to but after it is done with all folders it starts with the ones named differently.
2. After it finished with the folders i dont want, it starts going trough the MSXXXX folders again, when its done it finally finishes.

I cant understand why does it do it twice and why it goes over the folder i don't care for. Maybe you guys can find where i screw up?

Thank you!

P.S. I've seen you guys doing whole batches in a sinlge line of code, if you have one of those magic one-liners for this i'll appreciate very much.

See More: Why does my code it do it twice?

June 20, 2010 at 15:15:33
I think this is the main error:

dir MS* /a:D /b "%actdir%"

Your actually asking dir to list two sets of things:

[1] Folders that start with "MS"
[2] The whole directory of "%actdir%"

You should be asking dir for this:

dir /a:D /b "%actdir%ms*"

Report •

June 20, 2010 at 15:22:08

you're good

that actually did the trick. Thank you very much!

Report •

June 20, 2010 at 15:45:21
No problem!

You can actually make that script a lot smaller without passing the variables around:

@Echo off

echo Running from "%~dp0"

for /d %%i in ("%~dp0ms*") do (
    for %%j in ("%%~i\*") do (
        Echo Installing "%%~nxj"
        "%%~j" /quiet /noreboot

Echo Done!
goto :eof

Report •

Related Solutions

June 20, 2010 at 16:10:38
Awesome! Thanks again!

I tried not using the function but couldn't get the variables to work correctly, you seem to have done it quite easily

There is something i don't understand though. What does " %%~nxj " do?

Report •

June 20, 2010 at 16:29:59
"n" and "x" are modifiers, much like "d" and "p" in "%~dp0". "%%~nxj" simply expands to the filename with extension.

For the list of meanings execute "for /?" from the command line or check out

There are also lots of other web pages with the info as well.

Report •

June 21, 2010 at 09:31:58
Thank you for the explanation and reference, very interesting website, is it yours? It is on my bookmarks now ;)

by the way, if its really yours, probably a typo in the "under-title", it says : Juago's site

Report •

June 21, 2010 at 17:57:58
Yeah the site is mine, thanks pointing out the typo. I must have glazed over it a hundred times by now ;).

Report •

Ask Question