一、删除文件夹及其下所有子目录和文件(目录树)
include masm32rt.inc
;SSSSSSSSSSSSSSSSSSSSSSSSSS
.data
;SSSSSSSSSSSSSSSSSSSSSSSSSS
fileop SHFILEOPSTRUCT<>
Dir
db "C:\Users\Alex\AppData\Local\Temp",0
;SSSSSSSSSSSSSSSSSSSSSSSSSS
.code
;SSSSSSSSSSSSSSSSSSSSSSSSSS
start:
mov fileop.wFunc,FO_DELETE
mov fileop.pFrom, offset Dir
mov fileop.fFlags,FOF_NOCONFIRMATION
invoke SHFileOperation, addr fileop
invoke ExitProcess,0
end start
二、从文件说明符中获取文件名
方法一:用Windows API函数——PathFindFileName
原型:PTSTR PathFindFileName( __in PTSTR pPath);
详见: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773589(v=vs.85).aspx
方法二:
;data string1 db "C:\Windows\system32\calc.exe",0 (...) ;proc ExtractFileName proc lpPath: DWORD mov esi, lpPath petla: mov al, [esi] inc esi test al, al jz koniec cmp al, '\' jne petla mov edx, esi jmp petla koniec: ret ExtractFileName endp (...) ;usage (result in edx - pointer to first character of a filename) invoke ExtractFileName, addr string1 ;show the result invoke MessageBox, 0, edx, 0, 0
方法三:
.386 .model flat, stdcall option casemap:none include \masm32\include\windows.inc include \masm32\include\kernel32.inc include \masm32\include\user32.inc includelib \masm32\lib\user32.lib includelib \masm32\lib\kernel32.lib ;SSSSSSSSSSSSSSSSSSSSSSSSSS .data ;SSSSSSSSSSSSSSSSSSSSSSSSSS g_MsgCaption db "取文件名",0 g_szFS1 db "c:\a\a.txt", 0 g_szFS2 db "c:b.txt", 0 g_szFS3 db "c.txt", 0 ;SSSSSSSSSSSSSSSSSSSSSSSSSS .code ;SSSSSSSSSSSSSSSSSSSSSSSSSS ;::::::::::::::::::::::::::::::::::::::::::::::::::::: ; 输入:lpszFileSpec=指向文件说明符字符串的指针(首地址) ; 返回值:eax=0,lpszFileSpec指向的文件说明符字符串为空 ; eax>0, 其值为指向文件名字符串的指针(首地址) ;::::::::::::::::::::::::::::::::::::::::::::::::::::: GetFileName proc uses edi lpszFileSpec: dword mov eax, lpszFileSpec mov edi, eax test eax, eax jz @GetFileNameDone INVOKE lstrlen,eax test eax, eax jz @GetFileNameDone lea ecx,[eax+edi] @GetFileNameNext: cmp ecx, edi jb @GetFileNameSon mov al,[ecx] cmp al, '\' jz @GetFileNameSon cmp al, ':' jz @GetFileNameSon sub ecx, 1 jmp @GetFileNameNext @GetFileNameSon: lea eax, [ecx+1] @GetFileNameDone: ret GetFileName endp start: invoke GetFileName, offset g_szFS1 invoke MessageBox, NULL, eax, offset g_MsgCaption, MB_OK invoke GetFileName, offset g_szFS2 invoke MessageBox, NULL, eax, offset g_MsgCaption, MB_OK invoke GetFileName, offset g_szFS3 invoke MessageBox, NULL, eax, offset g_MsgCaption, MB_OK invoke ExitProcess,NULL end