Jump to content

Recommended Posts

Posted

I have a need to get the sub folders in a path that have an eight digit numbered name, EG:

C:\test\sam\mike\00000001

C:\test\sam\mike\00000002

C:\test\sam\mike\00000014

but not

C:\test\sam\mike\75123MM

_FileListToArray can be set to reurn only folders.

How do I set a filter for _FileListToArray to return only eight digit numbers?

If StringRegexp is the answer, what would be the parameters?

 

Thanks

 

 

 

 

 

Posted (edited)
3 hours ago, AutoitMike said:

If StringRegexp is the answer, what would be the parameters?

The mention of RegEx in the help on  _FileListToArray and _FileListToArrayRec is misleading. The filter you enter is internally converted into a RegEx to speed up the comparison code when searching - but the filter must be of the form indicated in the Help file.

Use :

#include <Array.au3>
#include <File.au3>
Local $aFolderList = _FileListToArrayRec("C:\test\sam\mike", "*" , $FLTAR_FOLDERS, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH)
Local $aFolderListNew[0]
For $i = 1 To $aFolderList[0]
    If StringRegExp($aFolderList[$i], '^(\d{8})$', 0) Then _ArrayAdd($aFolderListNew, $aFolderList[$i])
Next
_ArrayDisplay($aFolderListNew) ; *** just for display

If you need the full path to the folder in the result array ($aFolderListNew) , please use :

#include <Array.au3>
#include <File.au3>
Local $aFolderList = _FileListToArrayRec("C:\test\sam\mike", "*" , $FLTAR_FOLDERS, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH )
Local $aFolderListNew[0]
For $i = 1 To $aFolderList[0]
    If StringRegExp(StringRegExpReplace($aFolderList[$i], "^.*\\", ""), '^(\d{8})$', 0) Then _ArrayAdd($aFolderListNew, $aFolderList[$i])
Next
_ArrayDisplay($aFolderListNew) ; *** just for display

 

Edited by Musashi
Posted (edited)

Another solution would be to use StringIsDigit (see help file)

#include <Array.au3>
#include <File.au3>

Local $aFolderList = _FileListToArrayRec(@ScriptDir, "*" , $FLTAR_FOLDERS, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH )
Local $aFolderListNew[0], $sDrive, $sDir, $sFileName, $sExtension

For $i = 1 To $aFolderList[0]
  _PathSplit($aFolderList[$i], $sDrive, $sDir, $sFileName, $sExtension)
  If StringIsDigit($sFileName) Then _ArrayAdd($aFolderListNew, $aFolderList[$i])
Next
_ArrayDisplay($aFolderListNew) ; *** just for display

ps. in the case of full path, here another SRE :

If StringRegExp($aFolderList[$i], "(?:.*\\)(\d+)$") Then ...

pps.  note that in both cases I did not check for the exact 8 digits as I felt it was restricting the search for nothing...

Edited by Nine
added code / another SRE
Posted (edited)
1 hour ago, Nine said:

Another solution would be to use StringIsDigit

_PathSplit($aFolderList[$i], $sDrive, $sDir, $sFileName, $sExtension)
  If StringIsDigit($sFileName) Then _ArrayAdd($aFolderListNew, $aFolderList[$i])

 

_PathSplit with StringIsDigit is a good realisation as well (and likely easier to read, than a RegEx version, the OP was asking for). Currently, however, folders with more or less than 8 digits are also found this way, but that should be something @AutoitMike can solve himself ;).

EDIT : Oh, I see you've already added that :lol::

1 hour ago, Nine said:

pps. note that in both cases I did not check for the exact 8 digits

Edited by Musashi

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...