Variables 将带有*的文件名设置为变量批处理脚本
我有一个批处理文件存在问题。我需要找到文件名,然后将其设置为变量。然后,我将使用它将其传递到vbs脚本以进一步查看该文件。文件名为logfile\u date\u time.log,但时间根据开始时间的不同而不同。批处理文件的要点是找出此文件的最后修改日期Variables 将带有*的文件名设置为变量批处理脚本,variables,batch-file,vbscript,Variables,Batch File,Vbscript,我有一个批处理文件存在问题。我需要找到文件名,然后将其设置为变量。然后,我将使用它将其传递到vbs脚本以进一步查看该文件。文件名为logfile\u date\u time.log,但时间根据开始时间的不同而不同。批处理文件的要点是找出此文件的最后修改日期 set fordate=%date:~4,2%%date:~7,2%%date:~10,4% set filename=c:\logfile_%fordate%_*.log if exist %filename% (goto exist)
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
为了安全起见,我不得不修改文件夹的名称并删除一些代码,因为这是为了工作
谢谢你的帮助。谢谢 未测试:
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 ...
代码的问题在于它没有扩展通配符(
*
),并且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