Batch file to create folder and subfolders and copy files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lgt
Posts: 9
Joined: 08 Dec 2014 17:09

Batch file to create folder and subfolders and copy files

#1 Post by lgt » 08 Dec 2014 17:25

I have multiple project folders that contain files. The folders are named by their project id. I need to copy any pdf files that contain the word "approved" to a new folder. Below is an example.

Copy from here:
C:\Projects\Project_1\*Approved*.pdf

To here:
C:\Attachments\Project_1\Documents\*Approved*.pdf

The batch file needs to create a new project folder in "C:\Attachments" as well as 2 subfolders, "Documents" and "Images" and put the corresponding pdf files in the "Documents" folder.

I appreciate any help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to create folder and subfolders and copy file

#2 Post by foxidrive » 09 Dec 2014 02:11

Test this:

Code: Select all

@echo off
for /r "C:\Projects" %%a in ("*Approved*.pdf") do (
   for %%b in ("%%~dpa\.") do (
      echo processing "%%a"
      md "C:\Attachments\%%~nxb\Documents" 2>nul
      md "C:\Attachments\%%~nxb\Images" 2>nul
      copy "%%a" "C:\Attachments\%%~nxb\Documents" >nul
)
)
pause

lgt
Posts: 9
Joined: 08 Dec 2014 17:09

Re: Batch file to create folder and subfolders and copy file

#3 Post by lgt » 09 Dec 2014 09:27

That worked great! That's exactly what I needed. Thank you!

lgt
Posts: 9
Joined: 08 Dec 2014 17:09

Re: Batch file to create folder and subfolders and copy file

#4 Post by lgt » 09 Dec 2014 15:52

I noticed that the batch file is copying all the files every time it is run. Could it be possible to only copy new or modified files?

lgt
Posts: 9
Joined: 08 Dec 2014 17:09

Re: Batch file to create folder and subfolders and copy file

#5 Post by lgt » 09 Dec 2014 16:46

Figured out my last question. Am able to copy just new or modified files by using xcopy instead of copy and /s /d /y. Thanks!

Post Reply