批处理脚本读取包含path-to-dir的文件,并获取最新文件,从文件中读取最后一行并打印

时间:2013-02-07 18:34:55

标签: batch-file

为很长的标题道歉,我想知道是否有人可以帮我解决这个问题。

我要做的是读取一个文件,其中包含10个不同的目录路径,获取该目录中的最新文件并从该文件中读取最后一行。

@echo off
setlocal enableextensions enabledelayedexpansion
set host=%COMPUTERNAME%
echo Host: %host%
for /f "tokens=* delims=" %%I in (C:\temp\servers.txt) do (
    SET /A vidx=!vidx! + 1
    set var!vidx!=%%I
    echo Path-to-File: %%I
    for /f "tokens=* delims=" %%X in ('dir "%%I" /OD /B')  do (
    set newest=%%X
    )
    echo %newest%
    )

这是输出:

Host: Windows7
Path-to-File: \\Windows7\C$\direct\log\direct
ECHO is off.
Path-to-File: \\Windows7\C$\temp
ECHO is off.

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

对不起,我对你的描述感到有些困惑。下面的批处理文件是根据我了解您的要求编写的。

@echo off
setlocal EnableDelayedExpansion
echo Host: %COMPUTERNAME%

rem Process the file with the paths:
for /F "delims=" %%I in (C:\temp\servers.txt) do (
   echo Path-to-File: %%I
   rem Get the newest file in this directory
   for /F "delims=" %%X in ('dir "%%I" /OD /B') do (
      set "newest=%%~X"
   )
   echo Newest-File: !newest!
   rem Read the last line from this file
   for /F "usebackq delims=" %%L in ("!newest!") do (
      set "lastLine=%%L"
   )
   echo Last-Line: !lastLine!
)

我希望它有所帮助...

安东尼奥