home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol07 / 06 / dosqa / rdir.asm
Assembly Source File  |  1992-10-01  |  6KB  |  136 lines

  1. ;; MASM code demonstrating how to navigate through all the
  2. ;; subdirectories on an MS-DOS-formatted disk.  This sample
  3. ;; COM program lists every file containing neither hidden
  4. ;; nor system attributes on the current drive.
  5. ;;
  6. ;; To assemble, link, and convert to COM format, type:
  7. ;;      MASM RDIR;
  8. ;;      LINK RDIR;
  9. ;;      EXE2BIN RDIR RDIR.COM
  10.  
  11. FFName          equ     byte ptr [bp+30]        ;File name in DTA
  12. FFAttr          equ     byte ptr [bp+21]        ;File attribute in DTA
  13. IsDirectory     equ     10h                     ;Subdirectory attribute
  14. LocalAlloc      equ     2Ch                     ;Local stack allocation
  15.  
  16. code            segment
  17.                 assume  cs:code,ds:code
  18.                 org     100h
  19. begin:          jmp     Main
  20.  
  21. FileName        db      128 dup (?)
  22. Directory       db      ╥\╙,64 dup (?)
  23. StarDotStar     db      ╥*.*╙,0
  24. UpOneLevel      db      ╥..╙,0
  25. RootDir         db      ╥\╙,0
  26. crlf            db      13,10,╙$╙
  27.  
  28. ;; Main saves the current directory, switches to the root directory,
  29. ;; calls the procedure Searchdir, and then restores the current directory.
  30. Main            proc    near
  31.                 cld                             ;Clear direction flag
  32.                 mov     ah,47h                  ;Save the current directory
  33.                 xor     dl,dl
  34.                 mov     si,offset Directory+1
  35.                 int     21h
  36.  
  37.                 mov     ah,3Bh                  ;Change to the root directory
  38.                 mov     dx,offset RootDir
  39.                 int     21h
  40.  
  41.                 call    SearchDir               ;List all the files
  42.  
  43.                 mov     ah,3Bh                  ;Restore the directory that
  44.                 mov     dx,offset Directory     ;was current when we started
  45.                 int     21h
  46.  
  47.                 mov     ax,4C00h                ;Exit
  48.                 int     21h
  49. Main            endp
  50.  
  51. ;; SearchDir lists all the files in the current directory, then calls
  52. ;; itself recursively to list files in descendant directories.
  53.  
  54. SearchDir       proc    near
  55.                 push    bp                      ;Save BP
  56.                 mov     ah,2Fh                  ;Get DTA address
  57.                 int     21h
  58.                 push    bx                      ;Save it on the stack
  59.                 push    es
  60.                 sub     sp,LocalAlloc           ;Allocate stack space
  61.                 mov     bp,sp                   ;Stack pointer in BP
  62.                 mov     ah,1Ah                  ;Change DTA to location
  63.                 mov     dx,bp                   ;on the stack
  64.                 int     21h
  65. ; List all the files in the current directory.
  66.                 mov     ah,4Eh                  ;Find first file
  67.                 xor     cx,cx
  68.                 mov     dx,offset StarDotStar
  69.                 int     21h
  70.                 jc      FindFirstDir            ;Branch if none found
  71.  
  72. ListFile:       mov     ah,60h                  ;Add the drive and path to
  73.                 mov     si,bp                   ;the file name by calling
  74.                 add     si,30                   ;undocumented MS-DOS
  75.                 mov     di,offset FileName      ;function 60h
  76.                 int     21h
  77.  
  78.                 xor     al,al                   ;Convert the ASCIIZ file
  79.                 mov     cx,128                  ;name to one delimited by
  80.                 mov     di,offset FileName      ;a dollar sign
  81.                 repne   scasb
  82.                 mov     byte ptr [di-1],╙$╙
  83.  
  84.                 mov     ah,09h                  ;Display the file name
  85.                 mov     dx,offset FileName      ;by calling MS-DOS
  86.                 int     21h                     ;function 09h
  87.  
  88.                 mov     ah,09h                  ;Move the cursor to the
  89.                 mov     dx,offset crlf          ;next line
  90.                 int     21h
  91.  
  92.                 mov     ah,4Fh                  ;Find next file and display
  93.                 int     21h                     ;the file name if the search
  94.                 jnc     ListFile                ;was successful
  95. ; Now search for descendants of the current directory.  If found, change
  96. ; to each subdirectory in turn and call SearchDir recursively.
  97. FindFirstDir:   mov     ah,4Eh                  ;Find first subdirectory
  98.                 mov     cx,17h
  99.                 mov     dx,offset StarDotStar
  100.                 int     21h
  101.                 jc      SearchDirExit           ;Branch if none found
  102.  
  103. TestEntry:      cmp     FFName,╙.╙              ;Skip ╥.╙ and ..╙ entries
  104.                 je      FindNextDir
  105.                 test    FFAttr,IsDirectory      ;Branch if it╒s not a
  106.                 jz      FindNextDir             ;subdirectory name
  107.  
  108.                 mov     ah,3Bh                  ;Change to the directory
  109.                 mov     dx,bp                   ;whose name was just
  110.                 add     dx,30                   ;returned
  111.                 int     21h
  112.  
  113.                 call    SearchDir               ;List the files in it
  114.  
  115.                 mov     ah,3Bh                  ;Return to the parent
  116.                 mov     dx,offset UpOneLevel    ;directory
  117.                 int     21h
  118.  
  119. FindNextDir:    mov     ah,4Fh                  ;Find next subdirectory
  120.                 int     21h
  121.                 jnc     TestEntry
  122. ; Clean up the stack, restore the DTA address, and exit.
  123. SearchDirExit:  add     sp,LocalAlloc           ;Deallocate stack space
  124.                 mov     bx,ds                   ;Save DS in BX
  125.                 mov     ah,1Ah                  ;Restore the DTA address
  126.                 pop     ds
  127.                 pop     dx
  128.                 int     21h
  129.                 mov     ds,bx                   ;Restore DS from BX
  130.                 pop     bp                      ;Restore BP
  131.                 ret
  132. SearchDir       endp
  133.  
  134. code            ends
  135.                 end     begin
  136.