Jump to content

files and folders


Recommended Posts

With some help! I've put together this: it searches for a file and puts it into an array and table

What I need is to get the folder into an array where the internet favorties are, so i need

c:\documents and settings\user\favorites

the script works really well to recursively search the user profile for the favorties but unfortunately it picks every single file and adds this to the array

c:\documents and settings\user\favorites\fav1.url

c:\documents and settings\user\favorites\fav2.url

c:\documents and settings\user\favorites\fav3.url

c:\documents and settings\user\favorites\fav4.url

c:\documents and settings\user\favorites\fav5.url

c:\documents and settings\user\favorites\fav6.url

c:\documents and settings\user\favorites\fav7.url

Not really sure how to sort this one out!

It needs to scan all subfolders but just return the folder names the files found are in and not the folder names with the filenames...

Does that make sense? :o

$a = _FileSearch(@UserProfileDir & "*.url",1)

If $a[0] > 0 Then

For $i = 1 to $a[0]

ProgressOff()

$answer = MsgBox(262148,"Foundit!", "Found Microsoft Internet Explorer bookmarks (favorties) in the current Windows logon, would you like to add these to the backup?")

if $answer = 6 Then

_ArrayAdd ( $folders, $a[$i] )

GUICtrlSetData($mylist, $a[$i])

Endif

Next

Func _FileSearch($szMask,$nOption)

$szRoot = ""

$hFile = 0

$szBuffer = ""

$szReturn = ""

$szPathList = "*"

Dim $aNULL[1]

If Not StringInStr($szMask,"\") Then

$szRoot = @SCRIPTDIR & "\"

Else

While StringInStr($szMask,"\")

$szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))

$szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))

Wend

EndIf

If $nOption = 0 Then

_FileSearchUtil($szRoot, $szMask, $szReturn)

Else

While 1

$hFile = FileFindFirstFile($szRoot & "*.*")

If $hFile >= 0 Then

$szBuffer = FileFindNextFile($hFile)

While Not @ERROR

If $szBuffer <> "." And $szBuffer <> ".." And _

StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _

$szPathList = $szPathList & $szRoot & $szBuffer & "*"

$szBuffer = FileFindNextFile($hFile)

Wend

FileClose($hFile)

EndIf

_FileSearchUtil($szRoot, $szMask, $szReturn)

If $szPathList == "*" Then ExitLoop

$szPathList = StringTrimLeft($szPathList,1)

$szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"

$szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)

Wend

EndIf

If $szReturn = "" Then

$aNULL[0] = 0

Return $aNULL

Else

Return StringSplit(StringTrimRight($szReturn,1),"*")

EndIf

EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)

$hFile = FileFindFirstFile($ROOT & $MASK)

If $hFile >= 0 Then

$szBuffer = FileFindNextFile($hFile)

While Not @ERROR

If $szBuffer <> "." And $szBuffer <> ".." Then _

$RETURN = $RETURN & $ROOT & $szBuffer & "*"

$szBuffer = FileFindNextFile($hFile)

Wend

FileClose($hFile)

EndIf

EndFunc

Link to comment
Share on other sites

With some help! I've put together this: it searches for a file and puts it into an array and table

What I need is to get the folder into an array where the internet favorties are, so i need

c:\documents and settings\user\favorites

the script works really well to recursively search the user profile for the favorties but unfortunately it picks every single file and adds this to the array

c:\documents and settings\user\favorites\fav1.url

c:\documents and settings\user\favorites\fav2.url

c:\documents and settings\user\favorites\fav3.url

c:\documents and settings\user\favorites\fav4.url

c:\documents and settings\user\favorites\fav5.url

c:\documents and settings\user\favorites\fav6.url

c:\documents and settings\user\favorites\fav7.url

Not really sure how to sort this one out!

It needs to scan all subfolders but just return the folder names the files found are in and not the folder names with the filenames...

Does that make sense? :o

$a = _FileSearch(@UserProfileDir & "*.url",1)

If $a[0] > 0 Then

For $i = 1 to $a[0]

ProgressOff()

$answer = MsgBox(262148,"Foundit!", "Found Microsoft Internet Explorer bookmarks (favorties) in the current Windows logon, would you like to add these to the backup?")

if $answer = 6 Then

_ArrayAdd ( $folders, $a[$i] )

GUICtrlSetData($mylist, $a[$i])

Endif

Next

Func _FileSearch($szMask,$nOption)

$szRoot = ""

$hFile = 0

$szBuffer = ""

$szReturn = ""

$szPathList = "*"

Dim $aNULL[1]

If Not StringInStr($szMask,"\") Then

$szRoot = @SCRIPTDIR & "\"

Else

While StringInStr($szMask,"\")

$szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))

$szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))

Wend

EndIf

If $nOption = 0 Then

_FileSearchUtil($szRoot, $szMask, $szReturn)

Else

While 1

$hFile = FileFindFirstFile($szRoot & "*.*")

If $hFile >= 0 Then

$szBuffer = FileFindNextFile($hFile)

While Not @ERROR

If $szBuffer <> "." And $szBuffer <> ".." And _

StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _

$szPathList = $szPathList & $szRoot & $szBuffer & "*"

$szBuffer = FileFindNextFile($hFile)

Wend

FileClose($hFile)

EndIf

_FileSearchUtil($szRoot, $szMask, $szReturn)

If $szPathList == "*" Then ExitLoop

$szPathList = StringTrimLeft($szPathList,1)

$szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"

$szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)

Wend

EndIf

If $szReturn = "" Then

$aNULL[0] = 0

Return $aNULL

Else

Return StringSplit(StringTrimRight($szReturn,1),"*")

EndIf

EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)

$hFile = FileFindFirstFile($ROOT & $MASK)

If $hFile >= 0 Then

$szBuffer = FileFindNextFile($hFile)

While Not @ERROR

If $szBuffer <> "." And $szBuffer <> ".." Then _

$RETURN = $RETURN & $ROOT & $szBuffer & "*"

$szBuffer = FileFindNextFile($hFile)

Wend

FileClose($hFile)

EndIf

EndFunc

maybe i can just use stringtrim right but as each filename is different i dont know how many charactors to trim!

Link to comment
Share on other sites

maybe this

#Include <File.au3>
#Include <Array.au3>

$Location = @FavoritesDir & "\"

$FileList=_FileListToArray($Location, "*", 2)
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
_ArrayDisplay($FileList,"$FileList")

MsgBox(64, " First Folder", $Location & $FileList[1])

8)

NEWHeader1.png

Link to comment
Share on other sites

maybe i can just use stringtrim right but as each filename is different i dont know how many charactors to trim!

HI, what about _FileGetDir() ? _FileGetDir

Or maybe StringSplit could help.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

HI, what about _FileGetDir() ? _FileGetDir

Or maybe StringSplit could help.

So long,

Mega

the first code finds favorites\bookmark folders where it should stop at the first level favorites

the second idea seems to need a specific filename

as I know the depth of the folder because i specify it in the search "c:\documents and settings\nick\favorites"

i know there is going to be three \ \ \ and the forth "\" and after need to be trimmed.

In dos I would use a for .. next loop with delims and tokens

Maybe the same can be done with autoit.

Link to comment
Share on other sites

the first code finds favorites\bookmark folders where it should stop at the first level favorites

the second idea seems to need a specific filename

as I know the depth of the folder because i specify it in the search "c:\documents and settings\nick\favorites"

i know there is going to be three \ \ \ and the forth "\" and after need to be trimmed.

In dos I would use a for .. next loop with delims and tokens

Maybe the same can be done with autoit.

actually bugger, i dont know the depth of the path, it may be somewhere else thats the point of the search! DOH!

Link to comment
Share on other sites

cant i trim right everything up to and including the first "\" charactor?

Do I get you right? You´ll find a file in a folder and you don´t know how deep the path will be. Then you want to get the path?

Example:

c:\123\12\1\text.txt you need c:\123\12\1\

d:\fewrewr\werewr\hugo.123 you need d:\fewrewr\werewr\

Is that the point?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Do I get you right? You´ll find a file in a folder and you don´t know how deep the path will be. Then you want to get the path?

Example:

c:\123\12\1\text.txt you need c:\123\12\1\

d:\fewrewr\werewr\hugo.123 you need d:\fewrewr\werewr\

Is that the point?

So long,

Mega

yes exactly! what I have done does this if you look at the code but 2 problems

1. it returns c:\123\12\1\text.txt instead of c:\123\12\1 as you said

2. it then finds

c:\123\12\1\text.txt

c:\123\12\1\text2.txt

c:\123\12\1\2331\321\text.txt

it should just stop at c:\123\12\1\ and return this without the filename and not keep loooking anymore.

as a kind person gave me the search function (i did the rest of the code around it) im not sure how to change it. if i remove the findnextfile section it goes a bit odd !!

Link to comment
Share on other sites

yes exactly! what I have done does this if you look at the code but 2 problems

1. it returns c:\123\12\1\text.txt instead of c:\123\12\1 as you said

2. it then finds

c:\123\12\1\text.txt

c:\123\12\1\text2.txt

c:\123\12\1\2331\321\text.txt

it should just stop at c:\123\12\1\ and return this without the filename and not keep loooking anymore.

as a kind person gave me the search function (i did the rest of the code around it) im not sure how to change it. if i remove the findnextfile section it goes a bit odd !!

it should just stop at c:\123\12\1\

arrrgh! no, i mean it should stop finding files in this directory and then move on to the next c:\123\12\1\2331\321\

so if we have the following pst files

c:\documents and settings\nick\test.pst

" \james.pst

" \tod.pst

c:\documents and settings\nick\old\archive.pst

it should return

c:\documents and settings\nick

c:\documents and settings\nick\old

Link to comment
Share on other sites

Hmmh, if you´ve got the path when you found a file you could use the func mentioned above.

$path1 = "c:\1\dwsda\edw\wedewfd\hugo.123"
$path2 = "c:\1\dwsda\hugo.123"

MsgBox(0, "", _FileGetDir($path1))

Func _FileGetDir($sFile)
    Local $s, $i
    $s = ""
    If FileExists($sFile) Then
        $sFile = StringSplit($sFile, "\")
        For $i = 1 To $sFile[0] - 1
            If $i > 1 Then
                $s = $s & "\" & $sFile[$i]
            Else
                $s = $s & $sFile[$i]
            EndIf
        Next
        Return $s
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc  ;==>_FileGetDir

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

$PATH = "C:\Documents and Settings\One\Favorites\AutoIt\Using Autoit3 Production and Beta.url"

$STR = StringSplit($PATH, "\")
$PATH = StringTrimRight($PATH, StringLen($STR[$STR[0]]))
$II = MsgBox(262145,"","" & $PATH)
; Result = "C:\Documents and Settings\One\Favorites\AutoIt\"

this works, maybe it will give you ideas!

Link to comment
Share on other sites

  • Developers

$PATH = "C:\Documents and Settings\One\Favorites\AutoIt\Using Autoit3 Production and Beta.url"
$DirName = StringLeft($path,StringInStr($path,"\",0,-1)-1)
$Filename = StringTrimLeft($path,StringInStr($path,"\",0,-1))

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

$PATH = "C:\Documents and Settings\One\Favorites\AutoIt\Using Autoit3 Production and Beta.url"
$DirName = StringLeft($path,StringInStr($path,"\",0,-1)-1)
$Filename = StringTrimLeft($path,StringInStr($path,"\",0,-1))

Im excited! Nearly there

this works perfect! - except if there is one file in the folder you get 1 msg box, if there are 100 the msg box comes up 100 times! :o

$a = _FileSearch(@UserProfileDir & "\favorites\*.url",1)

If $a[0] > 0 Then

;For $i = 1 to $a[0]

For $i = 1 to $a[0]

$STR = StringSplit($a[$i], "\")

$a[0] = StringTrimRight($a[$i], StringLen($STR[$STR[0]]))

ProgressOff()

$answer = MsgBox(262148,"Found, "Favorites!")

if $answer = 6 Then

_ArrayAdd ( $folders, $a[$i] )

GUICtrlSetData($mylist, $a[$i])

Endif

Next

Func _FileSearch($szMask,$nOption)

$szRoot = ""

$hFile = 0

$szBuffer = ""

$szReturn = ""

$szPathList = "*"

Dim $aNULL[1]

If Not StringInStr($szMask,"\") Then

$szRoot = @SCRIPTDIR & "\"

Else

While StringInStr($szMask,"\")

$szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))

$szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))

Wend

EndIf

If $nOption = 0 Then

_FileSearchUtil($szRoot, $szMask, $szReturn)

Else

While 1

$hFile = FileFindFirstFile($szRoot & "*.*")

If $hFile >= 0 Then

$szBuffer = FileFindNextFile($hFile)

While Not @ERROR

If $szBuffer <> "." And $szBuffer <> ".." And _

StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _

$szPathList = $szPathList & $szRoot & $szBuffer & "*"

$szBuffer = FileFindNextFile($hFile)

Wend

FileClose($hFile)

EndIf

_FileSearchUtil($szRoot, $szMask, $szReturn)

If $szPathList == "*" Then ExitLoop

$szPathList = StringTrimLeft($szPathList,1)

$szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"

$szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)

Wend

EndIf

If $szReturn = "" Then

$aNULL[0] = 0

Return $aNULL

Else

Return StringSplit(StringTrimRight($szReturn,1),"*")

EndIf

EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)

$hFile = FileFindFirstFile($ROOT & $MASK)

If $hFile >= 0 Then

$szBuffer = FileFindNextFile($hFile)

While Not @ERROR

If $szBuffer <> "." And $szBuffer <> ".." Then _

$RETURN = $RETURN & $ROOT & $szBuffer & "*"

$szBuffer = FileFindNextFile($hFile)

Wend

FileClose($hFile)

EndIf

EndFunc

Link to comment
Share on other sites

  • Developers

Im excited! Nearly there

this works perfect! - except if there is one file in the folder you get 1 msg box, if there are 100 the msg box comes up 100 times! :o

Looking at your script i have no idea what it is doing... Is it doing a recursive file search ?

And what is it you want to accomplish ?

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

see really what i need is it to stop when its found one file in

c:\favorites

and not continue to list the other files with the same extension and search subfolders

just stop there

can someone help me?

Ok so you want to specify a filename ? and hen that is found retunr its full path ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok so you want to specify a filename ? and hen that is found retunr its full path ?

well um...

yes I want it to search the folder and all subfolders for a file, when it finds the first one it stops and returns the dir

its almost there, just need to stop it carrying on after its found the 1st!

Link to comment
Share on other sites

  • Developers

here is a recursive search i wrote a while ago...

Added one line between ;****** lines to just return the first nfile found.

Normally it would return an array with all found files.

Dim $FILES
Dim $DIR = "c:\documents and settings\user\favorites"; specify the directory to search
Dim $FILEMASK = "*.url"                   ; specify the filemask

$FILE = _GetFileList($DIR, $FILEMASK)
MsgBox(0, 'Found file:', $FILE)
;For $X = 0 To UBound($FILES) - 1
;   MsgBox(0, 'test:' & $X, $FILES[$X])
;Next 
Exit


Func _GetFileList($T_DIR, $T_MASK)
    Dim $N_DIRNAMES[200000]; max number of directories that can be scanned
    Local $N_DIRCOUNT = 0
    Local $N_FILE
    Local $N_SEARCH
    Local $N_TFILE
    Local $N_OFILE
    Local $T_FILENAMES
    Local $T_FILECOUNT
    Local $T_DIRCOUNT = 1
; remove the end \ If specified
    If StringRight($T_DIR, 1) = "\" Then $T_DIR = StringTrimRight($T_DIR, 1)
    $N_DIRNAMES[$T_DIRCOUNT] = $T_DIR
; Exit if base dir doesn't exists
    If Not FileExists($T_DIR) Then Return 0
; keep on looping until all directories are scanned
    While $T_DIRCOUNT > $N_DIRCOUNT
        $N_DIRCOUNT = $N_DIRCOUNT + 1
; find all subdirs in this directory and save them in a array
        $N_SEARCH = FileFindFirstFile($N_DIRNAMES[$N_DIRCOUNT] & "\*.*")  
        While 1
            $N_FILE = FileFindNextFile($N_SEARCH) 
            If @error Then ExitLoop
    ; skip these references
            If $N_FILE = "." Or $N_FILE = ".." Then ContinueLoop
            $N_TFILE = $N_DIRNAMES[$N_DIRCOUNT] & "\" & $N_FILE
    ; if Directory than add to the list of directories to be processed 
            If StringInStr(FileGetAttrib($N_TFILE), "D") > 0 Then
                $T_DIRCOUNT = $T_DIRCOUNT + 1
                $N_DIRNAMES[$T_DIRCOUNT] = $N_TFILE
            EndIf
        WEnd
        FileClose($N_SEARCH)
; find all Files that mtach the MASK
        $N_SEARCH = FileFindFirstFile($N_DIRNAMES[$N_DIRCOUNT] & "\" & $T_MASK)  
        If $N_SEARCH = -1 Then ContinueLoop
        While 1
            $N_FILE = FileFindNextFile($N_SEARCH) 
            If @error Then ExitLoop
    ; skip these references
            If $N_FILE = "." Or $N_FILE = ".." Then ContinueLoop
            $N_TFILE = $N_DIRNAMES[$N_DIRCOUNT] & "\" & $N_FILE
    ; if Directory than add to the list of directories to be processed 
            If StringInStr(FileGetAttrib($N_TFILE), "D") = 0 Then
        ;***** return first found
                Return $N_TFILE
        ;*****
                $T_FILENAMES = $T_FILENAMES & $N_TFILE & @CR
                $T_FILECOUNT = $T_FILECOUNT + 1
        ;MsgBox(0,'filecount ' & $T_FILECOUNT ,$N_TFILE)
            EndIf
        WEnd
        FileClose($N_SEARCH)
    WEnd
    $T_FILENAMES = StringTrimRight($T_FILENAMES, 1)
    $N_OFILE = StringSplit($T_FILENAMES, @CR)
    Return ($N_OFILE)
EndFunc ;==>_GetFileList
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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