【问题标题】:IF statement in cmd, how to use the current time to show an errorcmd中的if语句,如何使用当前时间显示错误
【发布时间】:2014-06-10 06:38:17
【问题描述】:

我正在为学校系统编写批处理文件中的代码,该代码显示学生何时更改了他/她的密码。 我的系统有问题,它显示所有学生用户名都已更改密码,但有些用户显示当前时间和日期。 我正在尝试做一个 if 语句说:如果用户的日期和时间 == 当天的日期和时间。显示回显消息。

我已经尝试了很多代码来工作,但它们似乎并不正确。

这是其中一个代码:

(for /f "delims=" %%i in ('net user %MyName% /domain ^| find /I "password last set"') do set MyResult=User 
%MyName%: %%i)

if "%DATE%-%TIME%" EQU "%MyResult%" echo "password not changed."

如果这段代码在完美的 vbs 中也将非常有用。

提前谢谢你。 :)

【问题讨论】:

  • 您在for 中获得%MyName%,但在if 中查看%MyResult%。错字?
  • net user 的输出是18.03.2014 19:59:36(日期和时间之间的空格)(至少在我的计算机上)。你检查%DATE%-%TIME%(日期和时间之间的破折号)

标签: batch-file vbscript cmd


【解决方案1】:

这是为您准备的 VBScript。由于在net user 返回时时间可能会略有变化并且您有机会对其进行测试,因此此脚本会检查它是否在当前日期和时间的 10 秒内。随意调整您认为合适的阈值。

' Create the command string...
strCommand = "cmd /c net user " & strUser & " /domain | find ""Password last set"""

' Run the command and capture its output...
strOut = CreateObject("WScript.Shell").Exec(strCommand).StdOut.ReadAll

' Remove the label ("Password last set") leaving only the date and time...
strOut = Trim(Mid(strOut, Len("Password last set") + 1))

If IsDate(strOut) Then
    If Abs(DateDiff("s", strOut, Date)) < 10 Then

        ' Password set within last 10 seconds

    End If
End If    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2018-08-25
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2017-04-28
    相关资源
    最近更新 更多