Strip text from output

May 8, 2009 at 12:01:06
Specs: Windows XP
Below is a snippet of code that I am using to identify all the local user account on a system

The output of this command always includes "The command completed successfully." Is there a way to strip this syntax from the output?

@echo off
FOR /F "skip=4 tokens=*" %%A IN ('NET USER ^| FIND /V "The command completed successfully"') DO CALL :ParseUsers %%A
GOTO:EOF

:ParseUsers
FOR %%? IN (%*) DO CALL :ChkAcc %%?
GOTO:EOF

@echo off
:ChkAcc
NET USER %1
ECHO.%1
GOTO:EOF


See More: Strip text from output

Report •


#1
May 8, 2009 at 13:36:28
:ChkAcc
  Net User %1 | find /V "The command completed successfully"
  echo.%1
goto :EOF


Report •

#2
May 8, 2009 at 20:17:32
Thanks your code worked.

Can I chain several of the find /v command together.

for example:
:ChkAcc
Net User %1 | find /V "The command completed successfully" ^| find /V "do not show" ^| find /v "do not show too"
echo.%1
goto :EOF


Report •

#3
May 9, 2009 at 02:55:56
Yes, but chain is accomplished by just the pjpe symbol | not ^|.

The caret (^)must prefix a redirector when coded inside the FOR clause not in an usual batch statement, so code

Net User %1 | find /V "The command completed successfully" | find /V "do not show" | find /V "do not show too"


Report •

Related Solutions

#4
May 9, 2009 at 18:01:12
I had already tested | and ^| before posting.

After some testing I found out that the error message that I wanted to remove was being produced only for a certain account. This account contains a space (e.g. myTest User), which throws an error when called via net user.

Is there any way to test for this error in the code below:
error messages:
"The user name could not be found."
"More help is available by typing NET HELPMSG 2221"

Also the code below is producing another error, which is that it query an account name (e.g. myAccountName) twice. The first time it returns all the account details, while the second time it only returns the name without the account details. Any idea on this one?

@echo off
FOR /F "skip=4 tokens=*" %%A IN ('NET USER ^| FIND /V "The command completed successfully"') DO CALL :ParseUsers %%A
GOTO:EOF

@echo off
:ParseUsers
FOR %%? IN (%*) DO CALL :ChkAcc %%?
GOTO:EOF

:ChkAcc
Net User %1 | find /V "The command completed successfully"
echo.%1
goto :EOF

Thanks in advance for any support.


Report •

#5
May 10, 2009 at 03:47:37

Let me begin from your last question, i.e. the supposed twice query. Actually the query is performed just one time by Net User, the second display is produced by the echo command. So modify as below

:ChkAcc 
  Net User "%*" | find /V "The command completed successfully" 
goto :EOF

As you can see I embraced the parameter by ". You have to do this when referring to strings as "myTest User". The error you face is due to bad parsing of the Net User command with a username containing spaces.

Your problem however is still present because the code you are running is not adequate to parse a user list with usernames with spaces and to modify that requires know-how of scripting I think is far beyond your capabilities.


Report •

#6
May 10, 2009 at 08:10:37
Your right, I need to brush up on my batch scripting skills, which I rarely use.

I did some more research after your last post and learned that the error being thrown is caused by a known bug in how net user handles an account name that contains a space or other characters (e.g. ^).

This command produces the error:
net user MyUser Name

This command does not produce the error:
net user "MyUser Name"


Report •


Ask Question