Last active
July 23, 2023 11:29
-
-
Save Penderis/950c914afe0e5b6cc4ee to your computer and use it in GitHub Desktop.
Windows Batch Script to recursively copy files from folders into new folder using a text document with filenames
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 DEST_DIR="Copy files to:"%=% | |
set /p SEARCH_DIR="Copy files from:"%=% | |
@echo. | |
@echo Please check folder name for accuracy. | |
@echo Copy files to: %DEST_DIR% | |
@echo Copy files from: %SEARCH_DIR% | |
set /p CORRECT_FOLDERS="Are these correct? (please check spelling) y/n:" | |
if '%CORRECT_FOLDERS%'=='y' GOTO:YES_ANSWER | |
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 | |
:YES_ANSWER | |
@echo. | |
@echo you answered yes | |
@echo. | |
if exist %DEST_DIR% GOTO:READ_DATA | |
if not exist %DEST_DIR% md %DEST_DIR%&GOTO:READ_DATA | |
PAUSE | |
:NO_ANSWER | |
@echo. | |
@echo you answered no | |
set /p TRY_AGAIN="Try again? y/n:" | |
if '%TRY_AGAIN%'=='y' GOTO:YES_ANSWER | |
if '%TRY_AGAIN%'=='n' GOTO:EXIT_PROGRAM | |
PAUSE | |
:EXIT_PROGRAM | |
@echo. | |
@echo "So fucking sorry" | |
PAUSE | |
GOTO:EOF | |
:READ_DATA | |
@echo. | |
set /p GET_FILENAMES="What is the name of the text file your filenames are stored in?"%=% | |
if exist %GET_FILENAMES%.txt @echo We will now read and copy the files for you, have some coffee might take awhile & GOTO:WRITE_DATA | |
if not exist %GET_FILENAMES%.txt @echo Filename does not match, please type only the name without .txt extention & GOTO:READ_DATA | |
PAUSE | |
:WRITE_DATA | |
@echo. | |
@echo reading file name... | |
for /f "usebackq delims=" %%a in ("%GET_FILENAMES%.txt") do ( | |
for /r "%SEARCH_DIR%" %%b in ("%%a*") do ( | |
@echo Copy Started... | |
copy "%%b" "%DEST_DIR%\%%~nxb" | |
) | |
) | |
@echo Copy finished, please review actions. Lekker Man. | |
PAUSE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source folder, destination folder, text document and batch file must be in same toplevel directory. text document name in prompt entered without extention.