Link to home
Start Free TrialLog in
Avatar of Silver_Power
Silver_PowerFlag for Canada

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

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
@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
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

Avatar of Silver_Power

ASKER

Thanks oDbA,

Tried this solution and it works perfectly.  

Regards.
@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