Jump to content

StringSplit / _StringExplode with sorting out #s


Recommended Posts

I've got several question here all kinda related.

Has StringSplit been replaced with _StringExplode?  How can I split a string between characters and digits? Or, is there a better way to do this?

I've got a directory that had a list of directories, that I need to read and store only valid names.  I have been using a search by FileFindFirstFile with a switch to sort out the ones that I know I don't want, and storing the good ones in an array.

I have a few that I'm having problems sorting out.  These all have words with numbers at the end and the words can be as short as 3 characters or long as 15 or 20 characters.  The numbers can be 1 digit or 10 to 15 digits long.

I've been trying to use a StringSplit to seperate the two types and haven't found a way to do this.  This is what I got.

#include <Array.au3>
local $search, $dfiles
local $set[1]
$n=0

$search = FileFindFirstFile("C:\class\*.*") 
While 1
            $dfiles = FileFindNextFile($search)
            If @error Then ExitLoop
            Switch $dfiles
                Case "Bob";skip
                Case "Janet";skip               
                Case "John";skip
            Case Else
                $n=StringSplit(String($dfiles),"")
                if IsNumber($n[2])=0 then
                    _ArrayAdd($set,$dfiles)
                 endif
            EndSwitch
        WEnd
        FileClose($search); Close the search handle
      _ArrayDisplay($set)

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

Has StringSplit been replaced with _StringExplode?  How can I split a string between characters and digits? Or, is there a better way to do this?

No, you can use the flag $STR_ENTIRESPLIT (with StringSplit) to split with the entire delimiter. If you mean regular expressions by "characters and digits" then it does not support it.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Like This?

; Johnmcloud - 2014
$String = 'abcyz12128257927782'
For $i = 0 To 9
    $iPos = StringInStr($String, $i)
    If $iPos <> 0 Then ExitLoop
Next
$First = StringLeft($String, $iPos - 1)
$Second = StringMid($String, $iPos + 1)
ConsoleWrite("ONLY LETTER: " & $First & @LF)
ConsoleWrite("ONLY NUMBER: " & $Second & @LF)

Or probably i have missunderstood :sweating:

Edited by johnmcloud
Link to comment
Share on other sites

This is part of a larger program, so all I need for now, is an array of directory names that have names+numbers.  Here are a few examples.

tony3869

ace15877644320

jennifer097375529774

jenniffer8432

barnhill2299354

Later I want to exclude these from the array, but that is for a different program.

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

If the requirements for sorting out are precise and well defined this can probably be easily (?) managed via regex

Are you talking about StringRegExp?

For some unknown reason, I have trouble getting that command to work correctly.  I see the help files and try what I can, but I don't seem to get it right.

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

  • Moderators

Admittedly regex makes my eyes bleed, but it would be the most efficient. To do it with String methods, you would have to break it down almost to the letter, not ideal:

Local $finalString, $finalNum
$sString = "ace15877644320"
$aSplit = StringSplit($sString, "")

    For $i = 1 To $aVar[0]
        If StringIsDigit($aSplit[$i]) Then
            $finalNum &= $aSplit[$i]
        Else
            $finalString &= $aSplit[$i]
        EndIf
    Next


    MsgBox(0, "Final String: " & $finalString, "Final Number: " & $finalNum)

Edit to fix horrible spelling.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Are you talking about StringRegExp?

Yes, seemingly the code in your first post means :

"exclude all files whose name begins with Bob/Janet/John or where 3rd char is not a digit "

This can be done with a regexp (this one works but it's redundant and not correct, only an example for demo)

Obviously the proper pattern depends on the requirements

;$dfiles = "Bob.txt"
;$dfiles = "Bob23.txt"
$dfiles = "ab25.txt"   ; <= not excluded
;$dfiles = "a3b.txt"

If not StringRegExp($dfiles, '^(Bob|Janet|John|..\D).*?') Then msgbox(0, "", $dfiles & " : ok")
Link to comment
Share on other sites

Yall got me thinking and I am wondering if I could just say any string that has a number in it add it to the array?  All I am wanting to do is to remove these folders, but I will need a list of folders to remove. 

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

To delete folders which have a number in their name you can do it like this

#include <File.au3>

Local $aFileList = _FileListToArray($parentdir, "*", 2)  ;  2 = Return Folders only

For $i = 1 to $aFileList[0]
   If StringRegExp($aFileList[$i], '\d+') Then DirRemove($parentdir & "\" & $aFileList[$i], 1) ; 1 = remove files and subdirectories    
Next
Link to comment
Share on other sites

Thanks mikell.  It looks good and better than what I was doing.

I guess my next question is I have a few exceptions that have numbers in them.  How do I exclude them from getting deleted?  What I do know is the name but not the number that is going to be next to them.  It should be like bob# or bob##.  I'm excluding bob from being deleted by the way.

RUN . . . Slide . . . TAG . . . Your out . . . PAINTBALL !!!

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...