【问题标题】:How to get the modified time of a remote file in batch script如何在批处理脚本中获取远程文件的修改时间
【发布时间】:2016-01-26 21:52:06
【问题描述】:

我正在尝试使用批处理脚本获取远程 Windows 服务器 2008/2012 上日志文件的最后修改日期。 我正在使用“net use”连接到机器,并且能够查看文件是否存在。

net use \\X.X.X.X /user:%USERNAME% %PASSWORD%
if exist "\\X.X.X.X\\C$\\Temp\\LogFiles\\abcd" (
    echo ABCD file exists on the server
) else (
    echo ABCD file does NOT exist on the server
)

另外,我可以使用 forfiles 获取本地文件的最后更新时间:

for /f "delims=" %%i in ('"forfiles /m MyLocalAbcd /c "cmd /c echo @file was last modified at @ftime" "') do set modif_time=%%i
echo %modif_time%

但是,我无法获取远程文件的修改时间。我试图提供完整的路径 - forfiles /M "\X.X.X.X\C$\Temp\LogFiles\abcd" - 甚至提供了 forfiles 选项 P 的路径,但它没有找到文件。

有没有一种简单的方法来获取远程文件的修改日期/时间? 另外,我想知道是否有办法使用 Windows 内置命令跟踪同一文件的最后 n 行。

任何帮助表示赞赏! 谢谢!

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

    试试dir

    或者以不同的方式使用 for 循环。

    for %%A in (\\server\C$\Temp\LogFiles\abcd) do echo %%~tA
    

    粘贴到命令提示符后的工作示例。

    for %A in (\\127.0.0.1\C$\windows\win.ini) do echo %~tA
    

    你偷偷问的另一个问题

    more +50 跳过前 50 行。

    这个 VBScript 做你想做的事。

    Set Arg = WScript.Arguments
    Set WshShell = createObject("Wscript.Shell")
    Set Inp = WScript.Stdin
    Set Outp = Wscript.Stdout
    Set rs = CreateObject("ADODB.Recordset")
    With rs
        .Fields.Append "LineNumber", 4 
    
        .Fields.Append "Txt", 201, 5000 
        .Open
        LineCount = 0
        Do Until Inp.AtEndOfStream
            LineCount = LineCount + 1
            .AddNew
            .Fields("LineNumber").value = LineCount
            .Fields("Txt").value = Inp.readline
            .UpDate
        Loop
    
        .Sort = "LineNumber ASC"
    
        If LCase(Arg(1)) = "t" then
            If LCase(Arg(2)) = "i" then
                .filter = "LineNumber < " & LCase(Arg(3)) + 1
            ElseIf LCase(Arg(2)) = "x" then
                .filter = "LineNumber > " & LCase(Arg(3))
            End If
        ElseIf LCase(Arg(1)) = "b" then
            If LCase(Arg(2)) = "i" then
                .filter = "LineNumber > " & LineCount - LCase(Arg(3))
            ElseIf LCase(Arg(2)) = "x" then
                .filter = "LineNumber < " & LineCount - LCase(Arg(3)) + 1
            End If
        End If
    
        Do While not .EOF
            Outp.writeline .Fields("Txt").Value
    
            .MoveNext
        Loop
    End With
    

    使用

    过滤器仅读取和写入标准输入和标准输出。这些仅在命令提示符下可用。

    filter <inputfile >outputfile
    filter <inputfile | other_command
    other_command | filter >outputfile
    other_command | filter | other_command
    

    剪切

    filter cut {t|b} {i|x} NumOfLines
    

    从文件的顶部或底部减少行数。

    t - top of the file
    b - bottom of the file
    i - include n lines
    x - exclude n lines
    

    示例

    filter cut t i 5 < "%systemroot%\win.ini"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 2012-02-27
      • 2017-12-09
      • 2014-04-14
      • 2011-08-06
      相关资源
      最近更新 更多