ktxmatrix Posted January 5, 2005 Share Posted January 5, 2005 Hey guys, Just wanted to know if there is any function like the MS-DOS 'dir' command to get a listing of all the files and directories in a particular folder ? I know that I can run the DOS prompt, and then use 'dir' to output the results to a file. I am working with a network drive and a local one for an FTP transfer situation in which I need to find if a folder on the local machine already exists on the server. In that case I have to go and download a folder created after the one I already have. It would help immensely if there is a function out there that I dont know about. Any help in this matter is greatly appreciated. Thanks Link to comment Share on other sites More sharing options...
Chris_1013 Posted January 5, 2005 Share Posted January 5, 2005 Trying searching the forum for _FileSearch - this is a useful little UDF that's been written that returns an array of the results you should be able to use. Link to comment Share on other sites More sharing options...
sugi Posted January 5, 2005 Share Posted January 5, 2005 Just use FileFind*-functions. Link to comment Share on other sites More sharing options...
Chris_1013 Posted January 5, 2005 Share Posted January 5, 2005 Yeah, the _FileSearch is just a nice little wrapper to these functions. Link to comment Share on other sites More sharing options...
SlimShady Posted January 5, 2005 Share Posted January 5, 2005 (edited) I am making one now. But I'm trying to add options. So you can choose to: - display the full path for every file - display the contents in a message box - list the files from subfolders Edited January 5, 2005 by SlimShady Link to comment Share on other sites More sharing options...
ktxmatrix Posted January 6, 2005 Author Share Posted January 6, 2005 Thanks everyone for the link to the _FileSearch function. I just needed to list the directories in a particular folder which is located on a networked drive. I can hence not use the 'dir' command. For some weird reason, FilGetAttrib is not working like it should. I try to look for the attribute letter "d" in the string returned by the FileGetAttrib function but it does not return anything and ends up listing all the files along with the directories. Is there a bug logged against this function ? or am I doing something horribly stupid. Link to comment Share on other sites More sharing options...
ezzetabi Posted January 6, 2005 Share Posted January 6, 2005 (edited) You are probably doing something strange. In fact I always used code like this (not in func usually, but it give the idea) for detecting if a file is a folder or not, even in network path (e.g. \\sever\c$\test). Func _IsDir($sPath) If StringInStr(FileGetAttrib($sPath),'d') Then Return 1 Else Return 0 EndIf EndFunc Edit: _FileSeach functions are not 'just a wrapper' of FileFindFirstFile and FileFindNext file since they can go in subdirectories! It is not so banal as it seems. If you don't think so, try to make your own. Edit2: This is my _FileSeach func... CODE Func _FileSearch($sIstr, $iSF) ; $iSF can sum up. ; $iSF = 4 means don't return also folders, only files. ; $iSF = 2 means stop at the first found. ; $iSF = 1 means looking in subfolders. ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements. Local $sIstr, $iSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1] $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1)) If $sCP = '' Then $sCP = @WorkingDir & '\' $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1)) If $sCriteria = '' Then $sCriteria = '*.*' ;To begin we seek in the starting path. $sCS = FileFindFirstFile($sCP & $sCriteria) While $sCS <> - 1 $sCF = FileFindNextFile($sCS) If @error Then FileClose($sCS) ExitLoop EndIf If $sCF = '.' Or $sCF = '..' Then ContinueLoop If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sCP & $sCF), 'd')) Then $sOutPut = $sOutPut & $sCP & $sCF & @LF If BitAND($iSF, 2) Then ExitLoop EndIf Wend ;And after, if needed, in the rest of the folders. If (BitAND($iSF, 2) Or BitAND($iSF, 3) And $sOutPut = '') Or (BitAND($iSF, 1) And Not BitAND($iSF, 2)) Then $sBuffer = @CR & $sCP & '*' & @LF ;The buffer is set for keeping the given path plus a *. Do $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1) ;current search. $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1)) ;Current search path. $iH = FileFindFirstFile($sCS) While $iH <> - 1 $sCF = FileFindNextFile($iH) If @error Then FileClose($iH) ExitLoop EndIf If $sCF = '.' Or $sCF = '..' Then ContinueLoop If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer ;Every folder found is added in the begin of buffer $sFP = $sCP & $sCF & '\' ; for future search $iH2 = FileFindFirstFile($sFP & $sCriteria) ; and checked with the criteria. While $iH2 <> - 1 $sCF2 = FileFindNextFile($iH2) If @error Then FileClose($iH2) ExitLoop EndIf If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sFP & $sCF2), 'd')) Then $sOutPut = $sOutPut & $sFP & $sCF2 & @LF ;Found items are put in the Output. If BitAND($iSF, 2) Then FileClose($iH2) FileClose($iH) ExitLoop 3 EndIf EndIf Wend EndIf Wend $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '') Until $sBuffer = '' EndIf If $sOutPut = '' Then $aNull[0] = 0 Return $aNull Else Return StringSplit(StringTrimRight($sOutPut, 1), @LF) EndIf EndFunc ;==>_FileSearch Edited January 6, 2005 by ezzetabi Link to comment Share on other sites More sharing options...
ktxmatrix Posted January 6, 2005 Author Share Posted January 6, 2005 I feel like an idiot upopn having found a workaround so quickly after posting my last message. I found a way to differentiate between directories and files if for some reason the 'StringInStr($text, FileGetAttrib($file, 'd'))' function thingy doesnt work. The FileGetTime function returns an error everytime you try to get the creation date of a folder but never on a file. So I am using that instead to list out only the folders in a particular folder in my program currently. Nothing brilliant really...just a simple workaround that did it. Just wanna say a big thank you to all the helpful replies and posts and specifically the _FileSearch function. Really pushed me in the right direction. Here is the code if anyone is interested. Global $dir_list[100]; local $i = 0; ; Shows the filenames of all files in the current directory, note that "." and ".." are returned. $search = FileFindFirstFile($dir & "*.*") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop if $file = "." OR $file = ".." then ContinueLoop if (StringInStr($dir & "*",FileGetAttrib ($file), "D")) then $t = FileGetTime($dir & $file, 1); If @error Then $dir_list[$i] = $file; MsgBox(0, "Directory found", $file); $i = $i + 1; EndIf endif WEnd ; Close the search handle FileClose($search) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now