Sunday, May 31, 2026

Batch Program - Move files of specific type to Target folder using BAT file

Using the following Batch programming code, you can Move files of specific type to a target folder. 
  • SET command is used to declare variable and initialize it.
  • If Target folder is missing then MD command creates the target folder.
  • CD command is is used to change directory from D Drive to G:\PowerPoint Y25 folder.
  • The FOR loop loops all MP4 files in the source folder and 
  • DO command uses a number of actions inside its parentheses ().
  • %%i is loop variable which scope is inside the loop.
  • %%nxi stands for filename with extension for each i i.e. mp4 type file.
  • MOVE command is used to move from source to target folder.
  • REM stands for comments in batch code.
  • ECHO command prints the text on console.
@ECHO OFF

SET "SourceDir=G:\PowerPoint Y25"
SET "DestinationDir=G:\Lesson4"
SET "SearchTerm=React"

REM --- 1. Ensure the destination directory exists ---
IF NOT EXIST "%DestinationDir%\" MD "%DestinationDir%"

REM --- 2. Change to the source directory for the search ---
CD /D "%SourceDir%"

REM --- 3. Loop through all PDF files and check the filename ---
FOR %%i IN (*.mp4) DO (
    ECHO Checking: "%%~nxi"
    
    REM Note: Removed the /I switch for case-sensitivity
    ECHO "%%~nxi" | FIND "%SearchTerm%" >NUL
    
    REM This checks: IF the error level is NOT greater than or equal to 1
    REM i.e., IF error level is exactly 0 (Match Found)
    IF NOT ERRORLEVEL 1 (
        ECHO **MATCH FOUND! Moving file: "%%~nxi"**
        
        REM Move the file to the destination folder
        MOVE "%%i" "%DestinationDir%"
    ) ELSE (
        ECHO No match found.
    )
)

PAUSE

No comments:

Post a Comment

Hot Topics