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


zelarra821

Active member
Member
Local time
2:30 AM
Posts
102
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.4061
    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
    2x1tb Solidigm m.2 nvme /External drives 512gb Samsung m.2 sata+2tb Kingston m2.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
    #1 Edge #2 Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 11 Pro 24H2 26100.4061
    Computer type
    PC/Desktop
    Manufacturer/Model
    Beelink Mini PC SER5
    CPU
    AMD Ryzen 7 6800U
    Memory
    32 gb
    Graphics card(s)
    integrated
    Sound Card
    integrated
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1TB Crucial nvme
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    still too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender
    Other Info
    System 3 is non compliant Dell 9020 i7-4770/24gb ram Win11 PRO 26100.4061
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.5472
    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
    5 x LG 25MS500-B - 1 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
    1000/400Mbps
    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
    Antivirus
    Defender / Malwarebytes
    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.5472
    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
    5 x LG 25MS500-B - 1 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
    1000/400Mbps
    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
    Antivirus
    Defender / Malwarebytes
    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
    *
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
    *
Code:
@echo off

setlocal enabledelayedexpansion

set "FOLDER="%D:\Bandeja de entrada\000 M¢vil\""

SET "ext[exclude]=.sh"

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.

SET choice=

SET /p choice=Move files? [Y/N]:

IF NOT '%choice%'=='' SET choice=%choice:~0,1%

IF '%choice%'=='Y' GOTO Yes
IF '%choice%'=='y' GOTO Yes

IF '%choice%'=='N' GOTO No
IF '%choice%'=='n' GOTO No
IF '%choice%'=='' GOTO No

ECHO "%choice%" is not valid

ECHO.

GOTO start

:Yes

move /y "D:\Bandeja de entrada\000 M¢vil\Audios\*.*" "D:\Bandeja de entrada\Audios"

move /y "D:\Bandeja de entrada\000 M¢vil\Documentos\*.*" "D:\Bandeja de entrada\Documentos"

move /y "D:\Bandeja de entrada\000 M¢vil\Im genes\*.*" "D:\Bandeja de entrada\Im genes"

move /y "D:\Bandeja de entrada\000 M¢vil\V¡deos\*.*" "D:\Bandeja de entrada\V¡deos"

move /y "D:\Bandeja de entrada\000 M¢vil\ZZZ Comprobar\*.*" "D:\Bandeja de entrada\ZZZ Comprobar"

:No

EXIT

@pause

endlocal
 

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
    *

My Computer

System One

  • OS
    Windows 11 Stable, Release Preview, Beta, Dev, Canary
    Computer type
    PC/Desktop
    Manufacturer/Model
    Micro-Star International Co., Ltd. MS-7D54
    CPU
    AMD Ryzen 9 PRO 5950X
    Motherboard
    MSI MAG X570S Tomahawk Max WiFi (MS-7D54) (2 PCI-E x1, 2 PCI-E x16, 2 M.2, 4 DDR4 DIMM, Audio, Video, 2.5GbE LAN, WiFi)
    Memory
    128 Gb
    Graphics Card(s)
    Intel(R) Arc(TM) A380 Graphics (2 ГБ)
    Sound Card
    Устройство с поддержкой High Definition Audio
    Monitor(s) Displays
    HP X34 [34" LCD] (6CM1300J5J) {2021}
    Screen Resolution
    3440x1440
    Hard Drives
    KINGSTON SFYRD2000G (2000 ГБ, PCI-E 4.0 x4)
    PSU
    Be Quiet! Dark Rock Pro 4
    Case
    Cooler Master HAF XB EVO
    Internet Speed
    1000 Mb/s
    Browser
    Chrome
    Antivirus
    Windows Defender
It's a accent, so I have to write in that way
 

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
    *

My Computer

System One

  • OS
    Windows 11 Stable, Release Preview, Beta, Dev, Canary
    Computer type
    PC/Desktop
    Manufacturer/Model
    Micro-Star International Co., Ltd. MS-7D54
    CPU
    AMD Ryzen 9 PRO 5950X
    Motherboard
    MSI MAG X570S Tomahawk Max WiFi (MS-7D54) (2 PCI-E x1, 2 PCI-E x16, 2 M.2, 4 DDR4 DIMM, Audio, Video, 2.5GbE LAN, WiFi)
    Memory
    128 Gb
    Graphics Card(s)
    Intel(R) Arc(TM) A380 Graphics (2 ГБ)
    Sound Card
    Устройство с поддержкой High Definition Audio
    Monitor(s) Displays
    HP X34 [34" LCD] (6CM1300J5J) {2021}
    Screen Resolution
    3440x1440
    Hard Drives
    KINGSTON SFYRD2000G (2000 ГБ, PCI-E 4.0 x4)
    PSU
    Be Quiet! Dark Rock Pro 4
    Case
    Cooler Master HAF XB EVO
    Internet Speed
    1000 Mb/s
    Browser
    Chrome
    Antivirus
    Windows Defender
Not for me.

I need to know where I should put SET "ext[exclude]=.sh" in that code, please
 

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
    *
Could someone help me?
 

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'm not familiar with "ext[exclude]" for DIR.

dir /b /a:-d !FOLDER! 2>NUL | findstr /v /i "\!IGNORE_EXT!$" | find /c /v ""

dir /b /a:-d !FOLDER! 2>NUL

Redirect STDERR to NUL, so we don't see any "File Not Found" messages.

findstr /v /i "\!IGNORE_EXT!$"
Filter out a trailing file extension.

find /c /v ""
Count all non-empty lines.

Code:
@echo off
setlocal enabledelayedexpansion

set "FOLDER=%1"
set "IGNORE_EXT=.sh"

echo Running dir.
echo.
dir !FOLDER!

for /f %%n in ('dir /b /a:-d !FOLDER! 2^>NUL ^| findstr /v /i "\!IGNORE_EXT!$" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files found.
        exit /b
    )
)

echo.
echo Continue with my script.

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

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

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

02/08/2025  09:45 AM    <DIR>          .
02/08/2025  09:45 AM    <DIR>          ..
02/08/2025  09:17 AM    <DIR>          Audios
02/08/2025  09:18 AM                 2 Delete.sh
02/08/2025  09:17 AM    <DIR>          Documentos
02/08/2025  09:17 AM    <DIR>          Imagenes
02/08/2025  09:18 AM                 2 Move.sh
02/08/2025  09:45 AM                 4 NORMAL.TXT
02/08/2025  09:18 AM    <DIR>          Vidoes
02/08/2025  09:18 AM    <DIR>          ZZZ Comprobar
               3 File(s)              8 bytes
               7 Dir(s)  3,339,671,646,208 bytes free

Continue with my script.

C:\Users\GARLIN\Downloads>del DIEGO\NORMAL.TXT

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

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

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

02/08/2025  09:46 AM    <DIR>          .
02/08/2025  09:46 AM    <DIR>          ..
02/08/2025  09:17 AM    <DIR>          Audios
02/08/2025  09:18 AM                 2 Delete.sh
02/08/2025  09:17 AM    <DIR>          Documentos
02/08/2025  09:17 AM    <DIR>          Imagenes
02/08/2025  09:18 AM                 2 Move.sh
02/08/2025  09:18 AM    <DIR>          Vidoes
02/08/2025  09:18 AM    <DIR>          ZZZ Comprobar
               2 File(s)              4 bytes
               7 Dir(s)  3,339,671,646,208 bytes free

No files found.
 

My Computer

System One

  • OS
    Windows 7

Latest Support Threads

Back
Top Bottom