Computing.Net > Forums > Programming > for loop problem

for loop problem

Reply to Message Icon

Original Message
Name: uli_glueck
Date: December 19, 2006 at 00:37:27 Pacific
Subject: for loop problem
OS: NT4.0
CPU/Ram: PIII 512 MB
Model/Manufacturer: 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


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: December 19, 2006 at 02:26:39 Pacific
Subject: for loop problem
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 2
Name: uli_glueck
Date: December 19, 2006 at 05:51:45 Pacific
Subject: for loop problem
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: December 19, 2006 at 06:17:12 Pacific
Subject: for loop problem
Reply: (edit)

Hi uli,

Post your code.


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

M2



Report Offensive Follow Up For Removal

Response Number 4
Name: uli_glueck
Date: December 19, 2006 at 06:28:45 Pacific
Subject: for loop problem
Reply: (edit)

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)


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: December 19, 2006 at 06:41:52 Pacific
Subject: for loop problem
Reply: (edit)

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



Report Offensive Follow Up For Removal


Response Number 6
Name: uli_glueck
Date: December 20, 2006 at 05:28:15 Pacific
Subject: for loop problem
Reply: (edit)

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


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: for loop problem

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge