Skipping particular line from batch file

August 25, 2011 at 14:05:22
Specs: Windows 7
Hello,

I am trying to log off all the users from the server except the user which is running the batch file. Please note that all users are terminal clients. I did some research and found out that Microsoft has a code which does similar work, but not exact.

query session >session.txt
for /f "skip=2 tokens=3," %%i in (session.txt) DO logoff %%i
del session.txt

The above code logs off all the users from a terminal client. The output file "session.txt" has following lines.

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
 services                                    0  Disc                        
 console                                     1  Conn                        
 rdp-tcp#0         Das Patel                 2  Active  rdpwd               
 rdp-tcp#2         Shaily                    3  Active  rdpwd               
 rdp-tcp#3         Ronnie                    4  Active  rdpwd               
>rdp-tcp#4         Administrator             5  Active  rdpwd               
 rdp-tcp#1         Palon                     6  Active  rdpwd               
 rdp-tcp                                 65536  Listen                      

Here, when reading the "session.txt" file, it should skip the line which starts with ">". I came across one code which skips that line, but that code also skips rest of the lines thereafter. I need some code to read the file in such a way that the output will skip the line which starts with ">" and creates a new file.

If we can also skip the line with blank USERNAME, that would be more helpful. But, if it doesn't work its fine.

So, the new file should have following values:

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
 services                                    0  Disc                        
 console                                     1  Conn                        
 rdp-tcp#0         Das Patel                 2  Active  rdpwd               
 rdp-tcp#2         Shaily                    3  Active  rdpwd               
 rdp-tcp#3         Ronnie                    4  Active  rdpwd               
 rdp-tcp#1         Palon                     6  Active  rdpwd               
 rdp-tcp                                 65536  Listen                      

Thanks in advance.


See More: Skipping particular line from batch file

Report •


#1
August 26, 2011 at 16:20:56
Maybe this will help:

@echo off
SetLocal EnableDelayedExpansion
for /f "skip=1 eol=> tokens=1*" %%i in ('query session^|findstr /b /v /r /c:"\ ..................\ "') DO (
  set un=%%j
  for /f "delims=	" %%a in ("!un:  =	!") do logoff %%a
)
pause


Report •
Related Solutions


Ask Question