Custom Batch Scripts; Needing Help

WyattWhiteEagle

Member
Member
VIP
Local time
2:10 PM
Messages
192
Where's the best to post custom batch text asking for help to improve it's results?
 

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
here
 

My Computers My Computers

  • At a glance

    7 X64i5 84002x8gb 3200mhz
    Computer type
    PC/Desktop
    OS
    7 X64
    CPU
    i5 8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Hard Drives
    various
    PSU
    pure power 11 400w cm
    Case
    Coolermaster
    Cooling
    cryorig m9i
  • At a glance

    7x64g54008gb ddr4 2400
    Computer type
    PC/Desktop
    OS
    7x64
    CPU
    g5400
    Motherboard
    ga b365m ds3h
    Memory
    8gb ddr4 2400
    PSU
    xfx pro 450w
From Google's AI Overview and mode.
It has a note attached to it saying, "AI can contain mistakes, so double-check responses."

Does the following "code" have anything missing or incorrect?

I searched for this and AI returned the code...
windows 7 64 bit move files recursively filter by specific file types into 100 megabytes limit per directory cmd batch
Code:
@echo off

cls

SETLOCAL ENABLEDELAYEDEXPANSION

:: --- CONFIGURATION ---
:: Path where your files currently are (no trailing slash)
SET "SourcePath=C:\Your\Source\Folder"

:: Path where you want the new folders to be created
SET "DestPath=C:\Your\Destination\Folder"

:: File extension filter (e.g., .mp4, .jpg, .pdf)
SET "ExtFilter=*.mp4"

:: Size limit per directory in bytes (100MB = 104,857,600 bytes)
SET /A MaxSizeBytes=104857600

:: --- SCRIPT EXECUTION ---
IF NOT EXIST "%DestPath%" MD "%DestPath%"
SET "CurrentFolderNum=1"
CALL :CREATE_NEW_DIR

:: Recursively find files and process them
FOR /R "%SourcePath%" %%F IN (%ExtFilter%) DO (
    IF "%%~aF" LSS "d" (
        SET "FileSize=%%~zF"
        
        :: Add current folder's size
        SET /A "TotalSize=!TotalSize! + FileSize"
        
        :: Check if adding the file exceeds the 100MB limit
        IF !TotalSize! GTR %MaxSizeBytes% (
            SET /A "CurrentFolderNum+=1"
            CALL :CREATE_NEW_DIR
            SET "TotalSize=!FileSize!"
        )
        
        ECHO Moving: "%%~nxF" to !CurrentFolder!
        MOVE "%%F" "!CurrentFolder!\"
    )
)

ECHO Task Completed!
PAUSE
EXIT /B

:CREATE_NEW_DIR
SET "CurrentFolder=%DestPath%\Group_!CurrentFolderNum!"
MD "%CurrentFolder%"
SET "TotalSize=0"
EXIT /B
 
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
Not exactly solved yet.
Making the needed changes, it works very well.
The optional changes work well also, such as

Code:
SET "CurrentFolder=%DestPath%\Group_!CurrentFolderNum!"

to

Code:
SET "CurrentFolder=%DestPath%\Txt_!CurrentFolderNum!"
 

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
Next task: Do multiple file types in my batch file.

One possible downfall might be, "variable may not distinguish between the many different file type's."
Possible work-around may be to create the new variable's with numbers.
Useful for when repeating the command's for multiple file extensions within the script.

Example:
Code:
SET "ExtFilter=*.mp4"
to
Code:
SET "ExtFilter001=*.mp4"
 
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
After running the newly customized script, the recent 001 and designated file extensions to match didn't work...

Back to original script customizations

Marking my spot within the script.
Will continue my works after some much needed rest.:cool:
 
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
It's beginning to look like this kind of script isn't a "one-size-fits-all" kind of script.
It seems like what I'm trying to accomplish is better achieved from one script calling other scripts when each file extension is accomplished.

Community, how might this script call another script?
 
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
This might help:
Another AI response...

To call another batch file from a main batch file in Windows 7 (64-bit), you must use the CALL command. [1, 2]
If you try to run the second batch file by simply typing its name without CALL, the first batch file will stop entirely, and control will never return to it. [3, 4, 5]

Run Sequentially (Wait for it to finish)​

Use CALL to pause the main script, run the second script, and then return to the main script. [1, 2]
Code:
@echo off
echo Starting the main script...

:: Call the second batch file
call "C:\path\to\second_file.bat"

echo Returned to the main script!
pause

⚠️ Important Windows 7 (64-bit) Caveats​



  • The Admin Path Trap:If your main script is running with Administrator privileges, Windows 7 changes the default working directory to C:\Windows\System32. If the second batch file is in the same folder as the first, it will fail with a "file not found" error. Fix this by adding a change-directory line to the top of your scripts:
    Code:
    cd /d "%~dp0"
    (That forces CMD to look in the folder where the script actually lives). [9, 10, 11]
  • Passing Parameters: You can pass arguments to the second file by adding them after the file name (e.g., call "file.bat" argument1 argument2). The second file can read them using %1 and %2. [1, 12, 13, 14, 15]

[1] https://ss64.com
[2] https://jpsoft.com
[3] https://stackoverflow.com
[4] https://df.lth.se
[5] https://opensource.com
[9] https://stackoverflow.com
[10] https://stackoverflow.com
[11] https://encyclopedia.pub
[12] https://superuser.com
[13] https://learn.microsoft.com
[14] https://superuser.com
[15] https://superuser.com
 
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
ok...I found how it is accomplished using the call command.
I'm just unsure where these need to be within my script in post 3 above.
Any idea's?
Code:
call file-1.bat
call file-2.bat
call file-3.bat
 

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
In my searching I found the following post.

See if this is what you are looking for:

First batch file

@echo off<br>::Batch File One (bat1.cmd)<br>cls<br>dir *.tmp<br>echo Calling Second Batch File...<br>pause<br><br>call bat2.cmd 1<br>echo Back to First Batch File...<br>echo Calling Second Batch File...<br>pause<br><br>call bat2.cmd 2<br>echo Back to First Batch File...

Second batch file

@echo off<br>::Batch File two (bat2.cmd)<br>cls<br>if "%1" EQU "" goto :eof<br>call :%1<br>goto :eof<br><br>:1<br>Echo Running commands from second batch file passing 1 as parameter...<br>pause<br>dir *.txt<br>goto :eof<br><br>:2<br>Echo Running commands from second batch file passing 2 as parameter...<br>pause<br>dir *.doc<br>goto :eof

The second batch file will do nothing unless a parameter is passed to it on the command line. This can be changed if needed, but should give you the idea of calling one batch file from another, running a specified sub-routine, and then return to the calling batch file.

Scr1ptW1zard
 
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
I'm actually wanting just one cmd batch file (*.bat) to sort multiple "file.ext" into 100mb limited directories labeled "*.Exe-1, Exe-2, Txt-1, Txt-2 Doc-1, Doc-2, etc."
These File's are saved from years and years of work.
I need them in 100mb directories before compressing to uplaod them to a cloud storage database.

Now to sift through these recent few postings, I might be able to see how it might fit together :cool:
 
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
I typed exactly what I wanted and AI returned something I might be looking for that I can build from.

I typed...
windows 7 64 bit sort multiple file extensions into multiple sudirectories 100mb limit cmd batch
Code:
@echo off
setlocal enabledelayedexpansion

:: 1. Define your size limit in bytes (100 MB = 104857600 bytes)
set "MAX_SIZE=104857600"

:: 2. Define extensions and their target folders (Format: "ext|folder_name")
:: You can add as many as you need
set "TYPES=jpg|Images png|Images mp4|Videos avi|Videos docx|Documents pdf|Documents"

:: 3. Process each file in the current directory
for %%F in (*) do (
    if "%%F" neq "%~nx0" (
        set "EXT=%%~xF"
        :: Remove the leading dot (e.g., .jpg becomes jpg)
        if defined EXT set "EXT=!EXT:~1!"
      
        if defined EXT (
            call :ProcessFile "%%F" "%%~zF" "!EXT!"
        )
    )
)

echo Done sorting!
pause
exit /b

:ProcessFile
set "FILENAME=%~1"
set "FILESIZE=%~2"
set "EXT=%~3"

:: 4. Find the matching folder for the extension
set "TARGET_DIR="
for %%T in (%TYPES%) do (
    for /f "tokens=1,2 delims=|" %%A in ("%%T") do (
        if /i "%EXT%"=="%%A" (
            set "TARGET_DIR=%%B"
        )
    )
)

:: 5. If extension is not in the list, skip
if not defined TARGET_DIR goto :eof

:: 6. Create the base directory if it doesn't exist
if not exist "%TARGET_DIR%" mkdir "%TARGET_DIR%"

:: 7. Check if the current file fits the 100MB directory limit
set "CURRENT_SIZE=0"
for /f "tokens=*" %%S in ('dir /b /a-d "%TARGET_DIR%\*.*" 2^>nul ^| find /v /c ""') do (
    set "FILE_COUNT=%%S"
)

:: Calculate current directory size recursively
set "TOTAL_BYTES=0"
for /r "%TARGET_DIR%" %%I in (*) do (
    set /a "TOTAL_BYTES+=%%~zI"
)

set /a "NEW_SIZE=%TOTAL_BYTES% + %FILESIZE%"

:: 8. Move the file if it fits, or create a new sub-folder if it exceeds 100MB
if %NEW_SIZE% gtr %MAX_SIZE% (
    :: Find the next available incremental folder (e.g., Images_2, Images_3)
    set "INDEX=2"
    :CheckFolder
    if exist "%TARGET_DIR%_!INDEX!" (
        :: Check the new folder size as well
        set "SUB_BYTES=0"
        for /r "%TARGET_DIR%_!INDEX!" %%J in (*) do (
            set /a "SUB_BYTES+=%%~jJ"
        )
        set /a "SUB_NEW_SIZE=!SUB_BYTES! + %FILESIZE%"
      
        if !SUB_NEW_SIZE! ltr %MAX_SIZE% (
            set "TARGET_DIR=%TARGET_DIR%_!INDEX!"
            goto MoveFile
        )
      
        set /a "INDEX+=1"
        goto CheckFolder
    ) else (
        set "TARGET_DIR=%TARGET_DIR%_!INDEX!"
        mkdir "%TARGET_DIR%"
    )
)

:MoveFile
move "%FILENAME%" "%TARGET_DIR%\"
echo Moved: %FILENAME% to %TARGET_DIR%
goto :eof

::=========================================================================

Rem: How to Customize It:
Rem: Change the File Limit:
Rem: Change the MAX_SIZE variable if you need a different limit.
Rem: The script calculates using bytes (104857600 bytes = 100 MB).

Rem: Add More Extensions:
Rem: In the TYPES section, you can add new rules separated by spaces using
Rem: the format extension|FolderName. (e.g., mp3|Music or zip|Archives).
Rem: The script is not case-sensitive.

Rem: How It Works:

Rem: * It looks at every file in the directory.

Rem: * It checks the file extension and maps it to a target folder name
Rem:   (e.g., .mp4 files go to Videos).

Rem: * It calculates the total size of files already inside the destination
Rem:   directory.

Rem: * If moving the file makes the folder exceed 100 MB, the script
Rem:   automatically creates a new directory (e.g., Videos_2, Videos_3) and
Rem:   moves the file there instead.

::=========================================================================
 
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
This AI response might be more of what I'm looking for...

Code:
@echo off
setlocal enabledelayedexpansion

:: 1. CONFIGURE YOUR PATHS AND LIMITS
set "SourcePath=C:\Your\Source\Folder"
set "TargetPath=C:\Your\Target\Folder"
:: Separate extensions with a space. Ex: "*.pdf *.docx *.xlsx"
set "ExtList=*.pdf *.docx *.txt"

:: 100MB in bytes = 104,857,600
set "MaxSize=104857600"
set "CurrentSize=0"
set "SubFolderCounter=1"

:: 2. CREATE TARGET DIRECTORY
mkdir "%TargetPath%\Batch_!SubFolderCounter!" >nul

:: 3. SCAN AND MOVE FILES
for /R "%SourcePath%" %%F in (%ExtList%) do (
    if exist "%%F" (
        :: Calculate new total size
        set /a "CurrentSize+=%%~zF"
        
        :: Check if limit exceeded
        if !CurrentSize! gtr %MaxSize% (
            :: Create new folder and reset
            set /a "SubFolderCounter+=1"
            mkdir "%TargetPath%\Batch_!SubFolderCounter!" >nul
            set "CurrentSize=%%~zF"
        )

        :: Move file and preserve folder structure if desired
        robocopy "%%~dpF" "%TargetPath%\Batch_!SubFolderCounter!%%~pF" "%%~nxF" /MOV >nul
    )
)

echo File movement complete.
pause
 

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
I noticed I mispelt some of the keywords in the previous AI search.
Another AI response...

Code:
@echo off
setlocal enabledelayedexpansion

:: CONFIGURATION
set "SourceDir=C:\Path\To\Source"
set "TargetBase=D:\Path\To\Target"
set /a MaxSize=104857600

:: PROCESS MP3 FILES
for /R "%SourceDir%" %%F in (*.mp3) do (
    if exist "%%F" if %%~zF LSS %MaxSize% (
        if not exist "%TargetBase%\Audio" mkdir "%TargetBase%\Audio"
        move "%%F" "%TargetBase%\Audio\"
    )
)

:: PROCESS JPG FILES
for /R "%SourceDir%" %%F in (*.jpg *.jpeg) do (
    if exist "%%F" if %%~zF LSS %MaxSize% (
        if not exist "%TargetBase%\Images" mkdir "%TargetBase%\Images"
        move "%%F" "%TargetBase%\Images\"
    )
)

echo File transfer complete.
pause
 

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
Another AI response.
As always..."USE WITH CAUTION"

::=========================================================================

To handle this complex moving logic entirely within a Windows 7 CMD
batch script, you must use a nested FOR /R loop combined with file-size
evaluation logic. Because Windows batch file-size variables evaluate
instantly, the script requires Delayed Environment Expansion (SETLOCAL
ENABLEDELAYEDEXPANSION) to accurately calculate individual file sizes on
the fly.

## The CMD Batch Script
Save the following code as a .bat file (e.g., move_files.bat). You can
run this from any directory. [1]

::=========================================================================
+++
@echo off
setlocal enabledelayedexpansion

:: ==========================================
:: CONFIGURATION
:: ==========================================
set "source_dir=C:\Path\To\Source"
set "target_dir=D:\Path\To\Target"
set "file_extensions=*.jpg *.png *.mp4 *.pdf"
set /a max_size=104857600

:: ==========================================
:: EXECUTION
:: ==========================================
echo Starting file relocation...

for %%E in (%file_extensions%) do (
for /r "%source_dir%" %%F in (%%E) do (
if exist "%%F" (
set "file_size=%%~zF"
if !file_size! LSS %max_size% (

:: Mirror the source subdirectory structure inside target directory
set "current_source_dir=%%~dpF"
set "relative_path=!current_source_dir:%source_dir%=!"
set "destination_path=%target_dir%!relative_path!"

:: Create target directory if it does not exist
if not exist "!destination_path!" mkdir "!destination_path!"

:: Move the file
move "%%F" "!destination_path!" >nul
echo Moved: %%~nxF
)
)
)
)

echo Done.
pause

## Key Parameter Settings

* source_dir: The absolute path to your source folder. It will scan all subfolders recursively.
* target_dir: The absolute root path where files will be moved.
* file_extensions: A space-separated list of file extensions you want to target.
* max_size: The exact byte limit. 104857600 bytes equals exactly 100 MB (100 × 1024 × 1024). [2]

## How the Script Works

1. Iterates Extensions: The outer for %%E loop processes your specified file types one by one. [3]
2. Recursive Search: The inner for /r loop targets the source_dir explicitly, allowing you to run the script from any directory without relying on the current active command-line path.
3. Size Filtering: The condition if !file_size! LSS %max_size% checks if the file is strictly Less Than 100 MB before taking action.
4. Structure Mirroring: The script strips the source_dir path out of the file's folder location. It uses the remaining relative path to reconstruct an identical folder tree inside target_dir before executing the move command.

If you want to test the script safely before moving your data, you can replace the word move with echo move to preview exactly which files will be moved and where they will go.
If you run into path errors, let me know:

* Do your folder paths contain spaces or special characters?
* Do you want to overwrite files if duplicates exist in the target folders?
* Should files exactly matching 100 MB be included or excluded?


[1] [https://learnconfigmgrintune.wordpress.com](https://learnconfigmgrintune.wordpress.com/2018/11/20/it-is-all-relative/)
[2] [https://multicommander.com](http://multicommander.com/docs/filetypesetup)
[3] [https://superuser.com](https://superuser.com/questions/1029080/moving-multiple-files-to-different-directories-via-commandline)

::=========================================================================
 

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
Throughout all this AI response's, it would seem that most of them would carry crumbs to or even a solution

Last AI response I checked seemed to suggest to contain more of those "solution-crumbs"

The chosen solution at the page above might be or contain what I'm looking for.

Batch script​

Here's a script which does just that. It has four parameters, the last one being optional: the source folder; the destination folder; the target size in bytes; the file extensions that should be matched. All parameters but the size require quotes.

@echo off<br>setlocal<br>setlocal enabledelayedexpansion<br><br>if "%~3" == "" exit /b 3<br>if "%~2" == "" exit /b 2<br>pushd "%~1" 2&gt;nul || exit /b 4<br>pushd "%~2" 2&gt;nul &amp;&amp; popd || exit /b 5<br><br>set target=%~3<br>set /a target=%target:~0,-3% 2&gt;nul || exit /b 6<br>set /a min=%target% / 1000<br><br>for /r "%~2" %%G in (*) do (<br>set size=%%~zG<br>set /a size=!size:~0,-3! 2&gt;nul<br>if !errorlevel! equ 0 (<br>if !size! gtr !target! exit /b 7<br>set /a target -= !size!<br>))<br><br>if %target% leq %min% goto :end<br>set exts=* &amp; if not "%~4" == "" set exts=%~4<br><br>for /f "delims=" %%G in ('dir %exts% /a:-d-h-s /b /o:-s 2^&gt;nul') do (<br>if !target! leq %min% goto :end<br>set size=%%~zG<br>set /a size=!size:~0,-3! 2&gt;nul<br>if !errorlevel! equ 0 if !size! leq !target! (<br>echo n 2&gt;nul|move /-y "%%~fG" "%~2" &gt;nul 2&gt;&amp;1 &amp;&amp; set /a target -= !size!<br>))<br>:end<br>popd<br>endlocal &amp; exit /b 0<br>

How it works​

The first lines ensure that the amount of parameters is correct. Each parameter is validated in order to reject non-existing folders and/or bad size values. To avoid integer overflow, the last 3 digits of the target size are truncated. A threshold value is set to 1/1000th of the target to provide some tolerance.

In order to account existing files, the content of the destination folder gets scanned and its total size gets computed. The resulting value is subtracted from the target size.

At this point the actual work begins: the source folder content is enumerated and sorted by file size, from biggest to smallest. Files get moved only when their size is less or equal to the current target, which is then updated. This greedy search continue until there are no more files or the minimum threshold is reached, whichever comes first.

Remarks​

  • The command interpreter uses 32-bit signed integers to perform arithmetic operations, thus the highest positive value is 2^31 - 1 = 2147483647.
  • The target size can be any value (in bytes) between 1 KB and 2 TB.
  • Files with the Hidden or System attribute set are ignored.
  • In case the source folder contain one or more files which are already present in the destination folder, the move operation will be skipped to avoid potential data loss.
  • For performance reasons, the script used a cached value rather then enumerating files at every step to find the size of the destination folder. Files that get skipped because they share the same name will still add up to the total, and the resulting size can be skewed a result.
  • Subfolders aren't processed. If you need to, add the /s parameter to the dir command. Since the source structure gets flattened, you might end with identical named files that wouldn't be moved to the destination.
 

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
Now to quit playing with these AI response's and put more effort into the script. :cool:
 

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
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
one for more idea's...

Here is a Windows 7 batch script to move files from subdirectories based on extensions and a size limit.

The Batch Script​

Create a text file, paste the code below, and save it as movefiles.bat.
<span>@echo off<br>setlocal enabledelayedexpansion<br><br>:: CONFIGURATION<br>set "SOURCE_DIR=C:\Your\Source\Folder"<br>set "DEST_DIR=C:\Your\Destination\Folder"<br>set "EXTENSIONS=.txt .jpg .pdf"<br>set /a MAX_SIZE=100*1024*1024<br><br>:: PROCESS FILES<br>for /r "%SOURCE_DIR%" %%F in (*) do (<br> set "FILE_EXT=%%~xF"<br> <br> for %%E in (%EXTENSIONS%) do (<br> if /i "!FILE_EXT!"=="%%E" (<br> if %%~zF LSS %MAX_SIZE% (<br> echo Moving: "%%F"<br> move "%%F" "%DEST_DIR%\"<br> )<br> )<br> )<br>)<br><br>echo Done!<br>pause<br></span>

How to Customize It​


  • Folders: Change C:\Your\Source\Folder and C:\Your\Destination\Folder to your actual folder paths.
  • Extensions: Update .txt .jpg .pdf to the file extensions you want to target, separated by spaces. Include the dots.
  • Size Limit: Change 100 in the set /a MAX_SIZE=100*1024*1024 line if you need a different Megabyte limit.

Important Notes​


  • Subdirectories: The /r switch ensures the script searches all nested folders.
  • Flat Destination: This script moves all matching files into a single destination folder. If two files in different subfolders share the exact same name, the second file will prompt to overwrite the first.
  • Size Operator: The LSS operator means "Less Than". Files exactly at 100MB or larger will be skipped.
Would you like to modify this script to recreate the original subfolder structure in the destination folder, or should it rename files automatically to prevent overwriting duplicates?
 

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
I search "windows 7 64 bit batch cmd move multiple files from multiple subdirectories based on extension 100mb max limit renaming from original deleting empty source subdirectories"

========================================================

AI Overview




Use this batch script to recursively process your files. It matches your target extension, checks if the file is ≤ 100 MB, renames the file by prefixing its parent folder name to prevent duplicates, moves it to the destination, and deletes the source folders if they are empty. [1, 2, 3, 4, 5]

cmd
<span data-copy-service-computed-style="font-family: monospace; font-size: 14px; font-weight: 400; margin: 0px; text-decoration: none; border-bottom: 0px rgb(0, 29, 53);">@echo off<br>setlocal enabledelayedexpansion<br><br>:: 1. SET YOUR VARIABLES BELOW<br>set "SourceDir=C:\Path\To\Source"<br>set "DestDir=C:\Path\To\Destination"<br>set "Ext=txt"<br><br>:: 2. CREATE DESTINATION FOLDER<br>if not exist "%DestDir%" mkdir "%DestDir%"<br><br>:: 3. PROCESS FILES<br>cd /d "%SourceDir%"<br>for /r %%F in (*.%Ext%) do (<br> if "%%~aF" neq "" (<br> if %%~zF leq 104857600 (<br> for %%I in ("%%~dpF.") do set "ParentName=%%~nxI"<br> move "%%F" "%DestDir%\!ParentName!_%%~nxF"<br> )<br> )<br>)<br><br>:: 4. REMOVE EMPTY DIRECTORIES (Bottom-Up)<br>for /f "tokens=* delims=" %%D in ('dir /ad /b /s ^| sort /r') do (<br> rd "%%D" 2&gt;nul<br>)<br><br>echo Done!<br>pause<br></span>
Use code with caution.


How to use it:
  1. Open Notepad.
  2. Paste the code block above into the document.
  3. Modify the variables in section 1 (SourceDir, DestDir, and Ext) with your exact directory paths and the file extension you want to isolate. (Note: The size is set in bytes: 100MB = 104857600bytes).
  4. Click File > Save As and name it something like MoveAndClean.bat.
  5. Set the "Save as type" dropdown to All Files and save it.
  6. Double-click the .bat file to run it. [1, 2, 3, 4]

If you want, I can help you:
  • Change the script to copy instead of move so you keep a backup.
  • Filter by date (e.g., only move files older or newer than a specific date).
Let me know how you'd like to proceed!



1783816386437.png1783816386456.png1783816386466.png
6 sites

  • How to rename multiple files in subfolders using Windows command prompt - DEV Community
    Aug 28, 2021 — To rename files recursively in subfolders using the Windows command prompt, you can use the REN (rename) command: * **FOR /R %G IN...
    1783816386475.png
    1783816386484.png
    DEV Community


  • move cmd command - GeeksforGeeks
    Oct 20, 2020 — move cmd command * [drive:][path]filename1 - Specifies the location and name of the file or files you want to move. * destination ...
    1783816386493.png
    1783816386501.png
    GeeksforGeeks


  • One-line batch script to delete empty directories
    Apr 17, 2008 — Raymond Chen Show more You don't need a whole 260KB program to do it. This batch file does the trick just fine: for /f "usebackq" ...
    1783816386514.png
    1783816386530.png
    Microsoft Dev Blogs
Show all







AI can make mistakes, so double-check responses

----------------------------------------------------------------------------------------------------------------------------------------------------------

========================================================================================

AI Mode

To move files from multiple subdirectories based on extension, filter by size, rename them to avoid conflicts, and delete empty folders, use the script below.
Windows Batch cannot natively read file sizes in megabytes easily, so the script converts 100MB into bytes (104,857,600 bytes).

The Batch Script​

Create a text file, paste this code, and save it as move_files.bat. [1, 2]
<span>@echo off<br>setlocal enabledelayedexpansion<br><br>:: --- CONFIGURATION ---<br>set "source_dir=C:\path\to\source"<br>set "dest_dir=C:\path\to\destination"<br>set "ext=*.txt"<br>set /a max_size=104857600<br>:: ---------------------<br><br>echo Processing files...<br><br>:: Loop through all subdirectories and match extension<br>for /r "%source_dir%" %%F in (%ext%) do (<br> if exist "%%F" (<br> :: Check file size limit<br> if %%~zF LSS %max_size% (<br> set "filename=%%~nxF"<br> set "filepath=%%~fF"<br> <br> :: Check for duplicate in destination<br> if exist "%dest_dir%\%%~nxF" (<br> set "count=1"<br> :loop<br> if exist "%dest_dir%\%%~nF_!count!%%~xF" (<br> set /a count+=1<br> goto loop<br> )<br> set "filename=%%~nF_!count!%%~xF"<br> )<br> <br> :: Move and rename the file<br> move "%%F" "%dest_dir%\!filename!" &gt;nul<br> )<br> )<br>)<br><br>echo Cleaning up empty directories...<br><br>:: Delete empty source subdirectories<br>for /f "delims=" %%D in ('dir "%source_dir%" /ad /b /s ^| sort /r') do (<br> rd "%%D" 2&gt;nul<br>)<br><br>echo Done!<br>pause<br></span>

How to Customize It​


  • source_dir: Change to your parent folder path.
  • dest_dir: Change to your target folder path.
  • ext: Change *.txt to your target extension (e.g., *.mp4, *.jpg).
  • Renaming Logic: If image.jpg already exists in the destination, the script renames the incoming file to image_1.jpg, image_2.jpg, etc.
  • Directory Cleanup: The sort /r command reverses the directory list. This ensures deepest subdirectories delete first, allowing parent folders to become empty and delete successfully.

Safety Warning​

Always test Batch scripts on a small test folder with copies of your files before running them on your live data.
If you want to modify this script, let me know:


[1] https://www.youtube.com
[2] https://www.youtube.com
 

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