Last Updated: February 25, 2016
·
3.295K
· penderis

Windows Batch Script to recursively copy files

https://gist.github.com/Penderis/950c914afe0e5b6cc4ee


@echo off
CLS
setlocal EnableDelayedExpansion
REM Changes root path to relative of current bat folder
pushd "%~dp0"

REM finds files in provided .txt file and copies them to destination directory
REM CHECK FOR ADMIN RIGHTS
COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
IF ERRORLEVEL 1 GOTO:NONADMIN
DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1

:ADMIN
REM GOT ADMIN RIGHTS
COLOR 1F
ECHO Hi, %USERNAME%!
ECHO Please wait...

set /p DESTDIR="Copy files to:"%=%
set /p SEARCH
DIR="Copy files from:"%=%
@echo.
@echo Please check folder name for accuracy.
@echo Copy files to: %DESTDIR%
@echo Copy files from: %SEARCH
DIR%
set /p CORRECTFOLDERS="Are these correct? (please check spelling) y/n:"
if '%CORRECT
FOLDERS%'=='y' GOTO:YESANSWER
if '%CORRECT
FOLDERS%'=='n' GOTO:NO_ANSWER
COLOR 2F
ECHO.
PAUSE
GOTO:EOF

:NONADMIN
REM NO ADMIN RIGHTS
COLOR 4F
ECHO.
ECHO PLEASE RUN AS ADMINISTRATOR
ECHO.
pause
GOTO:EOF

:YESANSWER
@echo.
@echo you answered yes
@echo.
if exist %DEST
DIR% GOTO:READDATA
if not exist %DEST
DIR% md %DESTDIR%&GOTO:READDATA
PAUSE

:NOANSWER
@echo.
@echo you answered no
set /p TRY
AGAIN="Try again? y/n:"
if '%TRYAGAIN%'=='y' GOTO:YESANSWER
if '%TRYAGAIN%'=='n' GOTO:EXITPROGRAM
PAUSE

:EXIT_PROGRAM
@echo.
@echo "So fucking sorry"
PAUSE
GOTO:EOF

:READDATA
@echo.
set /p GET
FILENAMES="What is the name of the text file your filenames are stored in?"%=%
if exist %GETFILENAMES%.txt @echo We will now read and copy the files for you, have some coffee might take awhile & GOTO:WRITEDATA
if not exist %GETFILENAMES%.txt @echo Filename does not match, please type only the name without .txt extention & GOTO:READDATA
PAUSE

:WRITEDATA
@echo.
@echo reading file name...
for /f "usebackq delims=" %%a in ("%GET
FILENAMES%.txt") do (
for /r "%SEARCHDIR%" %%b in ("%%a*") do (
@echo Copy Started...
copy "%%b" "%DEST
DIR%\%%~nxb"
)
)
@echo Copy finished, please review actions. Lekker Man.
PAUSE
</pre>