Copy files from different folders

Hello Experts,

I need to create a batch file to copy files with the same extension from different folders onto another drive, create a folder based on the time  when command was run, and create the files and folders.

Thank you for your assistance.
Silver_PowerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this; it's currently in test mode and will only list the files it would copy; remove the uppercase /L at the end of line 15 to run it for real:
@echo off
setlocal enabledelayedexpansion
set SourceDir=C:\Temp
REM *** Time stamp will be added to the folder name defined here
set TargetDir=D:\Temp\Target_
set Mask=*.txt
echo Getting system time ...
for /f "tokens=1-9" %%a in ('wmic.exe Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
	set /a Line += 1
	if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
	if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%Year%%Month%%Day%-%Hour%%Minute%%Second%
robocopy.exe "%SourceDir%" "%TargetDir%%TimeStamp%" %Mask% /s /r:0 /np /L

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
Can you clarify what "from different folders" means?  Is this just all subfolders under a single base folder?  Or a list of folders?  Or folders matching some pattern?

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
@oBdA,

I've become a fan of WMIC with LocalDateTime for timestamps like this.  The output is always predictable, doesn't vary with Windows localization, so easy for scripting.  See what you think, flattens the code a smidge as below.  Just a thought...

@echo off
setlocal enabledelayedexpansion
set SourceDir=C:\Temp
REM *** Time stamp will be added to the folder name defined here
set TargetDir=D:\Temp\Target_
set Mask=*.txt
echo Getting system time ...
for /f %%A in ('wmic os get LocalDateTime ^| findstr ^[0-9]') do (set LocalDateTime=%%A)
set TimeStamp=%LocalDateTime:~0,8%_%LocalDateTime:~8,6%
robocopy.exe "%SourceDir%" "%TargetDir%%TimeStamp%" %Mask% /s /r:0 /np /L

Open in new window

~bp
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Paul TomasiCommented:
Is this what you want?

Change lines 4 and 5 to point to your source and destination drive letters and run the batch file.

The titlebar displays the name of each file being processed as an indication the batch file is running.

@echo off
setlocal enabledelayedexpansion

set source=c:
set destination=e:

for /f %%a in ('wmic os get localdatetime ^| findstr ^[0-9]') do (set datetime=%%a)

set destination=%destination%\%datetime:~0,14%
mkdir %destination%

for /r %source%\ %%a in (*.*) do (
  title %%~pnxa

  set extension=%%~xa
  set extension=!extension:~1!

  if not exist "%destination%\!extension!\" mkdir "%destination%\!extension!\"
  copy %%a %destination%\!extension!\ >nul
)

Open in new window

Silver_PowerAuthor Commented:
Thanks oDbA,

Tried this solution and it works perfectly.  

Regards.
oBdACommented:
@Bill Prew,

sorry, thought I already sent this out:
LocalDateTime is certainly fit for scripting, too, but Win32_LocalTime doesn't vary with localization, either, and the main reason for my "long" version is that once it's done, there's a set of self-explaining variables that can easily be arranged in any order, even by non-batch-savvy users, without having to count characters. And since we're mainly scripting for non-batch-savvy users, I prefer readability (after a fashion; talking about the variables here, not the way ro create them ...) over shortness.
set TimeStamp=%LocalDateTime:~0,8%_%LocalDateTime:~8,6%
set TimeStamp=%Year%%Month%%Day%-%Hour%%Minute%%Second%

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.