DIR output including path on the same line as file name?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

DIR output including path on the same line as file name?

#1 Post by MicrosoftIsKillingMe » 03 Feb 2018 12:59

I want a compact DIR output that has path, name, size and date on the same line, like "Details" view of F3 search results in Windows.
DIR /S FOO.TXT | FINDSTR /C:":" achieves the compact wish, one line per filename, but of course size and path are missing because they're not on the same line in standard DIR output.

/B seems to be the only choice that shows the path and file on the same line, but it really is bare and doesn't have size and date.

Squashman
Expert
Posts: 4178
Joined: 23 Dec 2011 13:59

Re: DIR output including path on the same line as file name?

#2 Post by Squashman » 03 Feb 2018 13:52

The FOR command is your friend.

Code: Select all

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

Re: DIR output including path on the same line as file name?

#3 Post by MicrosoftIsKillingMe » 03 Feb 2018 15:00

Thank you. I would have never thunkit.

MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

Re: DIR output including path on the same line as file name?

#4 Post by MicrosoftIsKillingMe » 03 Feb 2018 16:05

test.bat
for /R c:\ %%I in ("ipconfig.txt*") do @echo %%~tI ... %%~zI ... %%~fI
Note the asterisk.
This gives (run from c:\, but I assume the /R parm makes that a moot point) the correct 2 files
01/20/2018 09:59 PM ... 8424 ... c:\ipconfig.txt
01/20/2018 09:59 PM ... 8424 ... c:\foo\ipconfig.txt

That's fine (although I fought and fought and failed trying to get anything to work with single ' instead of ").

Yet if I run it with the filespec asterisk removed
for /R c:\ %%I in ("ipconfig.txt") do @echo %%~tI ... %%~zI ... %%~fI
it seems to match everything in the world
... ... c:\"ipconfig.txt"
... ... c:\$Recycle.Bin\"ipconfig.txt"
... ... c:\$Recycle.Bin\S-1-5-18\"ipconfig.txt"
... ... c:\$Recycle.Bin\S-1-5-20\"ipconfig.txt"
... ... c:\foo\"ipconfig.txt"
... ... c:\foo\bar!_files\"ipconfig.txt"
etc. etc. etc. for hundreds of files before I Control-C

Now if I say to start in c:\batch,
for /R c:\batch\ %%I in ("ipconfig.txt") do @echo %%~tI ... %%~zI ... %%~fI
it doesn't return a line for every file in the dir; it returns just one line
... ... c:\BATCH\"ipconfig.txt"
but there is no ipconfig.txt in c:\batch. I assume that the one line returned "represents" the directory entry in the root ... that is, c:\batch\..
At any rate I'm deeply confused.

MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

Re: DIR output including path on the same line as file name?

#5 Post by MicrosoftIsKillingMe » 03 Feb 2018 16:28

Also, per FOR /?, wasn't I supposed to go
setlocal enableextensions
first, in order to use /R ? (It seems to make no difference if I include it)

FWIW I'm running on Win 7. VER is
Microsoft Windows [Version 6.1.7601]

Is enableextensions automatic for some DOS versions?

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

Re: DIR output including path on the same line as file name?

#6 Post by aGerman » 03 Feb 2018 16:54

MicrosoftIsKillingMe wrote:
03 Feb 2018 16:05
At any rate I'm deeply confused.
The FOR loop only checks for the existence of a file if globbing is applied (that is, if you use a pattern that contains wildcard characters).
MicrosoftIsKillingMe wrote:
03 Feb 2018 16:28
Is enableextensions automatic for some DOS versions?
DOS does not even support something like command extensions. Assuming you are talking about Windows the answer is yes. Extensions are enabled by default at least since XP (maybe earlier). But it's a registry setting which means that you could change this default.

Steffen

MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

Re: DIR output including path on the same line as file name?

#7 Post by MicrosoftIsKillingMe » 04 Feb 2018 01:20

In response to the last post,
---------------------------------------------------------------------------------------------------------------------------------------------
The basis of my question about command extensions, which I thought was based on enableextensions:
{FOR /?}
...
If Command Extensions are enabled, the following additional
forms of the FOR command are supported:
...
FOR /R
---------------------------------------------------------------------------------------------------------------------------------------------
and "globbing" (or even wildcards) is not explained below for FOR /R, so I suppose you have moved me closer to some understanding.
{FOR /?}
FOR /D %variable IN (set) DO command [command-parameters]

If set contains wildcards, then specifies to match against directory
names instead of file names.

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.
-------------------------------------------------------------------------------------------------------------------------------------------
Looks like I need to put in a few more hours of trial and error on this.

MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

Re: DIR output including path on the same line as file name?

#8 Post by MicrosoftIsKillingMe » 04 Feb 2018 01:49

ss64 clarified the asterisk when using /R
"Unlike some other variants of the FOR command you must include a wildcard (either * or ?) in the 'set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard e.g. if you are looping through multiple folders to find the exact filename myfile.txt you could instead specify myfile.t?t"

MicrosoftIsKillingMe
Posts: 42
Joined: 11 Dec 2017 09:08

Re: DIR output including path on the same line as file name?

#9 Post by MicrosoftIsKillingMe » 04 Feb 2018 05:09

This (namely the NOTES at the bottom of the .BAT below) shows the kind of information I was looking for, which I'll share in case it is helpful and useful to those less than superguru, some of whom don't have those details all committed to memory. There may be inaccuracies but it's what I deduced. The NOTES at the bottom include some important caveats - again, my own deductions, so take with a grain of salt.

Code: Select all

: alt_dir.bat
: This is an alternate homemade dir producing output like
: 01/20/2018 09:59 ... 8424 ... c:\ipconfig.txt
: 01/20/2018 09:59 ... 8424 ... c:\foo\ipconfig.txt

: This is handy for Win 7+ where productive 32 bit file management apps
: don't work, and you want a compact listing, but don't want to do an F3 search

: dir ipconfig.txt /a /s doesn't get path, size and dir on same line
: Directory of c:\ipconfig.txt
: 01/20/2018  09:59 PM            8,424 ipconfig.txt
: etc.
: 
: Here's the way to got the output shown atop:

for /R c:\ %%I in (ipconfig.txt*) do @echo %%~tI ... %%~zI ... %%~fI

: A fine way to understand .BATs is ss64, and in this case,
: https://ss64.com/nt/for_r.html

: Note, /R is to include subdirs
: Note, double %% needed in a batch file
: Note, with /R, a wildcard symbol is needed, even nonsensical as above
: Note, (ipconfig.txt*) could be ("ipconfig.txt*") but ('ipconfig.txt*') pukes
: Note, variables %I (and %%{TILDE}tI, etc.) are Case Sensitive.
: Note, {TILDE} is used above, not ~ because even in rem or : it expands!
:          (though proper delimiting could have managed that)
: Note, omitting c:\ would start in the current dir
By the way, that irritating wildcard requirement actually bit me, producing ipconfig.txt.lnk - grrrr.

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

Re: DIR output including path on the same line as file name?

#10 Post by aGerman » 04 Feb 2018 05:30

Code: Select all

for /f "delims=" %%i in ('2^>nul dir /a-d /b /s "C:\ipconfig.txt"') do ...
Nobody did ever say that you can't apply modifiers ~t and ~z if you process the output of the DIR command :wink:

Steffen

Post Reply