News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to get all .exe files name of a given drive ??

Started by maruf10, March 30, 2010, 11:30:22 AM

Previous topic - Next topic

maruf10

Is there any way to get the parent folder name of each file ??

hutch--

maruf10,

Tell us what you are going to do with this code. A recursive algo using the FindFirst/FindNext pair can do anything you want but it can also be used for mischief like deleting whole drives. You already have more than enough info to write this code as you have supplied C source code, what is the problem and what are you going to do with it ?
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

maruf10

#32
A mischief has already been done ...
all files (6 GB) of my PENDRIVE has already been deleted ...

I want to detect those .exe files which name is same as their parent folder like C:\TC\TC.exe

At what portion of the recursive procedure should i check to get the parent folder name of all files ??
I have tried but failed ....
It shows another folder name as a parent folder of each file ...



i have got a simpler way ... I can get the parent folder for a file from its full path .
In that case i would like to know the technique of string traversing in masm32 ...
how can i do this ??

qWord

here an unicode extended version of clive's code:
include masm32rt.inc
include \masm32\macros\ucmacros.asm

DeleteFilesW PROTO pwszPath:PWCHAR,pwszTarget:PWCHAR

WIN32_FIND_DATAW STRUCT
  dwFileAttributes DWORD    ?
  ftCreationTime FILETIME <>
  ftLastAccessTime FILETIME <>
  ftLastWriteTime FILETIME <>
  nFileSizeHigh DWORD    ?
  nFileSizeLow DWORD    ?
  dwReserved0 DWORD    ?
  dwReserved1 DWORD    ?
  cFileName WORD MAX_PATH dup(?)
  cAlternate WORD 14 dup(?)
WIN32_FIND_DATAW ENDS

rv MACRO FuncName:REQ,args:VARARG
  __arg equ <invoke FuncName>
  FOR var,<args>
__arg CATSTR __arg,<,reparg(var)>
  ENDM
  __arg
  EXITM <eax>
ENDM

.code
start:
invoke DeleteFilesW,uni$("C:\windows\"),uni$("windows.exe")
inkey
invoke ExitProcess, 0

DeleteFilesW proc pwszPath:PWCHAR,pwszTarget:PWCHAR
LOCAL w32FindDataW:WIN32_FIND_DATAW
LOCAL wszSubDir[512]:WORD
LOCAL hFind:HANDLE

mov edx,pwszPath
assume edx: ptr WORD
.if [edx]!='\' || [edx+2]!='\' || [edx+4]!='?' || [edx+6]!='\'
ucopy$ uni$("\\?\"),ADDR wszSubDir
invoke ucCatStr,ADDR wszSubDir,pwszPath
.else
ucopy$ edx,ADDR wszSubDir
.endif
assume edx: nothing

mov edx,rv(ucMultiCat,1,ADDR wszSubDir,uni$("*.*"))
mov hFind,rv(FindFirstFileW,edx,ADDR w32FindDataW)
.if eax == INVALID_HANDLE_VALUE
ret
.endif
.repeat
.if w32FindDataW.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
.repeat
.break .if !rv(lstrcmpiW,ADDR w32FindDataW.cFileName,uni$("."))
.break .if !rv(lstrcmpiW,ADDR w32FindDataW.cFileName,uni$(".."))
ucopy$ pwszPath,ADDR wszSubDir
invoke ucMultiCat,2,ADDR wszSubDir,ADDR w32FindDataW.cFileName,uni$("\")
invoke DeleteFilesW,ADDR wszSubDir,uadd$(ADDR w32FindDataW.cFileName,uni$(".exe"))
.until
.else
.if !rv(lstrcmpiW,ADDR w32FindDataW.cFileName,pwszTarget)
ucopy$ pwszPath,ADDR wszSubDir
invoke ucMultiCat,1,ADDR wszSubDir,ADDR w32FindDataW.cFileName
; !!!!
; invoke DeleteFileW,ADDR wszSubDir
; !!!!
invoke crt_wprintf,uni$("%s%S"),ADDR wszSubDir,chr$(10,13)
.endif
.endif
.until !rv(FindNextFileW,hFind,ADDR w32FindDataW)
invoke FindClose,hFind
ret
DeleteFilesW endp
end start
FPU in a trice: SmplMath
It's that simple!

clive

My prototype for listing files in the form \xxx\foo\foo.exe is provided below. I will note that such file names are VERY common for legal and harmless programs, I listed about 50 in my TEMP directory for various compilers, assemblers, Compaq SoftPaq's, etc.
-Clive

#include <windows.h>

#include <stdio.h>
#include <stdlib.h>

char Path[MAX_PATH];  //maximum length of full path

void Find(char *EndPath, char *Matcher) // End of Path passed down
{
  HANDLE hFind; // Local context, required unique for each descent

  WIN32_FIND_DATA FindData;   // describes a file by FindFirstFile,FindNextFile
                              //  declares a structure named FindData

  strcpy(EndPath, "*.*"); // Build wildcard version for search

  hFind = FindFirstFile(Path, &FindData);

  if (hFind == INVALID_HANDLE_VALUE)
    return;

  do
  {
    if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //Specifies the file attributes of the file found
    {                                                         //identify a directory
                                                              //FILE_ATTRIBUTE_DIRECTORY is a member of
                                                              //FindData.dwFileAttributes

      if ((FindData.cFileName[0] != '.') ||           // Cheap test for most cases
          ((strcmp(FindData.cFileName, ".") != 0) &&  // More expensive test for . and .. directories
           (strcmp(FindData.cFileName, "..") != 0)))
      {
        strcpy(EndPath, FindData.cFileName); // append current directory to the path
        strcat(EndPath, "\\");

        strcat(FindData.cFileName,".exe"); // make a matching pattern, using cFileName as a convenient buffer

        Find(EndPath + strlen(EndPath), FindData.cFileName); // Recurse, passing new end of path to optimize concatenation
      }
    }
    else
    {
      // Use a case insensitive compare to produce a list of files to delete

      if (stricmp(FindData.cFileName, Matcher) == 0) // File foo.exe if subdirectory foo
      {
        strcpy(EndPath, FindData.cFileName);   // append current file to path

        puts(Path); // Print the file name, with the whole path
      }
    }
  }
  while(FindNextFile(hFind, &FindData));

  FindClose(hFind);
}

int main(int argc, char **argv)
{
  strcpy(Path, "C:\\TEMP\\");

  Find(Path + strlen(Path), "TEMP.EXE");
}
It could be a random act of randomness. Those happen a lot as well.