2

Trying to get this to work.

Getting this line from dir: 3 File(s) 7,332,731,128 bytes and trying to just get the number of files to use for addition.

@Echo ON

dir %1 /a:h | find "File(s)" > hidden.txt
dir %1 | find "File(s)" > reg.txt

set /p hidden=<hidden.txt
echo %hidden%
IF /I "%hidden%"=="File Not Found" (
    Set hidden = 0
    ) Else (
Set hidden=%hidden~-29%)

echo %hidden%
Set /p reg=<reg.txt
IF /I "%hidden%"=="File Not Found" (
    set reg = 0
    ) ELSE (
    Set reg=%reg~-29%)
set /a total = %reg% + %hidden%

Echo The total number of files in the %1 directory is: %total%. This includes hidden files.
Echo.
Echo The total number of non-hidden files in the %1 directory is: %reg%.
Echo.
Echo The total number of hidden files in the %1 directory is: %hidden%
2
  • 1
    You can use a FOR /F command to either read the output file you created or parse the output of the DIR and FIND commands. By default the output will be split by the first space. So you can assign the FOR variable to an environmental variable to use later in your script. Commented Dec 12, 2017 at 16:45
  • A better option would be to use the /C option of the FIND command to count the number of files. dir "%1"/a:h-d /b|find /c /v "" Commented Dec 12, 2017 at 16:49

3 Answers 3

1

you don't need temp files which only will slow down your script:

if you want to count the hidden files try this

@echo off
set counter=0
for /f %%# in ('dir /a:h-d "%~1"') do (
  set /a counter=counter+1
)
echo hidden files=%counter%

to count all files

@echo off
set counter=0
for /f %%# in ('dir /a:-d "%~1"') do (
  set /a counter=counter+1
)
echo all files=%counter%
Sign up to request clarification or add additional context in comments.

Comments

1

This is just another For loop option:

@For /F %%A In ('Dir/AH-D-L "%~1" 2^>Nul') Do @If Not "%%A"=="0" Set "fileCount=%%A"
@Echo %fileCount%
@Pause

Remove -L, if you wish not to exclude junction points from your file count.

Comments

0

I've got the same result with just two FOR loops. Using sentences between single quotes inside the parentheses we can execute commands and store results.

I've left the same Result Text just to let you compare and see if this is what you're looking for:

CODE:

@echo off
setlocal

for /f %%A in ('dir "%~1" /a-h-d /b ^| find /V /C ""') do set "visCount=%%A"
for /f %%A in ('dir "%~1" /ah-d /b ^| find /V /C ""') do set "hidCount=%%A"

set /a totalCount = visCount + hidCount

echo.
echo The total number of files in the %1 directory is: %totalCount%. This includes hidden files.
echo.
echo The total number of non-hidden files in the %1 directory is: %visCount%.
echo.
echo The total number of hidden files in the %1 directory is: %hidCount%

With /B parameter of DIR command I only get one line per file and no more text, and with /A I can select Hidden or not files and no directories.

Then SET /A command let us use arithmetics operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.