在批处理脚本中将行从文件设置为变量

时间:2016-05-27 14:23:24

标签: batch-file

我正在尝试制作一个批处理文件,该文件将通过计算机名称的文本文件并获取每个登录的用户名。我不知道如何在循环中设置我需要的变量。我已经尝试过我在这里找到的几种解决方案,但它们似乎都没有用。 output.txt包含:

Sub getSubstrings()
    Dim str1 As String
    Dim str2 As String
    Dim char As String
    Dim n As Integer
    Dim x1 As Integer
    Dim x2 As Integer

    str1 = Cells(1, 1).Value 'A1
    str2 = Cells(1, 2).Value 'B1

    'Situation 1
    For i = Len(str1) To 1 Step -1
        char = Mid(str1, i, 1)
        If Not IsNumeric(char) And char <> "," Then
            n = i
            Exit For
        End If
    Next
    Cells(1, 3).Value = Right(str1, Len(str1) - n) 'write answer to C1

    'Situation 2
    x1 = 0
    x2 = 0
    For i = 1 To Len(str2)
        char = Mid(str2, i, 1)
        If Not IsNumeric(char) And char <> "," Then
            If x1 = 0 Then
                x1 = i  'set first index
            Else
                x2 = i  'set second
                Exit For
            End If
        End If
    Next
    Cells(1, 4).Value = Mid(str2, x1 + 1, x2 - x1 - 1)
End Sub

这些是我试图传递的计算机名称。当我PCHTW-HTT10425 PCHTW-HTT10437 时它给出了名字,但设置名称为%% A什么也没做。阅读它我认为在echo %%A添加会做的伎俩,但似乎没有做任何事情。我也试过SETLOCAL EnableDelayedExpansion,但它也没有用。

echo name:!name!

2 个答案:

答案 0 :(得分:0)

您的问题如何获取记录用户的计算机列表

我的正确答案如何不回答这个问题。矿井是一条线。

wmic /output:"c:\Outputfile.html" /node:@"COMPUTERLIST.TXT" computersystem get username /format:htable

在节点前放置@符号表示节点包含计算机名或IP地址的文本文件(您可以混合使用)。

请参阅wmic /?wmic /node /?wmic /output /?wmic /append /?wmic computersystem /?wmic computersystem get /?wmic computersystem call /?,{{1} },wmic computersystem set /?

获取当前启用的计算机列表

wmic /format /?

并且没有按照wmic的要求for /f "skip=3" %A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %A

\\

答案 1 :(得分:0)

setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (%FILEPATH%output.txt) do (
  echo %%A
  FOR /F "tokens=2 delims=\" %%F IN ('wmic /NODE:"%%A" computersystem get UserName') DO (
    for /f "delims=" %%B in ("%%F") do ( 
      echo %%B
      schtasks /create /ru %%B ...
    )
  )
  pause
)

注意:wmic具有丑陋的行结尾(额外的回车符),因此还有一些代码可以摆脱它。我使用了dbenham的one of several possibilities

相关问题