使用批处理文件复制目录路径中带有通配符的文件? [英] Using a batch file to copy files with a wildcard in the directory path?
问题描述
我想要一个批处理文件将一个月更改一次的文件夹中的文件复制到另一个文件夹,但是Windows命令提示符似乎不喜欢通配符.
I want a batch file to copy files from a folder, which changes every month, to another folder but it seems Windows command prompt doesn't like wildcards.
示例:
我要复制文件夹media1
并在此目录中包含文件:
Example:
I want to copy the folder media1
and containing files in this directory:
K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\FI81711001\FI81711001\FI81711_AHDF.001\OM_LOCAL_FLOPPY_1.44MB_S520_v1_1\media1
到此目录K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\
.
但是文件夹FI81711001
的1711
部分每个月都会发生变化,我不知道如何在脚本中使用通配符.
But the 1711
part of folders FI81711001
changes every month and I can't figure out how to get wildcards to work in scripting.
我尝试了以下脚本:
robocopy "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\FI8*\FI8*\FI8*\OM_LOCAL*\media1" "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\"
copy "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\FI8*\FI8*\FI8*\OM_LOCAL*\media1" "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\"
xcopy "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\FI8*\FI8*\FI8*\OM_LOCAL*\media1" "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\"
for /D %%D in (K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\FI8*\FI8*\FI8*\OM_LOCAL*\) do copy "%%~D\media1\" "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\"
运行For
脚本时,我得到%%D was unexpected at this time.
推荐答案
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76"
SET "destdir=U:\Eng"
SET "dt=1711"
FOR /d /r "%sourcedir%" %%a IN (*) DO (
ECHO %%a|FINDSTR /i "\FI8%dt%.*\FI8%dt%.*\FI8%dt%.*\OM_.*\media1" >NUL
IF NOT ERRORLEVEL 1 (
ECHO XCOPY "%%a\*" "%destdir%\"
)
)
GOTO :EOF
对于需要在何处复制的内容,我们仍然一无所知.我假设您想将....\media1
中的文件在适当的一天复制到某个已知目录(我的测试设置使用U:
代替K:
)
We're still in the dark about precisely what needs to be copied where. I've assumed you want the files in ....\media1
for the appropriate day copied to some known directory (My test setup uses U:
in place of K:
)
明确需要复制的内容和位置,可以对其进行完善.
With clarification of what needs to be copied and where, this can be refined.
请注意,dt
设置为4个字符的日期序列(请勿对该变量使用date
-这是保留名称).如何获取数据-好吧,关于SO如何提取日期数据有很多文章,因为日期数据随用户配置的不同而不同.我使用了一个常数,可以根据需要手动输入-取决于这是否是今天 上执行的计划作业还是随机运行.
Note that dt
is set to the 4-character date sequence (don't use date
for this variable - it's a reserved name). How you derive it - well, there are many articles on SO about how to extract date data, as it varies with user configuration. I've used a constant that could possibly be manually input if desired - depending on whether or not this is a scheduled job working on today or run at random.
基本上,请进行递归目录扫描,依次将每个目录名称分配给%%a
. echo
将该名称命名为findstr
,并在任何情况下查找字符串"\ FI8 * thedateinDT_anycharacters * \ FI8 * thedateinDT_anycharacters * \ FI8 * thedateinDT_anycharacters * \ OM_ anycharacters \ media"(
Essentially, do a recursive directory scan, assigning each directoryname in turn to %%a
. echo
that name into findstr
, looking for the string "\FI8*thedateinDT_anycharacters*\FI8*thedateinDT_anycharacters*\FI8*thedateinDT_anycharacters*\OM_anycharacters\media" in any case (the /i
switch).
如果找到了字符串,则errorlevel
将被设置为0
,而不是[1或更大],因此xcopy
命令将是echoed
(用于验证-将echo
删除为执行 xcopy
)
If the string is found, then errorlevel
will be set to 0
which is not [1 or greater] hence the xcopy
command will be echoed
(for verification - remove the echo
to execute the xcopy
)
这篇关于使用批处理文件复制目录路径中带有通配符的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!