How to get EVERY file in DIR including a file number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

How to get EVERY file in DIR including a file number

#1 Post by drgt » 03 Dec 2011 15:17

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!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#2 Post by Ed Dyreen » 03 Dec 2011 15:25

'

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. . .

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

How to get EVERY file in DIR including a file number

#3 Post by drgt » 03 Dec 2011 15:32

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?

aGerman
Expert
Posts: 4713
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 03 Dec 2011 17:11

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#5 Post by orange_batch » 03 Dec 2011 21:41

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)

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

How to get EVERY file in DIR including a file number

#6 Post by drgt » 03 Dec 2011 23:16

@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?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#7 Post by orange_batch » 03 Dec 2011 23:55

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.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

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

#8 Post by drgt » 04 Dec 2011 00:06

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!!!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#9 Post by !k » 04 Dec 2011 01:38

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
Last edited by !k on 04 Dec 2011 14:00, edited 1 time in total.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

How to get EVERY file in DIR including a file number

#10 Post by drgt » 04 Dec 2011 04:27

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#11 Post by orange_batch » 04 Dec 2011 04:44

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.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

How to get EVERY file in DIR including a file number

#12 Post by drgt » 04 Dec 2011 05:19

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)

aGerman
Expert
Posts: 4713
Joined: 22 Jan 2010 18:01
Location: Germany

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

#13 Post by aGerman » 04 Dec 2011 05:29

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

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

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

#14 Post by drgt » 04 Dec 2011 05:36

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 :(

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#15 Post by !k » 04 Dec 2011 05:54

drgt wrote:C:\>echo !num! files
files
give me dir/s output (last 3 lines) pliz

Post Reply