Jump to content

Unsure how to find the right path


Recommended Posts

I am looking for a func that will let me find a folder name. if I tell the script where to look eg. C:\Programdata\microsoft, and to look for a folder called "Testxxxxx" where the xxxxxx = a random number and pull that back to a varible. how do I do that.

 

I have looked through the help file and looked at google, I can't really find anything.

Any help would be nice

Link to comment
Share on other sites

  • Moderators

You must have missed _FileListToArray and _FileListToArrayRec in your searches. If you are looking for a folder in C:\Temp with some variation of "Testxxx" you would simply do this:

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

Local $aDirs = _FileListToArray("C:\Temp", "Test*", $FLTA_FOLDERS, True)
    If IsArray($aDirs) Then
        _ArrayDisplay($aDirs)
    Else
        ConsoleWrite("No folders matching the criteria found." & @CRLF)
    EndIf

I'll leave you to look through the help file to see when you would use _FileListToArrayRec over _FileListToArray.

"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

  • Moderators
12 minutes ago, scwoobles said:

I always have trouble with array's I Just cant grasp them.

Then you have a long, hard road ahead of you when it comes to scripting...

"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

  • Moderators

No, you would read the help file :) Look at the last parameter for _FileListToArray

"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

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

Local $aSplittedString
Local $aDirs = _FileListToArray ( "C:\Temp", "Test*", $FLTA_FOLDERS, True )
If IsArray ( $aDirs ) Then
    _ArrayDisplay ( $aDirs )  ; Notice element [0] contains the number of matches

    For $i = 1 to $aDirs [0]  ; For element 1 in the array to the element matches found in ( element [0] )
        ConsoleWrite ( "Folder: " & $aDirs [$i] & @CRLF )   ; Write the full folder name path to the console window
        $aSplittedString = StringSplit ( $aDirs [$i], "\" ) ; Split the folder name into a new array, split character = '\'.
        ConsoleWrite ( "The part you wanted : " & $aSplittedString [ Ubound ($aSplittedString) -1 ]  & @CRLF )
    Next
Else
    ConsoleWrite ( "No folders matching the criteria found." & @CRLF )
EndIf

I suggest read: https://www.autoitscript.com/wiki/Arrays. Its very helpful :)

Link to comment
Share on other sites

#include <MsgBoxConstants.au3>
MsgBox(0, @ScriptName, Example(), 60)
Func Example()
    ; Assign a Local variable the search handle of all files in the current directory.
    Local $hSearch = FileFindFirstFile(@AppDataCommonDir & "\microsoft\test*")
    ; Check if the search was successful, if not display a message and return False.
    If $hSearch = -1 Then
        ;MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
        Return SetError(1, 0, "")
    EndIf

    ; Assign a Local variable the empty string which will contain the files names found.
    Local $sFileName = "", $iResult = 0

    While 1
        $sFileName = FileFindNextFile($hSearch)
        ; If there is no more file matching the search.
        If @error Then ExitLoop
        
        FileClose($hSearch)
        Return SetError(0, 0, $sFileName) ; you're looking for 1 anywayz

        ; Display the file name.
        $iResult = MsgBox(BitOR($MB_SYSTEMMODAL, $MB_OKCANCEL), "", "File: " & $sFileName)
        If $iResult <> $IDOK Then ExitLoop ; If the user clicks on the cancel/close button.
    WEnd

    ; Close the search handle.
    FileClose($hSearch)
    Return SetError(2, 0, "")
EndFunc   ;==>Example

..am I too late to share this ? maybe now you can fish and catch oysters too :) 

Edited by argumentum
forgot to FileClose($hSearch)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Moderators
8 minutes ago, argumentum said:

..am I too late to share this ? maybe now you can fish and catch oysters too :) 

From 8 lines to 25, 3 times as slow, and only returns the first folder that meets the criteria. How is this worthwhile to the OP?

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

2 minutes ago, JLogan3o13 said:

and only returns the first folder that meets the criteria. How is this worthwhile to the OP?

 

17 hours ago, scwoobles said:

I am looking for a func that will let me find a folder name.

So I pasted the code.

1 hour ago, scwoobles said:

I always have trouble with array's I Just cant grasp them

So I pasted the code.

So the OP could have done this without much knowing of anything, just reading the help file. I'm just showing another way. Hope not to have offended or mislead or in any way created a detrimental situation for anyone.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

4 hours ago, scwoobles said:

 Anyway ok I got that working and it displays fine but how would I split the string just to get the folder. so instead of C:\temp\test1234 I would just get the test1234

The fastest (and as i think easiest) way is:

$sFullPath= 'C:\temp\test1234'; I would just get the test1234
$iPos=StringInStr($sFullPath,'\',0,-1)
$sLastFolder=StringMid($sFullPath,StringLen($sFullPath)-$iPos+1)
ConsoleWrite($sLastFolder&@CRLF)

 

Link to comment
Share on other sites

2 hours ago, scwoobles said:

I do appreciate all the feedback it now lets me know that I have to read the help file 30 times

Reading helpfile is always a good idea, but also testing the examples in helpfile brings some more feedback to your brain. I use AutoIt since > 7 years and every day i read in helpfile at least once a day. Most i also test the example.

Edited by AutoBert
Link to comment
Share on other sites

44 minutes ago, argumentum said:

are you turning this into a religion

no, but i am a old man. Only blond haircutters must new learn after pausing more as 1/2 hour. Some functions i don't need every day i have also the need to read about.

And no some roses for @Jos and the other devs: AutoIt is the first windows language, where i understood the helpfile from the beginning.

But maybe the dev's must write a tut who to use this helpfile, seems most people ignore it. But's the best i ever read.

Edited by AutoBert
Link to comment
Share on other sites

lol, you think your memory is bad @AutoBert. I've written programs that I forgot I did and wrote because, then again, forgot I wrote. Functions ? oh my oh my.
But the way you wrote it left you open for that and I, in a jolly mode, had to post :) 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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