我正在尝试根据第一个令牌包含的内容为文件写一个不同的输出,因为它们可能不同,这可能吗?
我根据跨多个表的查询会话命令的结果写出一个表,这给我留下了如下输出
Server 1
User1 2 Disc
Server 2
User1 1 Disc
rdp-tcp#0 User2 4 Active rdpwd
Server 3 Offline
Server 4
User1 8 Disc
User2 9 Disc
User3 10 Disc
我目前要编写的代码片段是:
>>%2 echo ^<table class="tftable" border="1"^>
for /f "tokens=1,2,3,4 delims=" %%a in (%1) do (
>>%2 echo ^<tr^>^<td^>%%a^</td^>^<td^>%%b^</td^>^<td^>%%c^</td^>^<td^>%%d^</td^>^</tr^>
)
>>%2 echo ^</table^>
我想使用IF Else语句根据字符串是否以字符串&#39; Server&#39;开头来调整我回显到文件的表行。或者&#39; rdp&#39;因为表中我想要的唯一输出是服务器名称,下面是会话的用户名和状态。
答案 0 :(得分:0)
@echo off
setlocal
echo ^<table class="tftable" border="1"^>
set "server="
for /f "delims=" %%A in (currentsessions.txt) do call :rawline "%%~A"
echo ^</table^>
:rawline
setlocal
set "line=%~1"
if not defined line exit /b
set "t1=%line:~0,18%"
call :trim t1 "%t1%"
if not defined t1 set "t1=%server%"
if "%line:~19,18%" == "" endlocal & set "server=%t1%" & exit /b
set "td=<tr><td>%t1%</td>"
call set /p "=%%td%%" <nul
if not "%t1: Offline=%" == "%t1%" (echo ^</tr^>) & exit /b
set "t2=%line:~19,18%"
call :trim t2 "%t2%"
set "td=<td>%t2%</td>"
call set /p "=%%td%%" <nul
set "t3=%line:~38,2%"
call :trim t3 "%t3%"
set "td=<td>%t3%</td>"
call set /p "=%%td%%" <nul
set "t4=%line:~42,7%"
call :trim t4 "%t4%"
set "td=<td>%t4%</td>"
call set /p "=%%td%%" <nul
set "t5=%line:~50%"
call :trim t5 "%t5%"
set "td=<td>%t5%</td></tr>"
call set /p "=%%td%%" <nul
echo.
if defined t1 endlocal & set "server=%t1%"
exit /b
:trim
setlocal
set "line=%~2"
:ltrim
if not defined line endlocal & set "%~1=" & exit /b
if "%line:~,1%" == " " set "line=%line:~1%" & goto :ltrim
:rtrim
if not defined line endlocal & set "%~1=" & exit /b
if "%line:~-1%" == " " set "line=%line:~0,-1%" & goto :rtrim
endlocal & set "%~1=%line%"
exit /b
处理原始读取线,以便可以进行位置分隔。
位置处理由即
完成set "t1=%line:~0,18%"
从字符0开始,得到以下18个字符。
使用对:trim
的调用从数据中删除空格。
call set /p "=..." <nul
行将数据打印到控制台而不用
换行符,以便下一个数据项可以附加在同一行。
server
变量仅在需要时更新,即
Server 4
使用相同的服务器3次,因此需要保留
需要最后一个已知的服务器才能完成以下行。