简体   繁体   中英

Removing unwanted spaces from WMIC output

In my user management batch file for any computer it generates a list of users that a user has input as authorized and compares it to a list of all users

To accomplish this I append users to the end of a file and then use

"Wmic useraccount where "localaccount=true" get name > AllUsers.txt" 

only problem with this is a get an output similar to

    "user1            "
    "user2            "
    "UnAuthorizedUser "  

without the quotes but the space is still there, This is a problem because I use findstr to compare the all users to auth users

    findstr /vig:AuthUsers.txt AllUsers.txt > UnauthorizedUsers.txt

this should output something similar to

    "UnAuthorizedUser"

given that user1 and user2 are authorized. the problem is that it outputs blank text because the authorized users do not have the spaces that the "AllUsers.txt" contains so its seeing what should be "user1" as "user1______" (pretend that the underscores are space), therefore it outputs a blank.

My question with all of this is simply, is there a command that can get rid of a space at the end of a word on multiple lines of a text file? or better yet make findstr ignore the spaces all together?

Your problem is that WMIC output is unicode and your match file is ascii. So you need some way to make the WMIC output be normal ascii. You can do this by using another FOR command which will help translate the characters into single byte.

@echo off

(FOR /F "skip=1 delims=" %%G IN ('wmic UserAccount where "LocalAccount=True" get Name') DO (
    for /f "delims=" %%H in ("%%G") do @echo %%H |findstr /VIG:AuthUsers.txt
))>UnauthorizedUsers.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM