问题
I have a directory which contains CSV files needing to be moved to another directory:
C:\Users\JohnSmith\Desktop\Testing\report-20180819040000-20180826040000-4
We receive a new file weekly where the dates in the directory name will be updated. I want to create a batch file to copy this data using a wildcard on report*
but I am running into some issues.
The wildcard appears to work without any issues when I first navigate to:
C:\Users\JohnSmith\Desktop\Testing\
then use:
dir report*
It also works fine when I navigate to:
C:\Users\JohnSmith\Desktop\Testing\
then run
copy * C:\Users\JohnSmith\Desktop\Testing\Destination
My goal is to be able to run something simple in my batch file like the below:
copy C:\Users\JohnSmith\Desktop\Testing\report* C:\Users\JohnSmith\Desktop\Testing\Destination
Whenever I try running the above, I receive the following error:
The system cannot find the file specified. 0 file(s) copied.`
Does anyone have any suggestions?
回答1:
Use For /D
with a wildcards for your directory name, then you can use Copy
with a wildcard too!
From the Command Prompt:
For /D %A In ("%UserProfile%\Desktop\Testing\report-*-*-*") Do @Copy "%A\*.csv" "%UserProfile%\Desktop\Testing\Destination">Nul 2>&1
From a batch file:
@For /D %%A In ("%UserProfile%\Desktop\Testing\report-*-*-*") Do @Copy "%%A\*.csv" "%UserProfile%\Desktop\Testing\Destination">Nul 2>&1
Alternatively you could do it directly at the Powershell Prompt:
Cp "$($Env:UserProfile)\Desktop\Testing\report-*-*-*\*.csv" "$($Env:UserProfile)\Desktop\Testing\Destination"
来源:https://stackoverflow.com/questions/52122044/copy-contents-from-folder-using-wildcard-on-the-directory