How to copy only files(not directories) using batch file?

一世执手 提交于 2020-01-25 06:32:30

问题


The directory structure is:

  Images
    -Folder1
      -image1.jpg
      -image2.jpg
    -Folder2
      -image3.jpg
      -image4.png 
    -Folder3
      -image6.png
      -image7.jpg 
    -Folder4

I want to copy all images(i.e *.jpg, *.png) files only (not the folders) into the parent directory("Images").

I have tried using "robocopy" as follows:

robocopy /np ..\..\Exam\Images ..\..\Exam\Images *.jpg *.png /S

Here all files as well as folders are copied :(. But I need only files to be copied. How to do this?

Many many thanks in advance!


回答1:


Try this on the command line:

for /r "Images" %i in (*.jpg *.png) do copy "%~fi" "my\target folder"

For a bach script the % must be doubled %%.




回答2:


I think COPY or XCOPY is best used for files while I prefer Robocopy when dealing with folders.

Using the posted example try: (adjust paths to suit your needs.

    @Echo off
    For /f %%b In ('Dir C:\Exam\Images /b /s /a:d') Do (
       Robocopy %%b C:\Exam\Images *.jpg *.png /xx /np
    )



回答3:


an easier way of doing this might be

for /r %%p in (*.png, *.jpg) do copy %%p destinationFolder.




回答4:


Robocopy only has the /XD switch to exclude directories but it excludes the whole directory. I'd use a batch file to do it instead.

Try this:

@echo off
setlocal

for /f %%a in ('dir *.jpg *.png /b /s /a-d') do (
   copy %%a PathToImagesFolder
)



回答5:


there is an easy to use program called Drop-It if this is a repetitive task then you can use this to sort|move|copy the files to a single directory. hope this helps



来源:https://stackoverflow.com/questions/16441479/how-to-copy-only-filesnot-directories-using-batch-file

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