How to Copy files of a certain type keeping folder structure

How to Copy files of a certain type keeping folder structure

In this tutorial we’ll take you through copying files of a certain type while keeping their folder structure intact with the Windows command-line.

This works on all sub-folders from where the script is launched from.

The idea is to copy all files of .someextension into their own folder.

At the end of the script we’ll add a section to ask the user (you!) if you want to delete all source files that were copied.

For this example, we’ll use one of the most common formats, JPEG.

Let’s begin 😉

Full code

@ECHO OFF

@SETLOCAL enableextensions
@CD /d "%~dp0"

REM Frequently changed
SET TYP=JPG
SET EXT1=*.jpg
SET EXT2=*.jpeg
SET EXT3=

REM Perma
SET TTL=Copy Utility [%TYP%]
SET DIR=..\_%TYP%
SET CPR=©2008-2016 Jenkins Media. All Rights Reserved.
TITLE %TTL%

REM Make the Target directory
MKDIR %DIR%

REM Do the (x)copy
XCOPY "%EXT1%" %DIR% /S
XCOPY "%EXT2%" %DIR% /S

:ASK
ECHO /!\ WARNING /!\
ECHO.
ECHO ABOUT TO DELETE ALL SOURCE FILES ! ! !
set INPUT=
set /P INPUT=Are you sure? (Y/N) : %=%
If /I "%INPUT%"=="y" GOTO yes 
If /I "%INPUT%"=="n" GOTO no
ECHO Incorrect input & GOTO ASK
:YES
ECHO.
REM Delete source files (CAUTION! - READ BELOW)
DEL /s %EXT1%
DEL /s %EXT2%

:NO
ECHO.
ECHO MOVING TARGET DIR BACK TO SOURCE ROOT
REM Move Target dir back to Source dir (MUST COME AFTER DEL COMMAND !!!)
MOVE "%DIR%" "%~dp0"

:END
ECHO.
ECHO ------------------------
ECHO %TTL% %CPR%
ECHO ------------------------
ECHO Grab Completed at %TIME% - %DATE%
PAUSE
EXIT

Report This Post

Report This Blog