Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is it possible to obtain a directory list of files whose first character is non-numeric or Alpha?

    As an example: Dir 6*.* will provide a list of those files which only begin with a 6.

    I'm not sure how to generate a list only for Alpha.

    Thanks,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Perhaps BATcher will know if this can be done with the DIR command - I don't.

    You could create a list using VBA in any Office application, by using the Dir function to loop through all files in a folder, and to output a filename if it begins with a letter. For example

    Code:
    Dim strFile As String
    Const strpath = "C:\Test\"
    strFile = Dir(strpath & "*.*")
    Do While Not strFile = ""
      If strFile Like "[A-Za-z]*" Then
    	Debug.Print strFile
      End If
      strFile = Dir
    Loop

  3. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,420
    Thanks
    33
    Thanked 195 Times in 175 Posts
    [quote name='jstevens' post='790401' date='23-Aug-2009 23:49']Is it possible to obtain a directory list of files whose first character is non-numeric or Alpha?

    As an example: Dir 6*.* will provide a list of those files which only begin with a 6.

    I'm not sure how to generate a list only for Alpha.[/quote]

    It's do-able via a BATch file, but a bit messy. I'll work on it...
    Could you define what you want to happen for files whose names begin with a special character (like $)?
    In other words, do you just want to exclude files whose first character is numeric?
    BATcher

    Time prevents everything happening all at once...

  4. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,420
    Thanks
    33
    Thanked 195 Times in 175 Posts
    [quote name='BATcher' post='790423' date='24-Aug-2009 06:03']It's do-able via a BATch file, but a bit messy. I'll work on it...
    Could you define what you want to happen for files whose names begin with a special character (like $)?
    In other words, do you just want to exclude files whose first character is numeric?[/quote]Here you are: it's a bit crude, but works:

    Code:
    @echo off
    :: produce a list of all the filenames which
    ::   DO NOT start with a numeric character
    setlocal
    :: sort files in name order, and omit all subdirectories
    for /f "tokens=*" %%a in ('dir /on /a-d') do call :checknum "%%a"
    endlocal
    goto :eof
    
    :checknum
    :: remove the double-quotes from each end of the string
    set line=%~1
    :: ignore any empty strings (shouldn't be any!)
    if not defined line goto :eof
    :: check on the first character of the filename
    ::   which in the UK is at position 36, column 37, of the line
    if "%line:~36,1%" GEQ "0" if "%line:~36,1%" LEQ "9" goto :eof
    :: the filename first char must be non-numeric, so write it to the console
    echo %line%
    goto :eof
    Note that the totals at the end include the numeric filenames which were omitted from the listing...
    It may not work in your locale because it assumes that in the directory list the first character of the filename appears at position 36 (starting from 0) or column 37 (starting from 1). This value is correct for the UK but may not be for the US! Change if necessary.
    BATcher

    Time prevents everything happening all at once...

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks I'll have to give it a try when I have an opportunity.

    Regards,
    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •