【问题标题】:Set file name with * as variable batch script将带 * 的文件名设置为变量批处理脚本
【发布时间】:2013-10-29 21:18:24
【问题描述】:

我有一个批处理文件,我遇到了问题。我需要找到一个文件的名称,然后将其设置为一个变量。然后我将使用它来将它传递给 vbs 脚本以进一步查看该文件。该文件的名称是 logfile_date_time.log,但时间会因开始时间而异。批处理文件的重点是找出这个文件的最后修改日期。

set fordate=%date:~4,2%%date:~7,2%%date:~10,4%

set filename=c:\logfile_%fordate%_*.log

if exist %filename% (goto exist) else (goto noexist)

:exist

vbsscript.vbs /file:%filename%

goto end

:noexist

file doesn't exist code blah blah

:end

pause

出于安全考虑,我不得不修改文件夹的名称并删除一些代码,因为这是为了工作。

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

【问题讨论】:

    标签: variables batch-file vbscript


    【解决方案1】:

    未测试:

    set "last_modified="
    for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
     do set "last_modified=%%~dpfnxf"
     goto :break_loop
    )
    :break_loop
    
    if defined last_modified echo file %last_modified% exist ...
    

    【讨论】:

      【解决方案2】:

      您的代码的问题在于它没有扩展通配符 (*),而且您的 VBScript 本身可能不处理文件名中的通配符(例如 FileSystemObject 方法不处理)。如果您要处理的文件是唯一与您的模式匹配的文件,您可以执行以下操作:

      @echo off
      
      setlocal
      
      set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
      
      pushd C:\
      for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
      popd
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        相关资源
        最近更新 更多