简体   繁体   中英

How do I copy a file using a wildcard in Windows without appending?

I have an excel file that I download from an vendor FTP site on a daily basis. Every day the file name changes to contain the date and the vendor appends either "actual" or "estimate" to the end of the file.

example:
F_02262014_actual.xlsx
F_02262014_estimate.xlsx
etc.

Because of these slight changes, I'm using a wildcard to copy the file to a consistent name in another directory.

Example:
copy \\Source\\F_02262014* \\Target\\CurrentFile.xlsx

My problem is that whenever I use a wildcard in the copy command, Windows assumes I'm appending several files to one. This causes the file to get corrupted (all contents deleted for some reason).

Is there a command, or series of commands, that will copy the first file matching the file pattern to the desired file name?

You cannot modify the behavior of the copy command in the way that you are asking for. Instead of doing a wildcard copy, try this:

IF EXIST "\Source\F_02262014_actual.xlsx" (copy \Source\F_02262014_actual.xlsx \Target\CurrentFile.xlsx) ELSE (copy \Source\F_02262014_estimate.xlsx \Target\CurrentFile.xlsx)
for %%f in ("\Source\F_02262014*") do copy /y "%%~ff" "\Target\CurrentFile.xlsx"

Written to include in batch file. If this will be executed from command line, replace all %% with %

I can't reproduce your reported problem.

DO you mean the source file is emptied, or that the target is empty?

I'd add the /b switch to the COPY

copy /B \Source\F_02262014* \Target\CurrentFile.xlsx

...worked for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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