Computing.Net > Forums > Programming > CMD and FOR statement

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

CMD and FOR statement

Reply to Message Icon

Name: joebagodoe
Date: January 25, 2005 at 06:38:11 Pacific
OS: Win2k SP4
CPU/Ram: 800Mhz/128M
Comment:

When executed as a batch file, why does this not work:
---------------
attrib "c:\Documents and Settings\All Users" +h
attrib "c:\Documents and Settings\svinyard" +h
attrib "C:\Documents and Settings\svinyard.LIBERTY" +h
"dir "c:\Documents and Settings" /B > c:\NTUser.txt"
for /f "delims=," %I IN (c:\NTUser.txt) DO rd /s/q "c:\Documents and Settings\%I"
for /f "delims=," %I IN (c:\NTUser.txt) DO rd /s/q "c:\Documents and Settings\%I"
for /f "delims=," %I IN (c:\NTUser.txt) DO rd /s/q "c:\Documents and Settings\%I"
for /f "delims=," %I IN (c:\NTUser.txt) DO rd /s/q "c:\Documents and Settings\%I"
for /f "delims=," %I IN (c:\NTUser.txt) DO rd /s/q "c:\Documents and Settings\%I"
del /f/q c:\NTUser.txt
attrib "c:\Documents and Settings\All Users" -h
attrib "c:\Documents and Settings\svinyard" -h
attrib "C:\Documents and Settings\svinyard.LIBERTY" -h
---------------

The output I get is:

---------------
Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

H:\>\\Sh-nas\install\SCRIPTS\Printer_Scripts\Remove_local_users.bat

H:\>attrib "c:\Documents and Settings\All Users" +h

H:\>attrib "c:\Documents and Settings\svinyard" +h

H:\>attrib "C:\Documents and Settings\svinyard.LIBERTY" +h
File not found - C:\Documents and Settings\svinyard.LIBERTY

H:\>"dir "c:\Documents and Settings" /B > c:\NTUser.txt"
The filename, directory name, or volume label syntax is incorrect.
\NTUser.txt) was unexpected at this time.

H:\>for /f "delims=," \NTUser.txt) DO rd /s/q "c:\Documents and Settings\I"

H:\>
---------------

Yet, if I paste it to a cmd window, all works fine. One more reason for me to learn a new scripting language!



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: January 25, 2005 at 07:47:54 Pacific
Reply:

The following line should not be enclosed by "

"dir "c:\Documents and Settings" /B > c:\NTUser.txt"

so replace it with

Dir "c:\Documents and Settings" /B > c:\NTUser.txt

The effect is to miss the C:\NTUser.txt file dooming the balance of code.

I hope you begin your script coding

@Echo Off

otherwise you get each line echoed to the screen with a cumbersome effect.



0

Response Number 2
Name: Mechanix2Go
Date: January 25, 2005 at 08:14:14 Pacific
Reply:

This is pretty hard to read but if the intent of the first three lines is to hide directories, don't you need ?:

attrib /d /s +h

M2


0

Response Number 3
Name: joebagodoe
Date: January 25, 2005 at 09:54:22 Pacific
Reply:

The attrib to hide the lines is so when DIR /B outputs to txt, it doesn't output hidden drectories, therefore not allowing the script to delete the "all users" folder and my profile. I simply needed to hide the root of that folder, and not the subfolders.

Actually, I have not been running the script with echo off, due to ignorance, but adding it does not seem to correct the issue. This is a very strange problem.

I also removed the quotes, but this had no effect.


0

Response Number 4
Name: Mechanix2Go
Date: January 25, 2005 at 10:01:29 Pacific
Reply:

Well, first things first.

Does your first line hide the directory or not?

M2


0

Response Number 5
Name: IVO
Date: January 25, 2005 at 12:34:28 Pacific
Reply:

Sorry for my previous post, but now I am quite sure to nail the error. It is due to the %I variable in the For statements as that works fine from the command prompt, but the same must be entered as %%I if embedded in a batch script. This explains the For statement failure.


0

Related Posts

See More



Response Number 6
Name: joebagodoe
Date: January 26, 2005 at 08:39:52 Pacific
Reply:

It would seem, sir, that you are the most knowledgeable programmer I have met thus far. Your suggestion corrected the issue and everything works perfectly.

Thank you


0

Sponsored Link
Ads by Google
Reply to Message Icon






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: CMD and FOR statement

Help creating batch file... www.computing.net/answers/programming/help-creating-batch-file/15530.html

New line using batch files in .txt www.computing.net/answers/programming/new-line-using-batch-files-in-txt/16946.html

Help with a batch file and if statements www.computing.net/answers/programming/help-with-a-batch-file-and-if-statements/19128.html