Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

windows - Copy Contents From Folder Using Wildcard On The Directory

I have a directory which contains CSV files needing to be moved to another directory:

C:UsersJohnSmithDesktopTesting
eport-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:UsersJohnSmithDesktopTesting

then use:

dir report*

It also works fine when I navigate to:

C:UsersJohnSmithDesktopTesting

then run

copy * C:UsersJohnSmithDesktopTestingDestination

My goal is to be able to run something simple in my batch file like the below:

copy C:UsersJohnSmithDesktopTesting
eport* C:UsersJohnSmithDesktopTestingDestination

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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%DesktopTesting
eport-*-*-*") Do @Copy "%A*.csv" "%UserProfile%DesktopTestingDestination">Nul 2>&1

From a batch file:

@For /D %%A In ("%UserProfile%DesktopTesting
eport-*-*-*") Do @Copy "%%A*.csv" "%UserProfile%DesktopTestingDestination">Nul 2>&1

Alternatively you could do it directly at the Powershell Prompt:

Cp "$($Env:UserProfile)DesktopTesting
eport-*-*-**.csv" "$($Env:UserProfile)DesktopTestingDestination"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...