Jump to content

Right Click Copy File or Folders


Danny35d
 Share

Recommended Posts

This is a scirpt that I use to copy file or folders by just right click into a folder. Right click into a folder select FileCopy - Target after the target folder is selected all you need to do is right click into a file or folder and select FileCopy - Source and the script will copy the file or folder into the target folder.

Note

To test the script you will need to compile and run the EXE. When the script is run the first time it will create the registry key and copy itself into %Windir% folder.

To remove the registry key all you need to do is run the script with the switch /delreg EX: ScriptName.exe /regdel

If $CmdLine[0] <> 0 Then
    Local $SourceKey = ''
    Local $IniFile = @ScriptDir & '\' & StringLeft(@ScriptName, StringInStr(@ScriptName, '.', 0, -1) -1) & '.ini'
    If $CmdLine[0] = 2 Then $SourceKey = StringMid($CmdLine[2], StringInStr($CmdLine[2], '\', 0, -1) + 1)
    Local $Source = IniRead($IniFile, 'Source', $SourceKey, '')
    Local $Target = IniRead($IniFile, 'Target', 'Folder', '')

    For $x = 1 To $CmdLine[0]
        Switch $CmdLine[$x]
            Case '/delreg'
                RegDelete('HKEY_CLASSES_ROOT\*\Shell\ClickCopy - Source')
                RegDelete('HKEY_CLASSES_ROOT\*\Shell\ClickCopy - Target')
                RegDelete('HKEY_CLASSES_ROOT\Folder\Shell\ClickCopy - Source')
                RegDelete('HKEY_CLASSES_ROOT\Folder\Shell\ClickCopy - Target')
                FileDelete($IniFile)
                _SelfDelete()
            Case '/source'
                If $Target = '' Then
                    MsgBox(48, "Target Folder", "You need to select a Target folder before selecting a Source file or folder.")
                    ExitLoop
                EndIf
                If $Source = '' And $CmdLine[2] <> '' Then
                    IniWrite($IniFile, 'Source', $SourceKey, $CmdLine[2])
                    $Source = $CmdLine[2]
                EndIf
                If $Target <> '' And $Source <> '' Then
                    _FileCopy($Source, $Target)
                    IniDelete($IniFile, 'Source', $SourceKey)
                EndIf
            Case '/target'
                If $Target = '' And $CmdLine[2] <> '' Or Not ($Target = $CmdLine[2]) Then
                    IniWrite($IniFile, 'Target', 'Folder', $CmdLine[2])
                EndIf
        EndSwitch
    Next
Else
    If IsAdmin() Then
        RegWrite('HKEY_CLASSES_ROOT\Folder\Shell\ClickCopy - Source\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Source "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\Folder\Shell\ClickCopy - Target\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Target "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\*\Shell\ClickCopy - Source\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Source "' & '%1"')
        RegWrite('HKEY_CLASSES_ROOT\*\Shell\ClickCopy - Target\Command', '', 'REG_SZ', @WindowsDir & '\' & @ScriptName & ' /Target "' & '%1"')
        If Not (@ScriptDir = @WindowsDir & '\' & @ScriptName) Then 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)  
    Local $iMsgBoxAnswer, $iret, $sTitle = 'FileCopy'   
    
    $iFlag = IsFolder($sSource)
    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

Func IsFolder($Path)
   Local $Attribs = FileGetAttrib($Path)
   If StringInStr($Attribs, "D") <> 0 Then Return(1)
   Return(0)    
EndFunc   ;==>IsFolder

Func _SelfDelete($iDelay = 0)
    Local $sCmdFile
    FileDelete(@TempDir & "scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
            & ':loop' & @CRLF _
            & 'del "' & @ScriptFullPath & '"' & @CRLF _
            & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
            & 'del ' & @TempDir & 'scratch.bat'
    FileWrite(@TempDir & "scratch.bat", $sCmdFile)
    Run(@TempDir & "scratch.bat", @TempDir, @SW_HIDE)
EndFunc

Edit:

Fix some typo...

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
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
 Share

  • Recently Browsing   0 members

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