flat assembler
Message board for the users of flat assembler.

Index > Windows > Search haddrive for specific files

Author
Thread Post new topic Reply to topic
seth0x2b



Joined: 02 Dec 2006
Posts: 8
seth0x2b 03 Dec 2006, 12:54
Hey guys.
Nice to see an assembler forum that has alot of visitors. I just started learning ASM about 1 week ago, and all I can say is that "IT's GREAT!" Smile.

Ok..so here's my problem...I'm trying for the past week to find(or do) a program to search the entire harddrive(all physical drives/partitions) for a file with a specific extension. In the end I'd like to write the result to a log file but that part I think I could write with no problem

All I came up with(actually found on this forum here http://board.flatassembler.net/topic.php?t=4434 and then simplified a bit) is a routine to search for a specific file extension inside a given folder. All I need now is a way to get the routine to run with a recursive search on all folders.


Code:
format PE GUI 4.0
entry BeginCode

include '%finc%\win32a.inc'

section '.code' executable readable writeable

BeginCode:
        invoke FindFirstFileA,filemask,wfd
        mov [search_handle],eax
@@:
        invoke CreateFileA,wfd.cFileName,(GENERIC_READ+GENERIC_WRITE),0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
        mov [file_handle],eax
        jmp FoundOne
FindNext:
        invoke FindNextFileA,[search_handle],wfd
        cmp eax,0
        je AllDone
        jmp @B

AllDone:
        ret
FoundOne:
        invoke MessageBox,0,wfd.cFileName,NULL,0
        jmp FindNext

section '.data' data readable writeable

wfd             FINDDATA
filemask        db      'C:\WINDOWS\*.bmp',0 
search_handle   dd      ?
file_handle     dd      ?


section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel32,\
         FindFirstFileA,'FindFirstFileA',\
         FindNextFileA,'FindNextFileA',\
         CreateFileA,'CreateFileA'

  import user,\
         MessageBox,'MessageBoxA'
    


I would apreciate any help and/or code

Thanks alot
Post 03 Dec 2006, 12:54
View user's profile Send private message Reply with quote
Remy Vincent



Joined: 16 Sep 2005
Posts: 155
Location: France
Remy Vincent 03 Dec 2006, 15:43
Yeah, let's talk about
seth0x2b wrote:
Ok.. search the entire harddrive...
I would apreciate any help and/or code
Thanks alot


Maybe knowing the usual "CODING" steps needed before beeing able to search an entire Drive, could help, and so you could skip some steps, and learn directly the last and more difficult step.

STEP 1 : Learn coding about how to process ONLY ONE file...
STEP 2 : Learn coding about how to process ONLY TWO files...
STEP 3 : Learn coding about how to process ONLY 3 files...
STEP 4 : Learn coding about how to process MANY files inside the SAME directory...
STEP 5 : Learn coding about how to process MANY files inside MANY directories...
STEP 6 : Learn coding about how to process MANY files inside MANY directories and MANY sub-directories...

I think that the terms "Entire Disk Search" contains a "brain process" that is as hard as... COMPARING "EACH FOUND FILE" WITH "ALL OTHER FILES"

But all this warning about skipping the needed steps and learn directly the hardest stuff is only a warning.. for example I remember I did it some years ago, in Pascal language, skipping many coding steps ; it was very funny and I was proud, BUT I was absolutly not able coding this algorithlm in another language than my usual one, Pascal, because I was programming skipping many coding steps...

_________________
Groups lower your IQ
Post 03 Dec 2006, 15:43
View user's profile Send private message Visit poster's website Reply with quote
seth0x2b



Joined: 02 Dec 2006
Posts: 8
seth0x2b 03 Dec 2006, 16:02
Not the best answer in the world but I can accept a little criticism.

I know I should not skip steps in the process of learning...but every newbie starts up with code snippets and then learns what everything does...At least this is my way of learning and it has served me good in the past.
I am not a newbie programmer..I'm just a newbie at ASM.

Thanks for your reply..
Still waiting for someone with a more constructive answer...

Cheers
Post 03 Dec 2006, 16:02
View user's profile Send private message Reply with quote
Remy Vincent



Joined: 16 Sep 2005
Posts: 155
Location: France
Remy Vincent 03 Dec 2006, 17:16
Code:
   {----- CONTROL Parameter 1 : File exists ?  }
   GI_FilePath1 := ParamStr(1);
   IF GI_FilePath1 <> ''
   THEN BEGIN
      {----- Disk files access }
      FindFirst ( GI_FilePath1 , AnyFile , G_SearchRec );
      G_FileExistsQ := ( DosError = 0 );

      {----- File found ?  }
      IF G_FileExistsQ = False THEN BEGIN
         WriteLn (   'ERROR: '  ,  'FilePath1 not found  ( Parameter 1 ).'   );
         HALT ( 1 );
      END;
   END
   ELSE BEGIN
      WriteLn (   'ERROR: '  ,  'FilePath1 expected  ( Parameter 1 ).'   );
      HALT ( 1 );
   END;
    


This code is about searching files, and this code is really hard to use, because we often use a single FILEEXISTS() function instead of coding more lines, but using more usual calls... Yeah,... because FINDFIRST is very close from the interrupt call used by assembler programmers with DOS INTERRUPT 21

I would not be able to code this in FAMS, without "forgetting" all my currents projects!!

_________________
Groups lower your IQ
Post 03 Dec 2006, 17:16
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 03 Dec 2006, 18:25
ls.asm (NASM, Linux)

Quote:

It is 1017 bytes in size, and sports many of the important command-line options:

-C columnar output; default if stdout is a tty
-1 one file per output line; default if stdout is not a tty
-l "long" output
-F show file type suffixes
-i show inode numbers
-s show size of files in 1K-blocks
-d don't expand directories
-a show all (hidden) files
-B don't show files ending in ~ (backups)
-N don't replace non-graphic characters with a question mark
-b show non-graphic characters as octal escape sequences
-R recursively list contents of subdirectories

(I'm especially proud of getting that last feature to work.)

The "long" output format only displays numeric user IDs, and it displays timestamps as an age, instead of the actual timestamp. There is no sorting capability, and the columnar output is much less intelligent than GNU's (though it looks pretty good most of the time). Beyond that, however, it conforms pretty closely to the standard ls program we all know and love.
Post 03 Dec 2006, 18:25
View user's profile Send private message Visit poster's website Reply with quote
seth0x2b



Joined: 02 Dec 2006
Posts: 8
seth0x2b 03 Dec 2006, 19:15
Wow that's much code Very Happy ...I'll have a look at it..

I've made some progress towards my goal..Ive written a program that probes for all drive letters and displays the ones that are fixed(harddrives).Easily changeable to any type of drive..
I'll post it since someone else would probably need something like this sometime ....
Code:
format PE GUI 4.0
entry start

include '%finc%\win32a.inc'

section '.data' data readable writeable
drive         db '?:\',0 ;drive name "template"
letter        dd  ?      ;to hold the current drive letter

;========= DRIVE TYPES ==========;
UNKNOWN = 0           ; the drive type is unknown
DOES_NOT_EXIST = 1    ; the drive does not exist
DRIVE_REMOVABLE = 2   ; floppy
DRIVE_FIXED = 3       ; harddrives
DRIVE_REMOTE = 4      ; USB?
DRIVE_CDROM = 5       ; cdrom units
DRIVE_RAMDISK = 6     ; ramdisks

section '.code' code readable executable
start:

        mov [letter],'A'
theLoop:                  ;loop through letters
        mov ecx, [letter]
        mov [drive], cl   ;add current letter to drive name (example: 'A:\')
        call get          ;get info for current drive letter
        inc [letter]      ;next letter
        cmp [letter],'Z'  ;compare the current letter to 'Z'...
        jg  exit          ;... and jump if current letter is greater than 'Z'

        jmp theLoop       ;from the top Smile
exit:
        invoke ExitProcess,0
        ret
get:
        invoke GetDriveType,drive
        cmp eax, DRIVE_FIXED      ;compare
        jz foundONE               ;jump if the result from GetDriveType equals DRIVE_FIXED
ret

foundONE:
        invoke MessageBox,0,drive,0,0 ;display drive name
        ret
section '.idata' import data readable writeable
  library kernel32,'KERNEL32.DLL',user32,'USER32.DLL'
  include '%finc%\apia\kernel32.inc'
  include '%finc%\apia\user32.inc'

    


Cheers
Post 03 Dec 2006, 19:15
View user's profile Send private message Reply with quote
Remy Vincent



Joined: 16 Sep 2005
Posts: 155
Location: France
Remy Vincent 03 Dec 2006, 21:35
seth0x2b wrote:
... ...
Ive written a program that probes for all drive letters
... ...


I'm sorry but all my drives letters are not displayed, so I've added some lines, and now all my drives letters are displayed, even USB "flash drives", the four (4) lines I've added are commented with ; A D D E D...
Code:
format PE GUI 4.0
entry start 

include 'MyPathTo \Fasm167\FasmW\INCLUDE\win32a.inc'

section '.data' data readable writeable 
drive         db '?:\',0 ;drive name "template" 
letter        dd  ?      ;to hold the current drive letter 

;========= DRIVE TYPES ==========; 
UNKNOWN = 0           ; the drive type is unknown 
DOES_NOT_EXIST = 1    ; the drive does not exist 
DRIVE_REMOVABLE = 2   ; floppy 
DRIVE_FIXED = 3       ; harddrives 
DRIVE_REMOTE = 4      ; USB? 
DRIVE_CDROM = 5       ; cdrom units 
DRIVE_RAMDISK = 6     ; ramdisks 

section '.code' code readable executable 
start: 

        mov [letter],'A' 
theLoop:                  ;loop through letters 
        mov ecx, [letter] 
        mov [drive], cl   ;add current letter to drive name (example: 'A:\') 
        call get          ;get info for current drive letter 
        inc [letter]      ;next letter 
        cmp [letter],'Z'  ;compare the current letter to 'Z'... 
        jg  exit          ;... and jump if current letter is greater than 'Z' 

        jmp theLoop       ;from the top  
exit: 
        invoke ExitProcess,0 
        ret 
get: 
        invoke GetDriveType,drive 
        cmp eax, DRIVE_FIXED      ;compare 
        jz foundONE               ;jump if the result from GetDriveType equals DRIVE_FIXED 
        cmp eax, DRIVE_CDROM      ; A D D E D
        jz foundONE               ; A D D E D
        cmp eax, DRIVE_REMOVABLE  ; A D D E D
        jz foundONE               ; A D D E D
ret

foundONE: 
        invoke MessageBox,0,drive,0,0 ;display drive name 
        ret 
section '.idata' import data readable writeable 
  library kernel32,'KERNEL32.DLL',user32,'USER32.DLL' 
  include 'MyPathTo \Fasm167\FasmW\api\kernel32.inc'
  include 'MyPathTo \Fasm167\FasmW\INCLUDE\api\user32.inc'
    

Do you fill it usefull??????
Post 03 Dec 2006, 21:35
View user's profile Send private message Visit poster's website Reply with quote
seth0x2b



Joined: 02 Dec 2006
Posts: 8
seth0x2b 03 Dec 2006, 21:55
I've specified that it finds only fixed drives...I initially designed the program to show all drives found...but since I am looking only for fixed drives, I posted the coresponding code.

Thanks for showing interest though Smile

Still waiting/learning/searching for info on the recursion part...

Cheers


Last edited by seth0x2b on 03 Dec 2006, 21:59; edited 1 time in total
Post 03 Dec 2006, 21:55
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 03 Dec 2006, 21:58
@Remmy,
Quote:

I'm sorry but all my drives letters are not displayed, so I've added some lines, and now all my drives letters are displayed, even USB "flash drives"


Quote:

Ive written a program that probes for all drive letters and displays the ones that are fixed(harddrives).


@Both, look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getlogicaldrives.asp , possible this function can make the program faster (a speedup probably unneded of course...)
Post 03 Dec 2006, 21:58
View user's profile Send private message Reply with quote
seth0x2b



Joined: 02 Dec 2006
Posts: 8
seth0x2b 03 Dec 2006, 22:05
LocoDelAssembly wrote:

@Both, look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getlogicaldrives.asp , possible this function can make the program faster (a speedup probably unneded of course...)


Actually I've used "GetLogicalDrives" in the Delphi version of the program that I want to achieve right now...

Maybe it speeds stuff up, but I don't think it's that noticeable..


[EDIT] Made a little mistake...I use "GetLogicalDriveStrings" http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getlogicaldrivestrings.asp


Cheers
Post 03 Dec 2006, 22:05
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 03 Dec 2006, 22:30
Quote:

[EDIT] Made a little mistake...I use "GetLogicalDriveStrings" http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getlogicaldrivestrings.asp

I saw that function but I think that the function that returns a bitmap is better, is easy to process and needs less resources.

Code:
drive db "A"-1,":\",0
.
.
.
  invoke GetLogicalDrives
  mov    esi, eax

.loop:
  inc    byte[drive]
  shr   esi, 1
  jnc   .loop
  jz    .exit

  invoke GetDriveType, drive
  cmp eax, DRIVE_FIXED
  jnz .loop

; **** Here process the fixed drive

  jmp .loop    
(UNTESTED)
Post 03 Dec 2006, 22:30
View user's profile Send private message Reply with quote
donkey7



Joined: 31 Jan 2005
Posts: 127
Location: Poland, Malopolska
donkey7 04 Dec 2006, 12:00
many years ago, when i was 14 or 15 i started wiriting html editor (i've stopped development when i tried to do syntax highlighting - richedit doesn't have such feature and i was too lazy to learn and make own control).

here it is:
http://asembler.republika.pl/bin/klop.zip

one of the features it had is adding all files from specified folders (and all subfolders). the code looks as follows:

Code:
DodajPlikiDoProjektu proc
  LOCAL szukaj:HANDLE

  invoke PathAppend,addr bufor,addr gwiazdka
  invoke FindFirstFile,addr bufor,addr dane_pliku
  .IF eax==INVALID_HANDLE_VALUE
    ShowLastError
    ret
  .ELSE
    mov szukaj,eax
  .ENDIF

szukej:
  mov eax,dane_pliku.dwFileAttributes
  and eax,FILE_ATTRIBUTE_DIRECTORY
  jz kuper
  mov eax,offset dane_pliku.cFileName
  cmp BYTE PTR [eax],'.'
  jnz fglak
  cmp BYTE PTR [eax+1],0
  jz nicnierobJ
  cmp BYTE PTR [eax+1],'.'
  jnz fglak
  cmp BYTE PTR [eax+2],0
  jz nicnierobJ
fglak:
  invoke PathRemoveFileSpec,addr bufor
  invoke PathAppend,addr bufor,addr dane_pliku.cFileName
  call DodajPlikiDoProjektu
  invoke PathRemoveFileSpec,addr bufor
  invoke PathRemoveFileSpec,addr bufor
  invoke PathAppend,addr bufor,addr gwiazdka
nicnierobJ:
  jmp nicnierob
kuper:
  invoke lstrcpy,addr BuforNaText,addr bufor
  invoke PathRemoveFileSpec,addr BuforNaText
  invoke PathAppend,addr BuforNaText,addr dane_pliku.cFileName
  
  mov tvitem.imask,TVIF_PARAM
  m2m tvitem.hItem,aktualnyDokumentProjektu
  mSend hPanelProjektyDrzewo,TVM_GETITEM,0,addr tvitem
  mov eax,tvitem.lParam
  cmp (PROJEKT ptr [eax]).ileDokumentow,-1
  jz nicnierob
  invoke DodajDokumentP,addr BuforNaText
nicnierob:
  invoke FindNextFile,szukaj,addr dane_pliku
  or eax,eax
  jnz szukej


  invoke FindClose,szukaj

  ret
DodajPlikiDoProjektu endp
    


i know it's very ugly and written in masm, but i was relatively new to assembler (maybe few months of learning asm) and i didn't know fasm then. i hope it will give you some hints about how to do such procedure yourself.
Post 04 Dec 2006, 12:00
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 04 Dec 2006, 13:57
Quote:

shr esi, 1
jnc .loop
jz .exit

Oh my god...
Post 04 Dec 2006, 13:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.