Check if there are files in a folder in a BAT file


zelarra821

Member
Member
Local time
10:58 AM
Posts
83
OS
Windows 11 Home 23H2
Hello.

I want to check if a folder is empty or not.

- If true: message asking if I want to perform step 2.

- If false: message indicating that the folder is empty and closing the CMD window.

* IMPORTANT: when checking if a folder is empty or not, you must subtract two from the number of files, as there are two that always have to be there.

Thank you very much.
 

My Computer

System One

  • OS
    Windows 11 Home 23H2
    Computer type
    Laptop
    Manufacturer/Model
    *
    CPU
    *
    Motherboard
    *
    Memory
    *
    Graphics Card(s)
    *
    Sound Card
    *
    Monitor(s) Displays
    *
    Screen Resolution
    *
    Hard Drives
    *
    PSU
    *
    Case
    *
    Cooling
    *
    Keyboard
    *
    Mouse
    *
    Internet Speed
    *
    Browser
    *
    Antivirus
    *
    Other Info
    *
Don't know if it will help for your exact requirement or not but check this out.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 24H2 26100.2314
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1tb Solidigm m.2 nvme+256gb SKHynix m.2 nvme /External drives 512gb Samsung m.2 sata+1tb Kingston m2.nvme+ 4gb Solidigm nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 10 Pro 22H2 19045.3930
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 9020
    CPU
    i7-4770
    Memory
    24 gb
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    256 gb Toshiba BG4 M.2 NVE SSB and 1 tb hdd
    PSU
    500w
    Case
    MT
    Cooling
    Dell factory
    Mouse
    Logitech wireless
    Keyboard
    Logitech wired
    Internet Speed
    still not telling
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
Hi.

The article is very cool, thank you very much, but it doesn't work for me.

I need to get a boolean value to use in an If and perform other tasks that I already have prepared.
 

My Computer

System One

  • OS
    Windows 11 Home 23H2
    Computer type
    Laptop
    Manufacturer/Model
    *
    CPU
    *
    Motherboard
    *
    Memory
    *
    Graphics Card(s)
    *
    Sound Card
    *
    Monitor(s) Displays
    *
    Screen Resolution
    *
    Hard Drives
    *
    PSU
    *
    Case
    *
    Cooling
    *
    Keyboard
    *
    Mouse
    *
    Internet Speed
    *
    Browser
    *
    Antivirus
    *
    Other Info
    *
I need to get a boolean value to use in an If and perform other tasks that I already have prepared.

What tasks, how do you have them prepared?
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 Build 22631.4249
    Computer type
    PC/Desktop
    Manufacturer/Model
    Sin-built
    CPU
    Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz (4th Gen?)
    Motherboard
    ASUS ROG Maximus VI Formula
    Memory
    32.0 GB of I forget and the box is in storage.
    Graphics Card(s)
    Gigabyte nVidia GeForce GTX 1660 Super OC 6GB
    Sound Card
    Onboard
    Monitor(s) Displays
    4 x LG 23MP75 - 2 x 24MK430H-B - 1 x Wacom Pro 22" Tablet
    Screen Resolution
    All over the place
    Hard Drives
    Too many to list.
    OS on Samsung 1TB 870 QVO SATA
    PSU
    Silverstone 1500
    Case
    NZXT Phantom 820 Full-Tower Case
    Cooling
    Noctua NH-D15 Elite Class Dual Tower CPU Cooler / 6 x EziDIY 120mm / 2 x Corsair 140mm somethings / 1 x 140mm Thermaltake something / 2 x 200mm Corsair.
    Keyboard
    Corsair K95 / Logitech diNovo Edge Wireless
    Mouse
    Logitech G402 / G502 / Mx Masters / MX Air Cordless
    Internet Speed
    100/40Mbps
    Browser
    All sorts
    Antivirus
    Kaspersky Premium
    Other Info
    I’m on a horse.
  • Operating System
    Windows 11 Pro 23H2 Build: 22631.4249
    Computer type
    Laptop
    Manufacturer/Model
    LENOVO Yoga 7i EVO OLED 14" Touchscreen i5 12 Core 16GB/512GB
    CPU
    Intel Core 12th Gen i5-1240P Processor (1.7 - 4.4GHz)
    Memory
    16GB LPDDR5 RAM
    Graphics card(s)
    Intel Iris Xe Graphics Processor
    Sound Card
    Optimized with Dolby Atmos®
    Screen Resolution
    QHD 2880 x 1800 OLED
    Hard Drives
    M.2 512GB
    Other Info
    …still on a horse.
dir /b /a:-d

/b -> brief (no output except for directory or file names)
/a:-d -> ignore all directories

Code:
@echo off
setlocal enabledelayedexpansion

set "FOLDER=%1"

echo Running dir.
echo.
dir !FOLDER!

for /f %%n in ('dir /b /a:-d !FOLDER! 2^>^&1 ^| find /c "File Not Found"') do (
    set COUNT=%%n
    if !COUNT! equ 1 (
        echo.
        echo No files found.
        exit /b
    )
)

echo.
echo Continue with my script.

endlocal
Code:
C:\Users\GARLIN\Downloads>TEST.bat FOLDER_WITH_FILES
Running dir.

 Volume in drive C has no label.
 Volume Serial Number is 5685-48D7

 Directory of C:\Users\GARLIN\Downloads\FOLDER_WITH_FILES

02/07/2025  11:28 PM    <DIR>          .
02/07/2025  11:28 PM    <DIR>          ..
09/18/2024  10:01 PM           400,738 ViVeTool-v0.3.3.zip
               1 File(s)        400,738 bytes
               2 Dir(s)  3,342,836,490,240 bytes free

Continue with my script.

C:\Users\GARLIN\Downloads>TEST.bat EMPTY_FOLDER
Running dir.

 Volume in drive C has no label.
 Volume Serial Number is 5685-48D7

 Directory of C:\Users\GARLIN\Downloads\EMPTY_FOLDER

02/07/2025  11:28 PM    <DIR>          .
02/07/2025  11:28 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  3,342,836,490,240 bytes free

No files found.
 

My Computer

System One

  • OS
    Windows 7
Batch:
@echo off
setlocal

REM Set the folder path
set "folderPath=C:\path\to\your\folder"

REM Check if the folder is empty
if exist "%folderPath%\*" (
    echo The folder is not empty.
) else (
    echo The folder is empty.
)

endlocal

Replace C:\path\to\your\folder with the path to the folder you want to check. This script checks if there are any files or subfolders in the specified folder and prints a message accordingly. Enjoy!
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
Maybe read post 3.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 Build 22631.4249
    Computer type
    PC/Desktop
    Manufacturer/Model
    Sin-built
    CPU
    Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz (4th Gen?)
    Motherboard
    ASUS ROG Maximus VI Formula
    Memory
    32.0 GB of I forget and the box is in storage.
    Graphics Card(s)
    Gigabyte nVidia GeForce GTX 1660 Super OC 6GB
    Sound Card
    Onboard
    Monitor(s) Displays
    4 x LG 23MP75 - 2 x 24MK430H-B - 1 x Wacom Pro 22" Tablet
    Screen Resolution
    All over the place
    Hard Drives
    Too many to list.
    OS on Samsung 1TB 870 QVO SATA
    PSU
    Silverstone 1500
    Case
    NZXT Phantom 820 Full-Tower Case
    Cooling
    Noctua NH-D15 Elite Class Dual Tower CPU Cooler / 6 x EziDIY 120mm / 2 x Corsair 140mm somethings / 1 x 140mm Thermaltake something / 2 x 200mm Corsair.
    Keyboard
    Corsair K95 / Logitech diNovo Edge Wireless
    Mouse
    Logitech G402 / G502 / Mx Masters / MX Air Cordless
    Internet Speed
    100/40Mbps
    Browser
    All sorts
    Antivirus
    Kaspersky Premium
    Other Info
    I’m on a horse.
  • Operating System
    Windows 11 Pro 23H2 Build: 22631.4249
    Computer type
    Laptop
    Manufacturer/Model
    LENOVO Yoga 7i EVO OLED 14" Touchscreen i5 12 Core 16GB/512GB
    CPU
    Intel Core 12th Gen i5-1240P Processor (1.7 - 4.4GHz)
    Memory
    16GB LPDDR5 RAM
    Graphics card(s)
    Intel Iris Xe Graphics Processor
    Sound Card
    Optimized with Dolby Atmos®
    Screen Resolution
    QHD 2880 x 1800 OLED
    Hard Drives
    M.2 512GB
    Other Info
    …still on a horse.
Far as i know there is no boolean value in batch scripting you have to declare it yourself for example using "Set True=1" or "Set False=0" commands.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP Pavilion
    CPU
    AMD Ryzen 7 5700G
    Motherboard
    Erica6
    Memory
    Micron Technology DDR4-3200 16GB
    Graphics Card(s)
    NVIDIA GeForce RTX 3060
    Sound Card
    Realtek ALC671
    Monitor(s) Displays
    Samsung SyncMaster U28E590
    Screen Resolution
    3840 x 2160
    Hard Drives
    SAMSUNG MZVLQ1T0HALB-000H1
if exist "%folderPath%\*" (
echo The folder is not empty.
) else (
echo The folder is empty.
)
You can't use exist "\path\*" because "." and ".." are always defined for every folder.

Code:
C:\Users\GARLIN\Downloads>dir FOLDER_WITH_FILES
 Volume in drive C has no label.
 Volume Serial Number is 5685-48D7

 Directory of C:\Users\GARLIN\Downloads\FOLDER_WITH_FILES

02/07/2025  11:28 PM    <DIR>          .
02/07/2025  11:28 PM    <DIR>          ..
09/18/2024  10:01 PM           400,738 ViVeTool-v0.3.3.zip
               1 File(s)        400,738 bytes
               2 Dir(s)  3,342,824,955,904 bytes free

C:\Users\GARLIN\Downloads>if exist FOLDER_WITH_FILES\* (echo Not Empty) else (echo Empty)
Not Empty
Code:
C:\Users\GARLIN\Downloads>dir EMPTY_FOLDER
 Volume in drive C has no label.
 Volume Serial Number is 5685-48D7

 Directory of C:\Users\GARLIN\Downloads\EMPTY_FOLDER

02/07/2025  11:28 PM    <DIR>          .
02/07/2025  11:28 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  3,342,825,029,632 bytes free

C:\Users\GARLIN\Downloads>if exist EMPTY_FOLDER\* (echo Not Empty) else (echo Empty)
Not Empty
 

My Computer

System One

  • OS
    Windows 7
I share bat file I want to get.

I only need not to show this two files:

1739006543806.webp

So, it would finish if I excluded this two files because there's no more files.

Thanks to all.
 

Attachments

My Computer

System One

  • OS
    Windows 11 Home 23H2
    Computer type
    Laptop
    Manufacturer/Model
    *
    CPU
    *
    Motherboard
    *
    Memory
    *
    Graphics Card(s)
    *
    Sound Card
    *
    Monitor(s) Displays
    *
    Screen Resolution
    *
    Hard Drives
    *
    PSU
    *
    Case
    *
    Cooling
    *
    Keyboard
    *
    Mouse
    *
    Internet Speed
    *
    Browser
    *
    Antivirus
    *
    Other Info
    *
Just exclude them, or exclude all .sh

SET "ext[exclude]=.sh"
 

My Computer

System One

  • OS
    Windows 11 Pro
Where do I put that code?
 

My Computer

System One

  • OS
    Windows 11 Home 23H2
    Computer type
    Laptop
    Manufacturer/Model
    *
    CPU
    *
    Motherboard
    *
    Memory
    *
    Graphics Card(s)
    *
    Sound Card
    *
    Monitor(s) Displays
    *
    Screen Resolution
    *
    Hard Drives
    *
    PSU
    *
    Case
    *
    Cooling
    *
    Keyboard
    *
    Mouse
    *
    Internet Speed
    *
    Browser
    *
    Antivirus
    *
    Other Info
    *

Latest Support Threads

Back
Top Bottom