问题
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.
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
To this directory K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\
.
But the 1711
part of folders FI81711001
changes every month and I can't figure out how to get wildcards to work in scripting.
I've tried the following scripts:
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\"
When I run the For
script I get %%D was unexpected at this time.
回答1:
@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
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.
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.
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).
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
)
回答2:
Besides missing quotes in your for /D
approach to protect the SPACE in the path, there is a conceptional problem:
you cannot put wildcards somewhere in the middle of a path, they can be used only in the very last item.
Since you have got a known fixed directory hierarchy depth, you could use nested for /D loops:
for /D %%A in ("K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\FI8*") do (
for /D %%B in ("%%~A\FI8*") do (
for /D %%C in ("%%~B\FI8*") do (
for /D %%D IN ("%%~C\OM_LOCAL*") do (
xcopy /L /F /I /E "%%~D\media1\*.*" "K:\Eng\NAVDB\Navigation Databases\Current\FI8_Icelandair_B75_76\media"
)
)
)
)
The /L
option of the xcopy command prevents it from copying any files, the /F
option displays both full source and destination paths; after having checked for the correct files that would be copied just remove /L
(and also /F
if you like) to actually copy them.
来源:https://stackoverflow.com/questions/46683634/using-a-batch-file-to-copy-files-with-a-wildcard-in-the-directory-path