Copy Contents From Folder Using Wildcard On The Directory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 09:38:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!