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


The last two questions:

If I want to exclude .sh and .txt, how should it be?

If I want to exclude two specific files, like test.docx and test.xlsx, how should it be?

Thanks.
 

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
    *
You know, this would be easier if you shared a complete problem description. Instead of asking more changes after every single answer.
 

My Computer

System One

  • OS
    Windows 7
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

1. If I want to exclude .sh and .txt, how should it be?

set "IGNORE_EXT=.sh,.txt"

2. If I want to exclude two specific files, like test.docx and test.xlsx, how should it be?

set "IGNORE_EXT=test.docx,test.xlsx"
 

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
    *
findstr can handle multiple match expressions separated by a space.
Code:
dir /b /a-d | findstr /v /i "\.sh$ \.txt$"
dir /b /a-d | findstr /v /i "\.sh$ \.txt$ \.xls$ \.pdf$"

Code:
@echo off
setlocal enabledelayedexpansion

set "FOLDER=%1"
set "IGNORE_EXT=\.sh$ \.txt$"

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
 

My Computer

System One

  • OS
    Windows 7
and how should it be if you want exclude specific files?

For example, Delete.sh and 'Budget 2024.xlsx'

Thanks
 

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 get an error:

"/v" is not recognized as an internal or external command, program, or executable batch file.
 

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
    *
The two files are:

Delete.sh

Budget 2024.xlsm
 

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:
>dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2023.xlsm"
file.sh
file.txt
You don't understand what I mean.

The files are this: one

1739053054369.webp
 

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
    *
findstr /v "pattern" -> exclude pattern
findstr /v /i "pattern1 pattern2 pattern3" -> exclude pattern1, pattern2 and pattern3

Code:
C:\Users\GARLIN\Downloads\COMCOM>dir /b
Budget 2024.xlsm
Delete.sh
NORMAL.txt

C:\Users\GARLIN\Downloads\COMCOM>dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2024.xlsm"
NORMAL.txt

C:\Users\GARLIN\Downloads\COMCOM>dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat\$"
NORMAL.txt

"\" is the escape character to protect some reserved characters from being treated differently.
"$" is the match for end of string.

"Budget 2024.xlsm" -> "Budget\ 2024.xlsm" because your filename has a space in the name. We need to protect the space as "Budget\ 2024".

"\.sh$" -> protects "." as matching character and only match if ".sh" is at the end of a file. Because we don't want to match other patterns by accident.

For example ".sh" would match "Morning.TV.Show". But it doesn't match "\.sh$" since the ".sh" must be at the end of the filename.

You can pipeline two different sets of findstr matches. findstr cannot provide a count of matches, so we need find to do the work.
Find all files, filtering out stringA, stringB, and stringC, but keeping stringD or stringE
Code:
dir /b /a-d | findstr /v /i "stringA stringB stringC" | findstr "stringD stringE" | find /c /v ""
 

My Computer

System One

  • OS
    Windows 7
I'm so sorry 😓😓

I don't get it works.

I execute bat files but it closes and I can't see anything in cmd screen.

There must be something wrong in my code:

Code:
@echo off

setlocal enabledelayedexpansion

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

set "IGNORE_EXT=\.sh$ \.txt$"

echo Running dir.

@pause

echo.
dir !FOLDER!

for /f %%n in (>dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat\$" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files found.
    @pause
        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

@pause

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
    *
You're adding syntax errors.
Code:
set "FOLDER="%D:\Bandeja de entrada\000 M¢vil\""
set "FOLDER=D:\Bandeja de entrada\000 M¢vil"

Code:
for /f %%n in ('dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat\$" ^| find /c /v ""') do (
for /f %%n in ('dir /b /a-d !FOLDER! | findstr /v /i "Delete.sh Budget\ 2024.xlsm !IGNORE_EXT!" ^| find /c /v ""') do (
 

My Computer

System One

  • OS
    Windows 7
OPTION 1

Code:
@echo off

setlocal enabledelayedexpansion

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

set "IGNORE_EXT=\.sh$ \.txt$"

echo Running dir.

echo.
dir !FOLDER!

for /f %%n in ('dir /b /a-d !FOLDER! | findstr /v /i "Delete.sh Budget\ 2024.xlsm !IGNORE_EXT!" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files found.
    @pause
        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

@pause

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

OPTION 2

Code:
@echo off

setlocal enabledelayedexpansion

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

echo Running dir.

echo.
dir !FOLDER!

for /f %%n in ('dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat\$" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files found.
    @pause
        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

@pause

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



There must be something wrong I don't find where it is. Sorry.
 

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 reedit my last post:

Code:
@echo off

setlocal enabledelayedexpansion

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

set "IGNORE_EXT=\.sh$ \.txt$"

echo Running dir.

echo.
dir !FOLDER!

for /f %%n in ('dir /b /a-d !FOLDER! | findstr /v /i "Delete.sh Budget\ 2024.xlsm !IGNORE_EXT!" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files found.
    @pause
        exit /b
    )
)

Code:
@echo off

setlocal enabledelayedexpansion

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

echo Running dir.

echo.
dir !FOLDER!

for /f %%n in ('dir /b /a-d | findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat\$" ^| find /c /v ""') do (
    set COUNT=%%n
    if !COUNT! equ 0 (
        echo.
        echo No files found.
    @pause
        exit /b
    )
)

Could somebody tell me where is the mistake? Please.

Thanks.
 

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
    *
Hello, I'm getting tired of insisting so much, but would you be so kind as to put the full line?

I'm not able to correct what you've put me with, because I've tried to put it the way I understood it, but it hasn't fixed anything, and of course, I don't know if it's because of me, or why it is.

Please. 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
    *
Code:
for /f %%n in ('dir /b /a-d !FOLDER! ^| findstr /v /i "Delete.sh Budget\ 2024.xlsm !IGNORE_EXT!" ^| find /c /v ""') do (


for /f %%n in ('dir /b /a-d ^| findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat$" ^| find /c /v ""') do (

The string "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat$" is not working, so this files appears in the total count.
 

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
    *
dir [folder name] is different than dir

Why don't you simulate the dir command by hand, without the ^'s because the ^ is only used to protect the | character from CMD.

You're making a lot of editing mistakes. Enter the command by hand, make sure it works before you copy it inside the (' ') part.
Code:
dir /b /a-d "D:\Bandeja de entrada\000 M¢vil" | findstr /v /i "Delete.sh Budget\ 2024.xlsm \.sh$ \.bat$" | find /c /v ""
 

My Computer

System One

  • OS
    Windows 7
Back
Top Bottom