Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Count Filenames Containing a User-Specified Extension  (Read 9424 times)

0 Members and 1 Guest are viewing this topic.

gregory

    Topic Starter


    Beginner

    Thanked: 1
    • Experience: Experienced
    • OS: Linux variant
    Count Filenames Containing a User-Specified Extension
    « on: December 18, 2012, 02:43:28 AM »
    Wanting to count filenames containing a user-specified extension.

    So...what I take is the usual way:
    Code: [Select]
    set /p ext=Enter an extension:

    for %%A in (*.%ext%) do (set /a FileCount+=1)
    If the user were to enter "png", a filename such as "file.pngs" would also get counted. I don't want that.
    Is there an absolute way to do this?

    Thanks.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Count Filenames Containing a User-Specified Extension
    « Reply #1 on: December 18, 2012, 03:26:11 AM »
    set /p ext=Enter an extension:

    for %%A in (*.%ext%) do if /i "%%~xA"==".%ext%" set /a FileCount+=1

    gregory

      Topic Starter


      Beginner

      Thanked: 1
      • Experience: Experienced
      • OS: Linux variant
      Re: Count Filenames Containing a User-Specified Extension
      « Reply #2 on: December 18, 2012, 04:01:47 AM »
      Well that should have been obvious... :-[

      Thanks foxidrive. I appreciate the help.

      Salmon Trout

      • Guest
      Re: Count Filenames Containing a User-Specified Extension
      « Reply #3 on: December 18, 2012, 04:20:30 AM »
      for %%A in (*.%ext%) do if /i "%%~xA"==".%ext%" set /a FileCount+=1

      Isn't the IF test redundant, since the *.%ext% filespec will ensure that only the files with the desired extension are returned by FOR?

      [edit] no it isn't redundant, just did a test - how about

      for %%A in ('dir /b') do if /i "%%~xA"==".%ext%" set /a FileCount+=1


      « Last Edit: December 18, 2012, 04:47:53 AM by Salmon Trout »

      gregory

        Topic Starter


        Beginner

        Thanked: 1
        • Experience: Experienced
        • OS: Linux variant
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #4 on: December 18, 2012, 04:47:06 AM »
        since the *.%ext% filespec will ensure that only the files with the desired extension are returned by FOR

        But it doesn't ensure that. Any given extension which contained within, the user-specified characters in order, would create a match. I wanted to match as a whole.

        I ended up using:

        Code: [Select]
        for %%A in (*.*) do (if /i "%%~xA"==".%ext%" (set /a FileCount+=1))

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #5 on: December 18, 2012, 05:33:18 AM »

        I ended up using:

        Code: [Select]
        for %%A in (*.*) do (if /i "%%~xA"==".%ext%" (set /a FileCount+=1))

        That is equivalent to my suggestion above.

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #6 on: December 18, 2012, 05:35:52 PM »
        That is equivalent to my suggestion above.

        You forgot the /f in the for command. 

        It makes sense to specify only the extension required though, as a folder could contain a few thousand files and only a handful of the target filetype.

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #7 on: December 19, 2012, 02:41:19 AM »
        You forgot the /f in the for command. 

        Well spotted!

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #8 on: December 19, 2012, 11:54:22 AM »
        XP finds files where the extension begins with the filespec (e.g. *.txt finds .txt and .txtx) whereas Windows 7 respects the filespec as given (*.txt only returns files with the .txt extension; to see those with further characters you'd need to modify the filespec e.g. *.txt*).





        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #9 on: December 19, 2012, 12:20:58 PM »
        whereas Windows 7 respects the filespec as given (*.txt only returns files with the .txt extension; to see those with further characters you'd need to modify the filespec e.g. *.txt*).

        Can you test that again, Salmon Trout?  I used the following in Windows 8 and it still matches the short filename.

        Code: [Select]
        @echo off
        for %%a in (*.txt) do echo %%a
        pause

        Screen output:
        file2.txt
        file3.txtx
        filename.txt

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #10 on: December 19, 2012, 12:39:28 PM »
        D:\test-delete>ver

        Microsoft Windows [Version 6.1.7601]

        D:\test-delete>dir
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        19/12/2012  19:42    <DIR>          .
        19/12/2012  19:42    <DIR>          ..
        19/12/2012  19:42                14 file1.txt
        19/12/2012  19:42                14 file1.txtx
        19/12/2012  19:42                14 file2.txt
        19/12/2012  19:42                14 file2.txtx
        19/12/2012  19:42                14 file3.txt
        19/12/2012  19:42                14 file3.txtx
        19/12/2012  19:42                14 file4.txt
        19/12/2012  19:42                14 file4.txtx
        19/12/2012  19:42                14 file5.txt
        19/12/2012  19:42                14 file5.txtx
        19/12/2012  19:42                14 file6.txt
        19/12/2012  19:42                14 file6.txtx
        19/12/2012  19:42                14 file7.txt
        19/12/2012  19:42                14 file7.txtx
        19/12/2012  19:42                14 file8.txt
        19/12/2012  19:42                14 file8.txtx
        19/12/2012  19:42                14 file9.txt
        19/12/2012  19:42                14 file9.txtx
                      18 File(s)            252 bytes
                       2 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>dir *.txt
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        19/12/2012  19:42                14 file1.txt
        19/12/2012  19:42                14 file2.txt
        19/12/2012  19:42                14 file3.txt
        19/12/2012  19:42                14 file4.txt
        19/12/2012  19:42                14 file5.txt
        19/12/2012  19:42                14 file6.txt
        19/12/2012  19:42                14 file7.txt
        19/12/2012  19:42                14 file8.txt
        19/12/2012  19:42                14 file9.txt
                       9 File(s)            126 bytes
                       0 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>dir *.txt*
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        19/12/2012  19:42                14 file1.txt
        19/12/2012  19:42                14 file1.txtx
        19/12/2012  19:42                14 file2.txt
        19/12/2012  19:42                14 file2.txtx
        19/12/2012  19:42                14 file3.txt
        19/12/2012  19:42                14 file3.txtx
        19/12/2012  19:42                14 file4.txt
        19/12/2012  19:42                14 file4.txtx
        19/12/2012  19:42                14 file5.txt
        19/12/2012  19:42                14 file5.txtx
        19/12/2012  19:42                14 file6.txt
        19/12/2012  19:42                14 file6.txtx
        19/12/2012  19:42                14 file7.txt
        19/12/2012  19:42                14 file7.txtx
        19/12/2012  19:42                14 file8.txt
        19/12/2012  19:42                14 file8.txtx
        19/12/2012  19:42                14 file9.txt
        19/12/2012  19:42                14 file9.txtx
                      18 File(s)            252 bytes
                       0 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>for %A in (*.txt) do @echo %A
        file1.txt
        file2.txt
        file3.txt
        file4.txt
        file5.txt
        file6.txt
        file7.txt
        file8.txt
        file9.txt

        D:\test-delete>for %A in (*.txt*) do @echo %A
        file1.txt
        file1.txtx
        file2.txt
        file2.txtx
        file3.txt
        file3.txtx
        file4.txt
        file4.txtx
        file5.txt
        file5.txtx
        file6.txt
        file6.txtx
        file7.txt
        file7.txtx
        file8.txt
        file8.txtx
        file9.txt
        file9.txtx

        (same behaviour in batch using DIR and FOR)


        and...

        C:\test-delete>ver

        Microsoft Windows XP [Version 5.1.2600]

        C:\test-delete>dir *.txt
         Volume in drive C has no label.
         Volume Serial Number is E4FF-7569

         Directory of C:\test-delete

        19/12/2012  19:46                14 file1.txt
        19/12/2012  19:47                14 file1.txtx
        19/12/2012  19:46                14 file2.txt
        19/12/2012  19:47                14 file2.txtx
        19/12/2012  19:46                14 file3.txt
        19/12/2012  19:47                14 file3.txtx
        19/12/2012  19:46                14 file4.txt
        19/12/2012  19:47                14 file4.txtx
        19/12/2012  19:46                14 file5.txt
        19/12/2012  19:47                14 file5.txtx
        19/12/2012  19:46                14 file6.txt
        19/12/2012  19:47                14 file6.txtx
        19/12/2012  19:46                14 file7.txt
        19/12/2012  19:47                14 file7.txtx
        19/12/2012  19:46                14 file8.txt
        19/12/2012  19:47                14 file8.txtx
        19/12/2012  19:46                14 file9.txt
        19/12/2012  19:47                14 file9.txtx
                      18 File(s)            252 bytes
                       0 Dir(s)  59,171,684,352 bytes free

        C:\test-delete>for %A in (*.txt) do @echo %A
        file1.txt
        file1.txtx
        file2.txt
        file2.txtx
        file3.txt
        file3.txtx
        file4.txt
        file4.txtx
        file5.txt
        file5.txtx
        file6.txt
        file6.txtx
        file7.txt
        file7.txtx
        file8.txt
        file8.txtx
        file9.txt
        file9.txtx


        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #11 on: December 19, 2012, 12:46:57 PM »
        Do you have short filenames disabled on the box?

        All I can say is that my Windows 8 displays them all - but if you have short filenames disabled then it will not match them.

        d:\ABC>dir *.txt
         Volume in drive D is My Passport
         Volume Serial Number is 74B7-6DCC

         Directory of d:\ABC

        20/12/2012  03:25 AM                41 file2.txt
        20/12/2012  03:25 AM                41 file3.txtx
        20/12/2012  03:25 AM                30 filename.txt

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #12 on: December 19, 2012, 01:03:31 PM »
        Both tests were done on NTFS volumes; Windows 7 64 bit Professional and 32 bit XP SP3; on both machines the reg key:

        HKEY_LOCAL_ MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\NtfsDisable8dot3NameCreation

        is set to 0 (short filenames not disabled)

        I wonder if it is a 64 vs 32 bit thing?



        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #13 on: December 19, 2012, 01:13:43 PM »
        Can you do a DIR /X with the files?

        Here is my Windows 7 box and it also behaves like XP.  The difference is that this is 32 bit, as is my Windows 8.


        Microsoft Windows [Version 6.1.7601]
        Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

        d:\ABC>dir *.txt
         Volume in drive D is D
         Volume Serial Number is F87A-310C

         Directory of d:\ABC

        Thu 20/12/2012  03:25                41 file2.txt
        Thu 20/12/2012  03:25                41 file3.txtx
        Thu 20/12/2012  03:25                30 filename.txt
                       3 File(s)            112 bytes
                       0 Dir(s)  85,933,006,848 bytes free


        d:\ABC>for %a in (*.txt) do @echo %a
        file2.txt
        file3.txtx
        filename.txt


        d:\ABC>dir /x
         Volume in drive D is D
         Volume Serial Number is F87A-310C

         Directory of d:\ABC

        Thu 20/12/2012  07:24    <DIR>                       .
        Thu 20/12/2012  07:24    <DIR>                       ..
        Thu 20/12/2012  03:25                41              file2.txt
        Thu 20/12/2012  03:25                41 FILE3~1.TXT  file3.txtx
        Thu 20/12/2012  03:25                30              filename.txt
                       3 File(s)            112 bytes
                       2 Dir(s)  85,933,006,848 bytes free


        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #14 on: December 19, 2012, 01:21:31 PM »
        W7 64 bit


        D:\test-delete>dir /x
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        19/12/2012  20:00    <DIR>                       .
        19/12/2012  20:00    <DIR>                       ..
        19/12/2012  19:42                14              file1.txt
        19/12/2012  19:42                14 FILE1~1.TX~  file1.txtx
        19/12/2012  19:42                14              file2.txt
        19/12/2012  19:42                14 FILE2~1.TX~  file2.txtx
        19/12/2012  19:42                14              file3.txt
        19/12/2012  19:42                14 FILE3~1.TX~  file3.txtx
        19/12/2012  19:42                14              file4.txt
        19/12/2012  19:42                14 FILE4~1.TX~  file4.txtx
        19/12/2012  19:42                14              file5.txt
        19/12/2012  19:42                14 FILE5~1.TX~  file5.txtx
        19/12/2012  19:42                14              file6.txt
        19/12/2012  19:42                14 FILE6~1.TX~  file6.txtx
        19/12/2012  19:42                14              file7.txt
        19/12/2012  19:42                14 FILE7~1.TX~  file7.txtx
        19/12/2012  19:42                14              file8.txt
        19/12/2012  19:42                14 FILE8~1.TX~  file8.txtx
        19/12/2012  19:42                14              file9.txt
        19/12/2012  19:42                14 FILE9~1.TX~  file9.txtx
                      19 File(s)            295 bytes
                       2 Dir(s)  275,366,141,952 bytes free



        XP 32 bit

        C:\test-delete>dir /X
         Volume in drive C has no label.
         Volume Serial Number is E4FF-7569

         Directory of C:\test-delete

        19/12/2012  19:47    <DIR>                       .
        19/12/2012  19:47    <DIR>                       ..
        19/12/2012  19:46                14              file1.txt
        19/12/2012  19:47                14 FILE1~1.TXT  file1.txtx
        19/12/2012  19:46                14              file2.txt
        19/12/2012  19:47                14 FILE2~1.TXT  file2.txtx
        19/12/2012  19:46                14              file3.txt
        19/12/2012  19:47                14 FILE3~1.TXT  file3.txtx
        19/12/2012  19:46                14              file4.txt
        19/12/2012  19:47                14 FILE4~1.TXT  file4.txtx
        19/12/2012  19:46                14              file5.txt
        19/12/2012  19:47                14 FILE5~1.TXT  file5.txtx
        19/12/2012  19:46                14              file6.txt
        19/12/2012  19:47                14 FILE6~1.TXT  file6.txtx
        19/12/2012  19:46                14              file7.txt
        19/12/2012  19:47                14 FILE7~1.TXT  file7.txtx
        19/12/2012  19:46                14              file8.txt
        19/12/2012  19:47                14 FILE8~1.TXT  file8.txtx
        19/12/2012  19:46                14              file9.txt
        19/12/2012  19:47                14 FILE9~1.TXT  file9.txtx
                      18 File(s)            252 bytes
                       2 Dir(s)  59,170,963,456 bytes free

         

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #15 on: December 19, 2012, 01:23:29 PM »
        That's the reason - the shortname generation doesn't follow the rules on XP and earlier (and apparantly Win 7 and 8 32 bit).

        I don't have a 64 bit box to test on...

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #16 on: December 19, 2012, 01:44:14 PM »
        I'm surprised; I'd have thought it was a filesystem thing, rather than an OS bitness issue, but your 32 bit W7 & W8 results suggest not.


        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #17 on: December 19, 2012, 01:55:27 PM »
        On W7 64 I tried creating the files on a FAT formatted SD card that happened to be in my card reader; same behaviour (*.txt finds only fileN.txt)

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #18 on: December 19, 2012, 02:11:46 PM »
        I posted a request in alt.msdos.batch.nt with this batch file, to gauge if it is universal.  I'll post results later.


        Code: [Select]
        @echo off
        >file1.txt echo abc
        >file2.txt echo abc
        >file3.txtx echo abc
        >file4.txtx echo abc

        dir /x *.txt*

        for %%a in (*.txt) do echo %%a

        pause

        This is my Win 8 32 bit machine - same as earlier.


        Directory of d:\ABC

        20/12/2012  08:08 AM                 5              file1.txt
        20/12/2012  08:08 AM                 5              file2.txt
        20/12/2012  08:08 AM                 5 FILE3~1.TXT  file3.txtx
        20/12/2012  08:08 AM                 5 FILE4~1.TXT  file4.txtx

        file1.txt
        file2.txt
        file3.txtx
        file4.txtx
        Press any key to continue . . .

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #19 on: December 19, 2012, 03:33:49 PM »
        foxidrive, do you sleep during the day?

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #20 on: December 19, 2012, 03:52:52 PM »
        foxidrive, do you sleep during the day?
        Drat!  My Vampire secret is out! ;)
        (I have insomnia and sleep when I can.)

        Two replies are in so far - XP 64 bit and Win7 Pro 64 bit - and both show 'standard' 32 bit behaviour.


        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #21 on: December 19, 2012, 05:54:28 PM »
        On my Windows 7 x64 as well as Windows 8 x64 systems, Ultimate and Professional respectively (doubt that matters but it might), *.txt matches a .txtx file, and dir /x shows the short filename as having a .txt extension.
        I was trying to dereference Null Pointers before it was cool.

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #22 on: December 20, 2012, 02:46:39 AM »
        So my x64 system is the odd one?

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #23 on: December 20, 2012, 05:11:21 AM »
        Another Win7HP 64 bit machine also shows the 'usual' behaviour.

        It would seem just that the behaviour can't be predicted for short filename generation.

        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #24 on: December 20, 2012, 11:02:30 AM »
        Dunno if this sheds any light...

        D:\test-delete>for /l %A in (1,1,3) do @echo hello world > file%A.lex

        D:\test-delete>for /l %A in (1,1,3) do @echo hello world > file%A.lexi

        D:\test-delete>for /l %A in (1,1,3) do @echo hello world > file%A.lexicon

        D:\test-delete>dir
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        20/12/2012  18:12    <DIR>          .
        20/12/2012  18:12    <DIR>          ..
        20/12/2012  18:12                14 file1.lex
        20/12/2012  18:12                14 file1.lexi
        20/12/2012  18:12                14 file1.lexicon
        20/12/2012  18:12                14 file2.lex
        20/12/2012  18:12                14 file2.lexi
        20/12/2012  18:12                14 file2.lexicon
        20/12/2012  18:12                14 file3.lex
        20/12/2012  18:12                14 file3.lexi
        20/12/2012  18:12                14 file3.lexicon
        19/12/2012  20:01                43 test.bat
                      10 File(s)            169 bytes
                       2 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>dir /x
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        20/12/2012  18:12    <DIR>                       .
        20/12/2012  18:12    <DIR>                       ..
        20/12/2012  18:12                14              file1.lex
        20/12/2012  18:12                14 FILE1~1.LE~  file1.lexi
        20/12/2012  18:12                14 FILE1~2.LE~  file1.lexicon
        20/12/2012  18:12                14              file2.lex
        20/12/2012  18:12                14 FILE2~1.LE~  file2.lexi
        20/12/2012  18:12                14 FILE2~2.LE~  file2.lexicon
        20/12/2012  18:12                14              file3.lex
        20/12/2012  18:12                14 FILE3~1.LE~  file3.lexi
        20/12/2012  18:12                14 FILE3~2.LE~  file3.lexicon
        19/12/2012  20:01                43              test.bat
                      10 File(s)            169 bytes
                       2 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>dir *.lex
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        20/12/2012  18:12                14 file1.lex
        20/12/2012  18:12                14 file2.lex
        20/12/2012  18:12                14 file3.lex
                       3 File(s)             42 bytes
                       0 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>dir *.lexi
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        20/12/2012  18:12                14 file1.lexi
        20/12/2012  18:12                14 file2.lexi
        20/12/2012  18:12                14 file3.lexi
                       3 File(s)             42 bytes
                       0 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>dir *.lex*
         Volume in drive D is SAT-D
         Volume Serial Number is E8B2-C5D7

         Directory of D:\test-delete

        20/12/2012  18:12                14 file1.lex
        20/12/2012  18:12                14 file1.lexi
        20/12/2012  18:12                14 file1.lexicon
        20/12/2012  18:12                14 file2.lex
        20/12/2012  18:12                14 file2.lexi
        20/12/2012  18:12                14 file2.lexicon
        20/12/2012  18:12                14 file3.lex
        20/12/2012  18:12                14 file3.lexi
        20/12/2012  18:12                14 file3.lexicon
                       9 File(s)            126 bytes
                       0 Dir(s)  275,366,141,952 bytes free

        D:\test-delete>

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #25 on: December 20, 2012, 02:23:34 PM »
        The short name generation is different - but short names can even be random hex numbers in some cases, so it's not an issue.


        Salmon Trout

        • Guest
        Re: Count Filenames Containing a User-Specified Extension
        « Reply #26 on: December 20, 2012, 02:42:00 PM »
        short names can even be random hex numbers

        I noticed that when I went to 1000 of each extension.