Finding .txt files of certain name length

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dewars
Posts: 11
Joined: 12 Apr 2012 07:44

Finding .txt files of certain name length

#1 Post by Dewars » 13 Apr 2012 23:37

Hi there,

I'm trying to find all .txt files on my C: Drive that have exactly 7 characters, then display them in a wide format.
The code I'm using at the moment is

Code: Select all

DIR C:\ /W /S | findstr /R "^.......\.txt"


But it's returning other files e.g.

Code: Select all

eulaENU.txt          InstallManager.cfg   Language.Dat
License.txt                     License_Italian.txt
schemes.txt      simplified.clr
Medical.txt                      Philosophy.txt
LICENSE.txt                   PATCH.ERR
AUTHORS.txt                   COPYING_GPL.txt
License.txt                       locdata.ini
license.txt                 PreEmptive.Attributes.dll
version.txt
nss_log.txt               omniture_log.txt          Output_NSP_Detector.log
maplist.txt              [maps]                   matchmodes.txt
actbusy.txt
credits.txt
points3.txt        points4.txt        polys.txt


Any help would be much appreciated.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Finding .txt files of certain name length

#2 Post by Aacini » 14 Apr 2012 00:09

I think this command should do that:

Code: Select all

for /R C:\ %%f in (???????.txt) do (
   echo %%~Nf
)

However, if previous command fails, then this code should correctly works:

Code: Select all

setlocal EnableDelayedExpansion
for /R C:\ %%f in (*.txt) do (
   set "name=%%~Nf"
   if "!name:~7,4!" == ".txt" echo %%~Nf
)

Dewars
Posts: 11
Joined: 12 Apr 2012 07:44

Re: Finding .txt files of certain name length

#3 Post by Dewars » 14 Apr 2012 00:26

So there's no way to just slightly modify my code? It just seems so close to working.
I was trying to do this without using any loops, but if there isn't any other way, then your way will have to do.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding .txt files of certain name length

#4 Post by foxidrive » 14 Apr 2012 04:17

Dewars wrote:Hi there,

I'm trying to find all .txt files on my C: Drive that have exactly 7 characters, then display them in a wide format.
The code I'm using at the moment is

Code: Select all

DIR C:\ /W /S | findstr /R "^.......\.txt"



Can you describe the purpose?

How do you want to display the folder names in such a listing?

Dewars
Posts: 11
Joined: 12 Apr 2012 07:44

Re: Finding .txt files of certain name length

#5 Post by Dewars » 14 Apr 2012 04:30

Hi foxidrive

Yes, I just want to display them in a wide format, and then append them on to the end of a text file I created earlier.
This is exam revision, so I'm guessing I'll have to know how to do this. I'm very close, I just don't see why those other files are showing up on the right.

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding .txt files of certain name length

#6 Post by foxidrive » 14 Apr 2012 06:12

Dewars wrote: I just don't see why those other files are showing up on the right.


Wide format creates strings of filenames. Your regexp is matching the leading part of the string.

This should only give you the ones in the first column, but you will lose any ones that don't start in column 1.

DIR C:\ /W /S | findstr /R "^.......\.txt$"

Dewars
Posts: 11
Joined: 12 Apr 2012 07:44

Re: Finding .txt files of certain name length

#7 Post by Dewars » 14 Apr 2012 06:28

The code you supplied works to some degree, but has left out quite a few of the files it displayed previously.

Code: Select all

version.txt
actbusy.txt
credits.txt
READ ME.txt
jReadMe.txt


Those are the only ones that showed up.

Thank you for taking the time to help me, I've spent ages trying to do this, and this is a step in the right direction.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding .txt files of certain name length

#8 Post by foxidrive » 14 Apr 2012 08:10

Can you use SED?

Like the previous one, any matches that occur after column 12 will be deleted, but it will give you those in column 1.

DIR /W /S | SED -n "s/\(^.......\.txt\)/\1/ip"


This next one is far more robust and should report all the files, but it does not provide a wide format listing.

Code: Select all

DIR \*.txt /a-d /s | SED -n "s/^....................................\(.......\.txt\).*/\1/ip"
Last edited by foxidrive on 14 Apr 2012 10:07, edited 1 time in total.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Finding .txt files of certain name length

#9 Post by Fawers » 14 Apr 2012 09:30

Aacini wrote:

Code: Select all

setlocal EnableDelayedExpansion
for /R C:\ %%f in (*.txt) do (
   set "name=%%~Nf"
   if "!name:~7,4!" == ".txt" echo %%~Nf
)

Mate, careful. This code won't find .txt at [position] !name:~7,4! unless you set name to %%~NXf.

Code: Select all

set "name=%%~NXf"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Finding .txt files of certain name length

#10 Post by foxidrive » 14 Apr 2012 10:22

This works here in an 80 column console. It cheats a bit because the lines aren't individual lines - they just wrap in this manner to provide a wide listing.

If any files contain a ^ or % or & then it will probably break.

Code: Select all

@echo off
for /f "delims=" %%a in ('DIR \*.txt /a-d /s ^| SED -n "s/^....................................\(.......\.txt\).*/\1/ip"') do call set v=%%v%%     %%a
echo %v:~5%
pause


Code: Select all

Colores.txt     get url.txt     geturls.txt     hex2dec.txt     Juldate.txt
mathtoy.txt     now.exe.txt     Numbers.txt     numeric.txt     setdate.txt
strings.txt     whereis.txt     WSHcalc.txt

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding .txt files of certain name length

#11 Post by dbenham » 14 Apr 2012 15:11

Aacini wrote:I think this command should do that:

Code: Select all

for /R C:\ %%f in (???????.txt) do (
   echo %%~Nf
)

That code lists all base file names width 7 or smaller

EDIT - I missed the /S option in the original request, so I need to modify the code below

Here a pure batch solution using nothing but native commands.

Since DIR cannot limit its output to files with base name of exactly 7 characters, you cannot use DIR to produce the wide format. You will need to use some type of loop to create the wide format.

You can use any number of techniques to check each file (one per line) to see if it matches your requirements.

I would use

Code: Select all

dir /b *.txt | findstr "^...........$"

Then you need to use a loop to get the results in your wide format.

You need to decide how wide the line is, and how many spaces between each file. I'm going to assume a width of 80 with 5 spaces between columns, so that allows 5 columns.

It would be nice to avoid using delayed expansion so that you don't have to worry about FOR variable expansion corrupting names containing exclamation points. I'm going to use some math tricks so that neither delayed expansion nor a CALL is needed to test the number of columns printed so far. If there is no division by 0, then the "success" code is executed, else the "error" code is excecuted.

The code uses the <NUL SET /P trick to print text without a new line. The trick sets ERRORLEVEL to an error, so I use VER to reset the ERRORLEVEL to 0 so that it doesn't fall into the "error" section.

Code: Select all

@echo off
set n=0
for /f "eol=: delims=" %%f in ('dir /b *.txt ^| findstr "^...........$"') do (
  2>nul set /a "n=n+1,1/(n%%5)" && (<nul set /p "=%%f     " & ver >nul) || (echo %%f)
)
2>nul set /a "1/(n%%5)" && (echo()



Dave Benham

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Finding .txt files of certain name length

#12 Post by Liviu » 14 Apr 2012 18:04

dbenham wrote:
Aacini wrote:I think this command should do that:

Code: Select all

for /R C:\ %%f in (???????.txt) do (
   echo %%~Nf
)
That code lists all base file names width 7 or smaller

On top of that, "dir" and "for" apply the wildcard to both long and short filenames (when the latter are enabled, of course). And even though most short filenames are 8.3, some are not.

Code: Select all

C:\tmp\8.3>dir /x ???????.txt

04/14/2012  06:40 PM                 0 ABCDE~1.TXT  a b c d e.txt
04/14/2012  06:40 PM                 0 ABCDE~2.TXT  a.b.c.d.e.txt
04/14/2012  06:40 PM                 0 ABCDE~3.TXT  ...abcde.txt
               3 File(s)              0 bytes

Code: Select all

C:\tmp\8.3>for %f in (???????.txt) do @echo %f
a b c d e.txt
a.b.c.d.e.txt
...abcde.txt

Caveat being that "???????.txt" can sometimes match files with names longer than 7 characters.

Liviu

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding .txt files of certain name length

#13 Post by dbenham » 14 Apr 2012 20:33

Liviu wrote:Caveat being that "???????.txt" can sometimes match files with names longer than 7 characters.
Good point! I had run across such behavior before, but forgot all about it.

----------

I worked out a native batch solution that works with the /S option.

The following simple solutions "works" , but it is all but worthless because it does not indicate the path of each file.

Code: Select all

@echo off
set n=0
for /f "delims=" %%F in ('dir /b /s *.txt ^| findstr "[\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\]$"') do (
  2>nul set /a "n=n+1,1/(n%%5)" && (<nul set /p "=%%~nxF     " & ver >nul) || (echo %%~nxF)
)
2>nul set /a "1/(n%%5)" && (echo()

I believe delayed expansion is required to include paths and get decent performance. The delayed expansion must be toggled to prevent corruption of names containing !.

Code: Select all

@echo off
setlocal disableDelayedExpansion
set old=
for /f "delims=" %%F in ('dir /b /s *.txt ^| findstr "[\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\][^\\]$"') do (
  set "new=%%~dpF"
  set "file=%%~nxF"
  setlocal enableDelayedExpansion
   if "!new!" neq "!old!" (
     if !n! neq 0 echo(
      echo(
      echo(
    echo  Directory of !new!
    echo(
    set "old=!new!"
    set n=0
  )
  set /a "n=(n+1)%%5"
  if !n! neq 0 (<nul set /p "=!file!     ") else echo !file!
  for /f "tokens=1* delims= " %%A in ("!n! !old!") do (
    endlocal
    set n=%%A
    set old=%%B
  )
)
if %n% neq 0 echo(

The output format is basically the same as DIR /S /W, except the file counts and total file size are not included. The file counts and total size could be added easily enough, except that batch math only supports numbers to ~2GB. A scan of an entire volume is likely to exceed the limit. At that point a different language would be needed, perhaps JScript or VBScript. (or else a very slow large number batch solution could be used, or a non-native utility with big int support)

Dave Benham

Post Reply