Jump to content

Search USB for EXE files


Recommended Posts

Hi Guy's,

I'm new to AutoIt and im a bit lost...

Im trying to write a script to search a USB-pen (who's drive letter is unknown) for all .EXE files and then close any that are open.

The way I see it I the script has 3 parts,

Part 1: Search for correct USB pen

Part 2: Search for all .EXE located on the USB pen

Part 3: Close any .EXE from the USB pen that may be in use.

So far I have written a script to scan for the USB pen but now I'm stuck, I not sure how to write the search function for Part 2!

Also I know for Part 3 I need to use some thing like:

If ProcessExists("$Process") Then
    ProcessClose("$Process")
EndIf

but I'm not to sure how to tie it in with the resukts from Part 2.

This is what i have so far

;Function to locate USB pen and search for all .exe files and close any that are open.

;Part 1 - locate all drives 
$var = DriveGetDrive( "ALL" )

If NOT @error Then
    For $i = 1 to $var[0]
        $OS = DriveGetFileSystem($var[$i])

;Find drive that has been "Fat" formatted (USB pen fomatting)                       
            if $OS="FAT" then
                FileChangeDir($var[$i])
                
    ;Search possible USB pen to see if it has a known file on it, to confirm if it is the correct drive
                $search = FileFindFirstFile("ZtechUSB.exe")  

    ; Check if the search was successful
                If $search = -1 Then
                    MsgBox(0, "Error", "No USB Driver found with ZtechUSB on the root")
                Exit
                else 
                EndIf


    ; Close the search handle
                FileClose($search)
                
    ;This is temporary in place so that I know that the scrip has picked up the correct script 
                MsgBox(4096,"Drive " & $i, $var[$i] & $OS
            endif
    Next
    
EndIf

;Part 2 - Search for all .EXE 's on USB pen

;Part 3 - Close all USB pen .EXE 's that are open

Any help you guys & gals could give, would be great! as I really would like to finish this script an release it to the forum:D

Edited by zerbo
Link to comment
Share on other sites

Welcome to the forums.

Hard drives might be FAT formatted and thumbdrives might be NTFS, so that's not a definitive method of finding USB drives.

You could try DriveGetDrive("Removable") and skip A: and B:.

FileFindFirstFile isn't recursive, so you'll have to loop through directories or parse the output of a dos dir command. There are plenty of examples in this forum.

Be careful when it comes time to end the processes, as the processes might be running from another source. There's a way to get this info from WMI... will send the link when I find it again.

Using that method, you won't need part 2. :D

Good luck!

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

;Part 2 - Search for all .EXE 's on USB pen

Here's what Scriptomatic tells me about my usb drives (one external hard disk, one sd, one compact flash). This should be enough to tell which one you want:

Access:

Availability:

BlockSize:

Caption: F:

Compressed: False

ConfigManagerErrorCode:

ConfigManagerUserConfig:

CreationClassName: Win32_LogicalDisk

Description: Removable Disk

DeviceID: F:

DriveType: 2

ErrorCleared:

ErrorDescription:

ErrorMethodology:

FileSystem: FAT

FreeSpace: 990527488

LastErrorCode:

MaximumComponentLength: 255

MediaType:

Name: F:

NumberOfBlocks:

PNPDeviceID:

PowerManagementCapabilities:

PowerManagementSupported:

ProviderName:

Purpose:

QuotasDisabled:

QuotasIncomplete:

QuotasRebuilding:

Size: 990576640

Status:

StatusInfo:

SupportsDiskQuotas: False

SupportsFileBasedCompression: False

SystemCreationClassName: Win32_ComputerSystem

SystemName: JEFF-DELL

VolumeDirty: False

VolumeName: NIKON D50

VolumeSerialNumber: 6A19A302

Access:

Availability:

BlockSize:

Caption: G:

Compressed: False

ConfigManagerErrorCode:

ConfigManagerUserConfig:

CreationClassName: Win32_LogicalDisk

Description: Local Fixed Disk

DeviceID: G:

DriveType: 3

ErrorCleared:

ErrorDescription:

ErrorMethodology:

FileSystem: FAT32

FreeSpace: 82589614080

LastErrorCode:

MaximumComponentLength: 255

MediaType: 12

Name: G:

NumberOfBlocks:

PNPDeviceID:

PowerManagementCapabilities:

PowerManagementSupported:

ProviderName:

Purpose:

QuotasDisabled:

QuotasIncomplete:

QuotasRebuilding:

Size: 120002150400

Status:

StatusInfo:

SupportsDiskQuotas: False

SupportsFileBasedCompression: False

SystemCreationClassName: Win32_ComputerSystem

SystemName: JEFF-DELL

VolumeDirty: False

VolumeName: WD USB 2

VolumeSerialNumber: 0D192DFC

Access:

Availability:

BlockSize:

Caption: H:

Compressed: False

ConfigManagerErrorCode:

ConfigManagerUserConfig:

CreationClassName: Win32_LogicalDisk

Description: Removable Disk

DeviceID: H:

DriveType: 2

ErrorCleared:

ErrorDescription:

ErrorMethodology:

FileSystem: FAT

FreeSpace: 104730624

LastErrorCode:

MaximumComponentLength: 255

MediaType:

Name: H:

NumberOfBlocks:

PNPDeviceID:

PowerManagementCapabilities:

PowerManagementSupported:

ProviderName:

Purpose:

QuotasDisabled:

QuotasIncomplete:

QuotasRebuilding:

Size: 128557056

Status:

StatusInfo:

SupportsDiskQuotas: False

SupportsFileBasedCompression: False

SystemCreationClassName: Win32_ComputerSystem

SystemName: JEFF-DELL

VolumeDirty: True

VolumeName: LEXAR MEDIA

VolumeSerialNumber: 19D31D2F

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hi Skruge,

Its true about some hard drives might be "FAT" formated thats why i.v put a ssearch to look at the root of the drive for a file I know will be on it.

The WMI thing sound great.

Also I have come accross this script that might do for Part 2

#include <GUIConstants.au3>
#include <Array.au3>

$a = _FileSearch("*.exe",1)
If $a[0] > 0 Then
    _ArraySort ($a)
EndIf
$but_height = 20
$but_width = 125
$button_in_a_row = 20

$tool_label_left = 10
$rows = ""
get_rows()

Func get_rows()
    $rows = $a[0] / $button_in_a_row
    $rows_check = StringSplit ($rows, ".")
    If $rows_check[0] > 0 Then
        If $rows  > $rows_check[1] Then $rows = $rows_check[1] + 1
    EndIf
EndFunc

If $rows > 6 Then $button_in_a_row = $button_in_a_row * 1.5

get_rows()

$width_replicator = $a[0]/10

$row_height = $button_in_a_row
If $a[0] < $button_in_a_row Then $row_height = $a[0]

Dim $go[1]

If $rows < 1 Then
    MsgBox (0, "Starter", "no programs found...")
    Exit
EndIf

$label_count = 1
GUICreate ("Starter", 20 + ($but_width) * $rows, $but_height * ($row_height + 2))
    GUICtrlCreateLabel ("choose your favorite tool...", $tool_label_left, 10, 20 + (125 * $width_replicator), 20)
    $label_count = $label_count + 1

    Opt ("GuiCoordMode", 0)
    $j = $button_in_a_row + 1
MsgBox (0, "Stage 1", "no programs found...1")
    For $i = 1 To $a[0] Step 1
        _ArrayAdd ($go, "")
        $new_title = StringSplit ($a[$i], "\")
        $title = StringLeft ($new_title[$new_title[0]], 50)
        $title = StringReplace ($title, ".exe", "")
        $title = StringReplace ($title, "_", " ")
        $title = StringUpper (StringLeft ($title, 1)) & StringTrimLeft ($title, 1)
        $title = StringLeft ($title, 16)
        
MsgBox (0, "Stage 2", "no programs found...2")
        If $i = $j Then
            $tool_label_left = $tool_label_left + $but_width 
            Opt ("GuiCoordMode", 1)
            GUICtrlCreateLabel ("", $tool_label_left, 10, 145, 20)
            $label_count = $label_count + 1
            Opt ("GuiCoordMode", 0)
            $j = $j + $button_in_a_row
        EndIf
        $go[$i] = GUICtrlCreateButton ("...", 0, $but_height, 25, $but_height, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_ICON))
        GUICtrlSetImage ($go[$i], $a[$i], "", 0)
        GUICtrlCreateLabel ($title, 30, 5)
        GUICtrlCreateLabel ("", -30, -5, 1, 1)
            GuiCtrlSetTip (-1, $a[$i])
    Next

GuiSetState()
MsgBox (0, "Stage 3", "no programs found...3")
While 1
    $msg = GUIGetMsg()
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    For $i = 1 to $a[0] Step 1
        If $msg = $go[$i] Then
            $pos_to_scriptdir = StringReplace ($a[$i], @ScriptDir, "")

            $splitted = StringSplit ($pos_to_scriptdir, "\")
            $work_dir = @ScriptDir
            For $j = 1 to $splitted[0] - 1 Step 1
                $work_dir = $work_dir & "\" & $splitted[$j]
                $work_dir = StringReplace ($work_dir, "\\", "\")
            Next

            Run (@ScriptDir & $pos_to_scriptdir, $work_dir & "\")

        EndIf
    Next

Wend
;--------------------------------------------

Func _FileSearch($szMask,$nOption)
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]
MsgBox (0, "Stage 4", "no programs found...4")
    If Not StringInStr($szMask,"\") Then
         $szRoot = @SCRIPTDIR & "\"
    Else
         While StringInStr($szMask,"\")
              $szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))
              $szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))
         Wend
    EndIf
    If $nOption = 0 Then
         _FileSearchUtil($szRoot, $szMask, $szReturn)
    Else
         While 1
              $hFile = FileFindFirstFile($szRoot & "*.*")
              If $hFile >= 0 Then
                   $szBuffer = FileFindNextFile($hFile)
                   While Not @ERROR
                        If $szBuffer <> "." And $szBuffer <> ".." And _
                             StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _
                             $szPathList = $szPathList & $szRoot & $szBuffer & "*"
                        $szBuffer = FileFindNextFile($hFile)
                   Wend
                   FileClose($hFile)
              EndIf
              _FileSearchUtil($szRoot, $szMask, $szReturn)
              If $szPathList == "*" Then ExitLoop
              $szPathList = StringTrimLeft($szPathList,1)
              $szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"
              $szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)
         Wend
    EndIf
    If $szReturn = "" Then
         $aNULL[0] = 0
         Return $aNULL
    Else
         Return StringSplit(StringTrimRight($szReturn,1),"*")
    EndIf
EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)
MsgBox (0, "Stage 5", "no programs found...5")
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
         $szBuffer = FileFindNextFile($hFile)
         While Not @ERROR
              If $szBuffer <> "." And $szBuffer <> ".." Then _
                   $RETURN = $RETURN & $ROOT & $szBuffer & "*"
              $szBuffer = FileFindNextFile($hFile)
         Wend
         FileClose($hFile)
    EndIf
EndFunc

Put could some one stripe out the gui part and any non needed info as currently i still dont under full stand the all the syntax.

Link to comment
Share on other sites

IF this program is going to be used for you, and only you.. then just DriveGetDrive Removable.. then search for the specific drive serial.

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

  • 9 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...