如何查找文件并将文件名分配给变量?
示例: 假设我要在以下位置查找文件。文件名不断变化,获取正确文件的唯一方法是在文件名字符串中查找今天的日期:
<代码> X:\ VoyagerBackups \ PickupLocation \ tsiuiolou_live_Full_PCV048DB42_201812140000.Lts.bak
这是我到目前为止在.bat文件中拥有的内容。如果我对文件名进行硬编码,则执行将按预期工作:
回显
雷姆确定日期
设置mm1 =%date:〜4,2%
设置dd1 =%date:〜7.2%
设置yyyy1 =%date:〜10,4%
设置rundate1 =%yyyy1 %% mm1 %% dd1%
设置executefile1 = tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.Lts.bak
设置downloadfile = tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.bak
extractor64.exe -F%executefile1%-E%downloadfile%
移动%executefile1%X:\ VoyagerBackups \ BackupFiles
答案 0 :(得分:2)
请勿使用与语言环境/用户设置有关的%date%变量。
改为使用wmic
或PowerShell,它们都有更稳定的方法。
:: Q:\Test\2018\12\14\SO_53787816.cmd
@Echo off
for /f "usebackq" %%A in (`
powershell -NoP -C "(Get-Date).ToString('yyyyMMdd')"
`) Do Set "Today1=%%A"
Echo PowerShell today:%Today1%
for /f "tokens=1-3 delims=.+-" %%A in (
'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set "Today2=%%A"
Set "Today2=%Today2:~0,8%"
Echo wmic today:%Today2%
Set "Base=X:\VoyagerBackups\PickupLocation\"
PushD "%Base%" || (Echo can't find base:%Base%&Pause&Goto :Eof)
For /f "delims=" %%A in ('Dir /B "*%Today1%*" ') Do (
Echo found file:%%~fA
)
PopD