Custom Batch Scripts; Needing Help

Surely through all these AI response's I can hopefully formulate a working script tailored to my satisfactory.
A question though...(notice the placement of the quotation marks)
Is the syntax
Code:
set YourChosenVariable="C:\Path\To\Source/Directory"
and not
Code:
set "YourChosenVariable=C:\Path\To\Source\Directory"
 
Last edited:

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bit 7601 ...Intel(R) Core(TM) i3-N3054.00 GBStandard VGA Graphics Adapter
Computer type
Laptop
Computer Manufacturer/Model Number
LENOVO IdeaPad Slim 3 15IAN8
OS
Microsoft Windows 7 Home Premium 64-bit 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3-N305
Motherboard
Oracle Corporation VirtualBox
Memory
4.00 GB
Graphics Card(s)
Standard VGA Graphics Adapter
Sound Card
High Definition Audio Device
Hard Drives
VBOX HARDDISK ATA Device
AI Response...

To process and clean your directory, create a .bat file in your main folder. This script recursively finds specific file types, moves them to categorized folders until they reach ~100MB, renames duplicates, and removes empty source folders. [1, 2, 3, 4]

The Batch Script
Save the following code as a .bat file (e.g., OrganizeFiles.bat) in the root directory you want to process. [1]


Code:
@echo off
setlocal enabledelayedexpansion

:: ==========================================
:: CONFIGURATION
:: ==========================================
:: Add the extensions you want to process, separated by spaces
set "TARGET_EXTS=.mp4 .avi .mkv .mp3 .jpg .png"
set "LIMIT_MB=100"
set /a LIMIT_BYTES=LIMIT_MB * 1024 * 1024

echo Starting file organization process...

:: ==========================================
:: 1. MOVE & SORT FILES
:: ==========================================
for %%E in (%TARGET_EXTS%) do (
    echo Processing extension: %%E
    set "SubDirName=Folder_%%E"
 
    REM Create destination folder
    if not exist "!SubDirName!\" md "!SubDirName!\"
 
    REM Get current size of the destination folder
    set "FolderSize=0"
    for /r "!SubDirName!\" %%A in (*) do set /a FolderSize+=%%~zA

    for /r %%F in (*%%E) do (
        REM Skip files if they are already in the destination folder
        echo "%%F" | findstr /i "!SubDirName!" >nul
        if errorlevel 1 (
            if !FolderSize! LSS %LIMIT_BYTES% (
                call :MoveAndRename "%%F" "!SubDirName!"
                set /a FolderSize+=%%~zF
            )
        )
    )
)

:: ==========================================
:: 2. CLEAN EMPTY FOLDERS
:: ==========================================
echo Cleaning up empty source directories...
for /r /d %%D in (*) do (
    rmdir "%%D" 2>nul
)
echo Done!
pause
exit /b

:: ==========================================
:: SUBROUTINE: HANDLE DUPLICATES & MOVE
:: ==========================================
:MoveAndRename
set "SrcFile=%~1"
set "DestDir=%~2"
set "FileName=%~n1"
set "FileExt=%~x1"

if exist "%DestDir%\%FileName%%FileExt%" (
    echo Duplicate found: %FileName%%FileExt%
    set "Count=1"
    :CheckNext
    if exist "%DestDir%\%FileName%_dup!Count!%FileExt%" (
        set /a Count+=1
        goto CheckNext
    )
    echo Renaming to: %FileName%_dup!Count!%FileExt%
    move "%SrcFile%" "%DestDir%\%FileName%_dup!Count!%FileExt%"
) else (
    move "%SrcFile%" "%DestDir%\"
)
goto :eof


How It Works
  1. Extension Filtering: The script loops through a space-separated list of extensions (e.g., .mp4, .jpg). You can edit the TARGET_EXTS variable to add or remove types. [1, 2, 3]
  2. Size Limiting: It calculates the size of the target directory in bytes and stops moving files when the total approaches 100MB. [1]
  3. Duplicate Handling: If a file of the same name exists, it recursively appends _dup1, _dup2, and so on, preventing files from being overwritten.
  4. Directory Cleanup: It uses the rmdir command to recursively delete folder trees, safely removing only those that are empty after files are transferred. [1]


End of AI Response
 
Last edited:

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bit 7601 ...Intel(R) Core(TM) i3-N3054.00 GBStandard VGA Graphics Adapter
Computer type
Laptop
Computer Manufacturer/Model Number
LENOVO IdeaPad Slim 3 15IAN8
OS
Microsoft Windows 7 Home Premium 64-bit 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3-N305
Motherboard
Oracle Corporation VirtualBox
Memory
4.00 GB
Graphics Card(s)
Standard VGA Graphics Adapter
Sound Card
High Definition Audio Device
Hard Drives
VBOX HARDDISK ATA Device
Back
Top