Computing.Net > Forums > Programming > for loop problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

for loop problem

Reply to Message Icon

Name: uli_glueck
Date: December 19, 2006 at 00:37:27 Pacific
OS: NT4.0
CPU/Ram: PIII 512 MB
Product: Siemens
Comment:


I want to get the foldername of the profiles on a pc in a variable to check if there exist a specific folder.
The problem is I get spaces at the beginning and the end of the var like:

" c:\winnt\profiles\all users "
" c:\winnt\profiles\uli "
...

That means I can't work with it.
Deleting all spaces is not possible cause of the all users folder.

Here is my not really working code:

@echo off
cls

FOR /d %%A in (c:\winnt\Profiles\*) do call :sub1 %%A

goto :eof
:sub1

set PROFIL=%*
echo "%PROFIL%"

:eof

thanks in advance for any help
uli



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: December 19, 2006 at 02:26:39 Pacific
Reply:

Hi uli,

not sure why you get spaces. My structure is different, but this works:

@echo off
for /f "tokens=* delims=" %%A in ('dir /s/b/ad "\Documents and Settings\all*"') do (
echo %%A
)



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: uli_glueck
Date: December 19, 2006 at 05:51:45 Pacific
Reply:

Thanks for your quick help M2,

your solution works perfect as long as I don't call a procedur.
Then it is nearly the same effect.

" foldername"

Seems to be a Bug in NT4.
I want it in a procedur cause I delete a folder in every profile and want to analyze an errorcode for a logfile.

uli


0

Response Number 3
Name: Mechanix2Go
Date: December 19, 2006 at 06:17:12 Pacific
Reply:

Hi uli,

Post your code.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: uli_glueck
Date: December 19, 2006 at 06:28:45 Pacific
Reply:

M2, you pointed me in the right direction: :-)
This seems to be the solution:

for /f "tokens=* delims=" %%A in ('dir /s/b/ad "c:\winnt\Profiles\.folder"') do call :sub1 %%A

goto :eof
:sub1

set folder=%*
set folder=%folder:~1%
rd /s /q "%folder%"
if "%errorlevel%" == "0" (echo %folder% erased >>log.txt)


0

Response Number 5
Name: Mechanix2Go
Date: December 19, 2006 at 06:41:52 Pacific
Reply:

Hi uli,

Yeah, we're left wondering if it's an NT4 bug.

NOTES:

When testing for errorlevel you can skip the == and simply use:

if errorlevel X do

More important, in your case, note that an errorlevel test will resolve TRUE if the errorlevel is EQUAL OR MORE. So a test for errorlevel 0 will always be true.

What you probably want here is:

if not errorlevel 1 echo it worked


=====================================
If at first you don't succeed, you're about average.

M2



0

Related Posts

See More



Response Number 6
Name: uli_glueck
Date: December 20, 2006 at 05:28:15 Pacific
Reply:

Hi M2,

thanks for the errorlevel advice. Helped to avoid one of these little "uli" bugs in my script. I am a bit in a hurry cause of this project. (The profile thing is just a little part...)

uli


0
Reply to Message Icon

batch programming questio... Searching file w/ C++



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: for loop problem

DOS - For Loop Problem www.computing.net/answers/programming/dos-for-loop-problem/17488.html

For loop problem again www.computing.net/answers/programming/for-loop-problem-again/18249.html

Batch FOR Loop %ERRORLEVEL% www.computing.net/answers/programming/batch-for-loop-errorlevel/13528.html