繁体   English   中英

批处理文件以查找和复制最新文件

[英]Batch file to find and copy the latest file

有一个系统每15分钟生成一次txt文件转储,每次都使用不同的文件名。 我需要找到包含文本“ ASN”的最新文件,并将其复制到我可以使用的文件夹中。

到目前为止,我已经拥有了,但是我无法复制任何文件。

SET smart_server='Z:\JUL2017'

FOR /F "delims=|" %%I IN ('DIR "%smart_server%" /S /B /O:D ^| find /i "ASN" ') DO SET NewestFile=%%I
copy "%smart_server%\%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 

源目录是一个映射的驱动器,我希望将其复制到本地驱动器。

下面将复制包含文本ASN的文件,忽略文件本身而不是文件名的大小写:

@echo off
set "smart_server=Z:\JUL2017"

rem list all files (recursive) latest first
for /F "delims=|" %%I in ('dir "%smart_server%" /S /B /A-D /O:-D') do (
   find /i "ASN" "%%I" > nul
   rem success (errorlevel == 0)?
   if not errorlevel 1 (
      set "NewestFile=%%I"
   )
)
if defined NewestFile (
   rem addionally echoing the command due copy is not verbose
   echo copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
   copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
) else (
   echo Found no file!
)

我已经改变了几件事。

1套:
我更改了变量smart_server的设置,因为您的设置在路径中包含'

2. dir命令:
/O:D排序将显示最早的反向列表使用: /O:-D 进一步排除要显示为dir的目录,因为您无法使用find搜索它们,请使用: /AD

3.管道查找:
似乎要查找的管道不适用于文件名中的空格,因此我将其从命令中删除并在for循环中执行。 如果find成功,则设置NewestFile变量。 我将find/I一起使用,因此它忽略了文本的大小写。


如果您需要脚本来复制文件名本身包含ASN并以.txt结尾的文件,则可以使用(这也忽略了这种情况):

@echo off

set "smart_server=Z:\JUL2017"
for /F "delims=|" %%I IN ('DIR "%smart_server%\*ASN*.txt" /S /B /A-D /O:-D') DO SET NewestFile=%%I
echo copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 
copy "%NewestFile%" "C:\htdocs\smart_asn_downloads\new" 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM