Page 1 of 2

How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 15:17
by drgt
I used this

(set num=0) & (for /f %a in ('dir /a /b /s') do @echo %a & (set/a num+=1 >nul)) & (echo %num% files)

but

it does not give me all of the files (regardless attribute) and the number at the end is different on the 1st run!

Re: How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 15:25
by Ed Dyreen
'

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "?=" &set "$=" &for /f "usebackq delims= " %%? in (
   `dir /s *.*`
) do    set "$=!?!" &set "?=%%~?"

echo.Active directory: '!cd!'
echo.Number of files : '!$!'

pause
exit /b

Code: Select all

Active directory: 'F:\ADMIN\REPAIR'
Number of files : '8'
Druk op een toets om door te gaan. . .

How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 15:32
by drgt
Thanks Ed.

Just 2 comments

1. You are suggesting a batch file, can it be done on a single command line?

2. Are subdirectories included in your suggestion?

Re: How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 17:11
by aGerman
You wrote that you want to get every file. In this case you have to exclude folders using dir /a-d.
A simple way to display a number for each file is to pipe the dir output to findstr.

Code: Select all

dir /a-d /b /s|findstr /n .

It's also possible to process this output in a for /f loop.

Code: Select all

for /f "tokens=1* delims=:" %i in ('dir /a-d /b /s^|findstr /n .') do ...

... where the the number is in %i and the file name in %j.

Regards
aGerman

Re: How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 21:41
by orange_batch
drgt wrote:(echo %num% files)

Because this is part of the same command line, DOS changes %num% to it's value before actually processing any command. Either use:

setlocal enabledelayedexpansion
...(echo !num! files)


or (slower)

...(call echo %%num%% files)

How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 23:16
by drgt
@aGerman

See following error:
G:\>dir /a-d /b /s|findstr /n
FINDSTR: Bad command line

@Ed Dyreen
Your code hangs. I waited a while before I terminated it.

@orange_batch
Well the first problem is to get every file. Properties report 1098 files and with my command I only get 400 something (like I said, the number varies with every run).
Your suggestion involves a batch file and not a single command, right?

Re: How to get EVERY file in DIR including a file number

Posted: 03 Dec 2011 23:55
by orange_batch
I'm talking about modifying the code you wrote. I don't know why your number of files varies, I use dir /a:-d /b /s in an important script and it works for me.

Re: How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 00:06
by drgt
orange_batch wrote:I'm talking about modifying the code you wrote. I don't know why your number of files varies, I use dir /a:-d /b /s in an important script and it works for me.


I don't know what is wrong I just used dir /a:-d /b /s >c:\log.txt, then copied the text file onto a spreadsheet, and counted 402 entries.
However, Folder properties in windows explorer reports 1098 files in some 400 directories!!!

Re: How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 01:38
by !k
hidden & system files?

Code: Select all

setlocal enabledelayedexpansion
set "all=0"
for /f "tokens=1-3" %%x in ('dir /a-d/s/-c 2^>nul ^|findstr /c:"File(s)" /c:"Total Files"') do (
   if "!all!"=="1" set "num=%%x"
   if "%%x %%y"=="Total Files" (set all=1) else set "all=0"
)
echo !num! files
endlocal

How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 04:27
by drgt
Did not realize it is so difficult to get a complete file list regardless attribute setting!!!

@!k

Here the result of your code: (NOTE that I added G: after dir, because g is the drive whose contents we want to list).

Code: Select all

C:\>setlocal enabledelayedexpansion

C:\>set "all=0"

C:\>for /F "tokens=1-3" %x in ('dir g: /a-d/s/-c 2>nul |findstr /c:"File(s)"') d
o (
if "!all!" == "1" set "num=%x"
 if "%y" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=20"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=12"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=10"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=93"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=10"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=44"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=20"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=7"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=1"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=81"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=67"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=30"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>(
if "!all!" == "1" set "num=7"
 if "File(s)" == "File(s):" (set all=1 )  else set "all=0"
)

C:\>echo !num! files
 files

C:\>endlocal

Re: How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 04:44
by orange_batch
drgt wrote:
orange_batch wrote:I'm talking about modifying the code you wrote. I don't know why your number of files varies, I use dir /a:-d /b /s in an important script and it works for me.


I don't know what is wrong I just used dir /a:-d /b /s >c:\log.txt, then copied the text file onto a spreadsheet, and counted 402 entries.
However, Folder properties in windows explorer reports 1098 files in some 400 directories!!!

Ok I'll be explicit, also I made a mistake in my previous post - I overlooked that you were entering that line straight to the command line.

In command prompt, change directory to the folder you want and paste this in:

Code: Select all

echo off&echo.&set num=&(for /f "delims=" %a in ('dir /a:-d /b /s') do echo %~nxa&set /a num+=1 >nul)&call echo Files: ^%num^%&echo on

The syntax required has weird behaviour when writing a single line like this to the command line.

By the way, instead of pasting your log into excel, in Notepad make sure Format: Word Wrap is unchecked, then make sure click View: Status Bar is checked. Scroll to the bottom of your log, click on the last line, and on the bottom right you'll see the line number Ln (beside Col for column).

Beware that Windows folder properties counts (maybe only specific types of) symbolic folders as files - command prompt more correctly does not. Take a look with dir /a:L /b /s.

dir will also count 3x the number of existing directories in its totals because it also counts . and .. for each directory, so divide by 3 (then add 1 if you want to include the target directory in the count).

File systems are quirky but manageable for the most part.

How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 05:19
by drgt
orange_batch wrote:

Code: Select all

echo off&echo.&set num=&(for /f "delims=" %a in ('dir /a:-d /b /s') do echo %~nxa&set /a num+=1 >nul)&call echo Files: ^%num^%&echo on


This code also yields 402 entries! :(

1094 were copied with xcopy (4 are corrupted)

Re: How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 05:29
by aGerman
drgt wrote:@aGerman

See following error:
G:\>dir /a-d /b /s|findstr /n
FINDSTR: Bad command line

You forgot the dot. It matches "any character". See my origin command line:

Code: Select all

dir /a-d /b /s|findstr /n .

Regards
aGerman

Re: How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 05:36
by drgt
aGerman wrote:You forgot the dot. It matches "any character". See my origin command line:

Code: Select all

dir /a-d /b /s|findstr /n .

Regards
aGerman


Yes, it worked now BUT it also reports 402 files :(

Re: How to get EVERY file in DIR including a file number

Posted: 04 Dec 2011 05:54
by !k
drgt wrote:C:\>echo !num! files
files
give me dir/s output (last 3 lines) pliz