Jump to content
Sign in to follow this  
nekokk

How to get path and name of a file I right-clicked on?

Recommended Posts

nekokk

How to get path and name of a file I right-clicked on?

I would like to do something to a file I right-clicked on , I hope the script can know its target is the file be chosen without a given path and name .

e.g

$Path_Name = (a way I need to get the path and name from a file I right-clicked on)

FileCopy($path,"The destination path of the copied file")

So when I want to copy a file , I just need choose it and run my script for copy, of course I will apply it to another way not only for copy, thanks in advance :whistle:

This problem badger me for a long time...

Share this post


Link to post
Share on other sites
BigDod

Why not use FileOpenDialog instead of right clicking.



Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites
nekokk

Why not use FileOpenDialog instead of right clicking.

Because I hope something I want to do can work directly on a file be clicked, no need to give a path or name, only one condition is requirement and the condition is what I chosen, what I clicked on.

thank you for your reply. :whistle:

Share this post


Link to post
Share on other sites
Danny35d

How to get path and name of a file I right-clicked on?

To get the path of a file or directory by right-click you will need to modify the following reg keys:

HKEY_CLASSES_ROOT\*\Shell\Your Script Name\Command

HKEY_CLASSES_ROOT\Folder\Shell\Your Script Name\Command

This is a script I use to copy user data from the old computer into the new computer. All I need to do is right click in a folder and select FileCopy - Target, then I just right click into a file or folder that I want to copy and select FileCopy - Source. The script will start copying the select file or folder into the Target folder.

Local $IniFile = @ScriptDir & '\' & StringLeft(@ScriptName, StringInStr(@ScriptName, '.', 0, -1) -1) & '.ini'
Local $DirSource = IniRead($IniFile, 'FileCopy', 'DirSource', '')
Local $FileSource = IniRead($IniFile, 'FileCopy', 'FileSource', '')
Local $Target = IniRead($IniFile, 'FileCopy', 'Target', '')

If $CmdLine[0] <> 0 Then
    For $x = 1 To $CmdLine[0]
        Switch $CmdLine[$x]
            Case '/delreg'
                RegDelete('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Source')
                RegDelete('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Target')
                RegDelete('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Source')
                RegDelete('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Target')               
            Case '/dirsource'
                If $DirSource = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'FileCopy', 'DirSource', $CmdLine[2])
                    $DirSource = $CmdLine[2]
                EndIf
                If $Target <> '' And $DirSource <> '' Then
                    _FileCopy($DirSource, $Target)
                    IniWrite($IniFile, 'FileCopy', 'DirSource', '')
                EndIf
            Case '/filesource'              
                If $FileSource = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'FileCopy', 'FileSource', $CmdLine[2])
                    $FileSource = $CmdLine[2]
                EndIf
                If $Target <> '' And $FileSource <> '' Then
                    _FileCopy($FileSource, $Target, 0)
                    IniWrite($IniFile, 'FileCopy', 'FileSource', '')
                EndIf
            Case '/target'
                If $Target = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'FileCopy', 'Target', $CmdLine[2])
                    $Target = $CmdLine[2]
                EndIf
                If $Target <> '' And $DirSource <> '' Then
                    _FileCopy($DirSource, $Target)
                    IniWrite($IniFile, 'FileCopy', 'DirSource', '')
                ElseIf $Target <> '' And $FileSource <> '' Then
                    _FileCopy($FileSource, $Target, 0)
                    IniWrite($IniFile, 'FileCopy', 'FileSource', '')
                EndIf               
        EndSwitch
    Next
Else
    If IsAdmin() Then
        RegWrite('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Source\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /DirSource "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Target\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Target "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Source\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /FileSource "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Target\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Target "' & '%1"')
        FileCopy(@AutoItExe, @WindowsDir & '\' & @ScriptName, 1)
    EndIf
EndIf
Exit

; Flag = 0     Copy file
; Flag = 1     Copy directory

; Return 1     if copy file or copy directory is success
; Return 0     if copy file or copy directory is failure
; Return -1    if answer message NO
Func _FileCopy($sSource, $sTarget, $iFlag = 1)  
    Local $iMsgBoxAnswer, $iret, $sTitle = 'FileCopy'   
    If $iFlag then $sTitle = 'DirCopy'
    $iMsgBoxAnswer = MsgBox(68, $sTitle, "Are you sure, you want to copy?" & @CRLF & @CRLF & "Source: " & $sSource & @CRLF & "Target: " & $sTarget)
    If $iMsgBoxAnswer = 7 Then Return(-1)
    
    If $iFlag Then
        Local $sDirFolder = StringMid($sSource, StringInStr($sSource, '\', 0, -1) + 1)
        ToolTip('DirCopy ' & $sSource & ' To ' & $sTarget & '\' & $sDirFolder, 0, 0, '', 1)
        $iret = DirCopy($sSource, $sTarget & '\' & $sDirFolder, 1)      
    Else
        ToolTip('FileCopy ' & $sSource & ' To ' & $sTarget, 0, 0, '', 1)
        $iret = FileCopy($sSource, $sTarget & '\', 9)
    EndIf
    ToolTip('')
    If Not $iret Then
        If $iFlag Then
            MsgBox(16, $sTitle, "Copying directory " & $sSource & " To " & $sTarget & " fail.")
        Else
            MsgBox(16, $sTitle, "Copying file " & $sSource & " To " & $sTarget & " fail.")
        EndIf
    EndIf
    Return($iret)
EndFunc

Note:

To test this script you will need to compile first. When you run the script the first time I will modify the registry and copy itself into %Windir% folder after this is done all you need to do is right-click.

To remove the registry key run the script with /delreg switch Ex: ScriptName.exe /delreg

I hope this help you....


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites
nekokk

To get the path of a file or directory by right-click you will need to modify the following reg keys:

HKEY_CLASSES_ROOT\*\Shell\Your Script Name\Command

HKEY_CLASSES_ROOT\Folder\Shell\Your Script Name\Command

This is a script I use to copy user data from the old computer into the new computer. All I need to do is right click in a folder and select FileCopy - Target, then I just right click into a file or folder that I want to copy and select FileCopy - Source. The script will start copying the select file or folder into the Target folder.

Local $IniFile = @ScriptDir & '\' & StringLeft(@ScriptName, StringInStr(@ScriptName, '.', 0, -1) -1) & '.ini'
Local $DirSource = IniRead($IniFile, 'FileCopy', 'DirSource', '')
Local $FileSource = IniRead($IniFile, 'FileCopy', 'FileSource', '')
Local $Target = IniRead($IniFile, 'FileCopy', 'Target', '')

If $CmdLine[0] <> 0 Then
    For $x = 1 To $CmdLine[0]
        Switch $CmdLine[$x]
            Case '/delreg'
                RegDelete('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Source')
                RegDelete('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Target')
                RegDelete('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Source')
                RegDelete('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Target')               
            Case '/dirsource'
                If $DirSource = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'FileCopy', 'DirSource', $CmdLine[2])
                    $DirSource = $CmdLine[2]
                EndIf
                If $Target <> '' And $DirSource <> '' Then
                    _FileCopy($DirSource, $Target)
                    IniWrite($IniFile, 'FileCopy', 'DirSource', '')
                EndIf
            Case '/filesource'              
                If $FileSource = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'FileCopy', 'FileSource', $CmdLine[2])
                    $FileSource = $CmdLine[2]
                EndIf
                If $Target <> '' And $FileSource <> '' Then
                    _FileCopy($FileSource, $Target, 0)
                    IniWrite($IniFile, 'FileCopy', 'FileSource', '')
                EndIf
            Case '/target'
                If $Target = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'FileCopy', 'Target', $CmdLine[2])
                    $Target = $CmdLine[2]
                EndIf
                If $Target <> '' And $DirSource <> '' Then
                    _FileCopy($DirSource, $Target)
                    IniWrite($IniFile, 'FileCopy', 'DirSource', '')
                ElseIf $Target <> '' And $FileSource <> '' Then
                    _FileCopy($FileSource, $Target, 0)
                    IniWrite($IniFile, 'FileCopy', 'FileSource', '')
                EndIf               
        EndSwitch
    Next
Else
    If IsAdmin() Then
        RegWrite('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Source\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /DirSource "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\Folder\Shell\FileCopy - Target\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Target "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Source\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /FileSource "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\*\Shell\FileCopy - Target\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Target "' & '%1"')
        FileCopy(@AutoItExe, @WindowsDir & '\' & @ScriptName, 1)
    EndIf
EndIf
Exit

; Flag = 0     Copy file
; Flag = 1     Copy directory

; Return 1     if copy file or copy directory is success
; Return 0     if copy file or copy directory is failure
; Return -1    if answer message NO
Func _FileCopy($sSource, $sTarget, $iFlag = 1)  
    Local $iMsgBoxAnswer, $iret, $sTitle = 'FileCopy'   
    If $iFlag then $sTitle = 'DirCopy'
    $iMsgBoxAnswer = MsgBox(68, $sTitle, "Are you sure, you want to copy?" & @CRLF & @CRLF & "Source: " & $sSource & @CRLF & "Target: " & $sTarget)
    If $iMsgBoxAnswer = 7 Then Return(-1)
    
    If $iFlag Then
        Local $sDirFolder = StringMid($sSource, StringInStr($sSource, '\', 0, -1) + 1)
        ToolTip('DirCopy ' & $sSource & ' To ' & $sTarget & '\' & $sDirFolder, 0, 0, '', 1)
        $iret = DirCopy($sSource, $sTarget & '\' & $sDirFolder, 1)      
    Else
        ToolTip('FileCopy ' & $sSource & ' To ' & $sTarget, 0, 0, '', 1)
        $iret = FileCopy($sSource, $sTarget & '\', 9)
    EndIf
    ToolTip('')
    If Not $iret Then
        If $iFlag Then
            MsgBox(16, $sTitle, "Copying directory " & $sSource & " To " & $sTarget & " fail.")
        Else
            MsgBox(16, $sTitle, "Copying file " & $sSource & " To " & $sTarget & " fail.")
        EndIf
    EndIf
    Return($iret)
EndFunc

Note:

To test this script you will need to compile first. When you run the script the first time I will modify the registry and copy itself into %Windir% folder after this is done all you need to do is right-click.

To remove the registry key run the script with /delreg switch Ex: ScriptName.exe /delreg

I hope this help you....

Thank you very much :whistle: this is helpful!

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×