在 Windows 環境下的 xcopy 誤以為跟 linux 下的 cp 一樣好用, 結果並沒有, 只 copy 檔案時, 或某一個特定資料夾時是OK 的, 但遇到第一層就是 * 或 ? 的資料夾時,就會出意外了, 並不支援.
例如:
xcopy max* to D:\
如果目錄下有 max123.txt 與 maxABC folder, 則只會 copy max123.txt, wildcard match 不到 maxABC 的目錄.
解法有點複雜, 不是一般人能用第一印象記住的指令:
FOR /d %a in (.\max*) DO XCOPY "%a" D:\%a\ /s /y
上面是針對在 console 裡的語法, 寫進去 batch file 的話, 又不一樣! %a 要變成 %%a, 哎 microsoft 為什麼設計的這麼複雜, 應該是被古時候的架構綁住, 所以 % 已經被其他地方給使用, 又要讓舊的 script 可以正常執行, 又要讓新功能也可以執行, 變成現在這樣”怪怪的”.
最佳解答
Copying Files with Wildcards in the Path
https://stackoverflow.com/questions/8609028/copying-files-with-wildcards-in-the-path
Using XCOPY, it is no longer possible to use wildcarding in the file name for directories, and then just have a group of similarly named directories copied over to an archive or network structure.
(Hint: You want to do this all the time, if you are managing big wads of data. Eg. Video or movie files (where many files may be associated with the production. Or, price history data files, in various formats, or database files from various research projects, or experimental work.)
I am sure I used to be able to do this with older XCOPY, but with upgrades (for security), wildcards in the file name, do not work right (ie. you can build a directory structure, with the “/t” option, but you cannot get the damn files within the directories to migrate! Damned annoying.)
Well, we here at the Farm, always get to a solution. Even if it takes using a .44 magnum or a Styer 50. 🙂
So, heres what I came up with:
Assume you have a bunch of directories called “Fark_1, Fark_2, Fark-Special, Fark_in_the_Park, Fark99… ” and so on.
If all these “Fark…” directories exist on a C:\ disk, and you want to migrate a copy of this sub-structure to the archive disk on the network (say it is called: “h:”) you can just do the following:
Run a FOR DO from Windows (MS-DOS) command prompt:
c:\> XCOPY fark* h: /s /e /f /t
The /s /e are for copying subdirs
and empty directories, the /f says to
show the files, and the /t says to only
copy the disk directories. No XCOPY
option seems to exist anymore to copy the
disk structure AND the files in the
directories.
To get the files to copy over, you then do the following, also from Windows command prompt:
FOR /d %a in (fark*) DO XCOPY "%a" h:%a\ /s /e /f /h /k /y
The /d says we are using directory names.
The %a is the variable name, substituted
into the command script for XCOPY. Make
sure to use whatever logical name or drive
letter you have for your archive structure.
(ie. don't use h: in the above command,
unless you have mapped h: to the place on
the network where you want to copy your
files to.) And note, the /y option means
overwrite any files already there.
Yes, this is old-school unix/dos stuff, but it works, and you can put it into a batch job. And you can avoid being the mouseboy to get the work done. (Point, click, grunt, curse. Repeat. The canonical process for using a GUI, right?) Now, there are lots of fancy ways to do this. And various utils like Robocopy, XXcopy and so on. But this gets it done, and if you have any copy of Windows, you probably already have the XCOPY and FOR-DO thingys. All the best. Oh, if you slot the two commands into a batch job, then you have to adjust the syntax for two levels of command substitution (I think is the correct term).
batch example:
REM --- copy the "fark*" files to the archive disk h:
@echo off
REM --- create/update the structure
xcopy fark* h: /s /e /f /t
REM --- copy over the fark* dirs, and all associated subdirs
for /d %%a in (fark*) do xcopy "%%a" "h:%%a\" /s /e /f /h /k /y
資料來源
xcopy from wildcard directory
https://stackoverflow.com/questions/23209474/xcopy-wildcard-source-folder-name-to-destination
Using Wildcards for Directories in Window’s copy Command
https://stackoverflow.com/questions/43243096/using-wildcards-for-directories-in-windows-copy-command