DosTips.com

A Forum all about DOS Batch
It is currently 29 May 2016 17:02

All times are UTC-06:00




Post new topic  Reply to topic  [ 13 posts ] 
Author Message
PostPosted: 13 Apr 2012 23:37 
Offline

Joined: 12 Apr 2012 07:44
Posts: 11
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:
DIR C:\ /W /S | findstr /R "^.......\.txt"


But it's returning other files e.g.
Code:
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.


Top
   
PostPosted: 14 Apr 2012 00:09 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1137
Location: México City, México
I think this command should do that:
Code:
for /R C:\ %%f in (???????.txt) do (
   echo %%~Nf
)

However, if previous command fails, then this code should correctly works:
Code:
setlocal EnableDelayedExpansion
for /R C:\ %%f in (*.txt) do (
   set "name=%%~Nf"
   if "!name:~7,4!" == ".txt" echo %%~Nf
)


Top
   
PostPosted: 14 Apr 2012 00:26 
Offline

Joined: 12 Apr 2012 07:44
Posts: 11
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.


Top
   
PostPosted: 14 Apr 2012 04:17 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5598
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:
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?


Top
   
PostPosted: 14 Apr 2012 04:30 
Offline

Joined: 12 Apr 2012 07:44
Posts: 11
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


Top
   
PostPosted: 14 Apr 2012 06:12 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5598
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$"


Top
   
PostPosted: 14 Apr 2012 06:28 
Offline

Joined: 12 Apr 2012 07:44
Posts: 11
The code you supplied works to some degree, but has left out quite a few of the files it displayed previously.

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


Top
   
PostPosted: 14 Apr 2012 08:10 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5598
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:
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.

Top
   
PostPosted: 14 Apr 2012 09:30 
Offline

Joined: 08 Apr 2012 17:11
Posts: 187
Aacini wrote:
Code:
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:
set "name=%%~NXf"


Top
   
PostPosted: 14 Apr 2012 10:22 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5598
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:
@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:
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


Top
   
PostPosted: 14 Apr 2012 15:11 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1638
Location: United States (east coast)
Aacini wrote:
I think this command should do that:
Code:
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:
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:
@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


Top
   
PostPosted: 14 Apr 2012 18:04 
Offline
Expert

Joined: 13 Jan 2012 21:24
Posts: 469
dbenham wrote:
Aacini wrote:
I think this command should do that:
Code:
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:
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:
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


Top
   
PostPosted: 14 Apr 2012 20:33 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1638
Location: United States (east coast)
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:
@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:
@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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 13 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot], sambul35 and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Limited