windows - Issues with spaces in FOR loop variable - batch script -


i'm writing batch script use wmic command list of groups on windows machine, group info using net localgroup <groupname>, , write info output file. here have:

for /f "skip=1" %%a in ('"wmic group name"') net localgroup %%a  >> "%outputfilepath%" 2> nul && echo.  >> "%outputfilepath%" && echo ^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=  >> "%outputfilepath%" && echo. >> "%outputfilepath%" 

the issue having seems getting quotes around %%a variable in do net localgroup %%a because outputs info groups administrators fine when gets group name remote desktop users, fails return info group.

in other words, seems happening in loop:

net localgroup administrators net localgroup remote desktop administrators 

the first operation successful. second not. obviously, there need quotes around remote desktop administrators in order seen single argument can't quite figure out how happen %%a loop variable.

i have tried putting quotes around "%%a" , '%%a'. doesn't help.

any input appreciated.

rem

@echo off setlocal enabledelayedexpansion  set "outputfilepath=u:\ofp.txt" del  /f /q "%outputfilepath%" /f "skip=1delims=" %%a in ('wmic group name') (  set "group=%%a"  call :loptrail  if defined group (   net localgroup "!group!"  >> "%outputfilepath%" 2> nul   echo.>> "%outputfilepath%"   echo ==========================================>> "%outputfilepath%"   echo.>> "%outputfilepath%"  ) )  goto :eof  :loptrail set "group=%group:~0,-1%" if "%group:~-1%"==" " goto loptrail goto :eof 

the issue wmic output unicode , %%a requires delims= else returns first [implicit-space]-delimited token.

unfortunately, means %%a contains trailing spaces, net appears disapprove of.

since batch not allow metavariable substringed, need put common environment variable , invoke enabledelayedexpansion.

the value in group appears terminated null, :loptrail routine first arbitrarily removes last character, remaining trailing spaces.

the last line of wmic output generate forlorn empty group net processing proceeds if group non-empty.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -