Sign in to follow this  
Followers 0
Suma

script to Copy file to the E:\temp folder

6 posts in this topic

I just want to make a script to Copy file to the E:temp folder.

However the following script doesn't work. I tested the DirCreate command and it is working and creating the folder.

However copyfile doesn't work. It returns 0. How do I catch the runtime error and display it? I tried below method to catch the error but I don't get any msgbox displaying the error.

My code is pasted below:

#include-once
#include <File.au3>
#include <WinAPI.au3>
Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$SRC = "" ;set the variable to nothing, string data
$DST = 0 ; sets it to 0

$SRC = GetExplorerSelection();
;MSgbox(0,"file",GetExplorerSelection())
;if $SRC = "" then exit ;makes sure that there is data in the the variable before continuing
;$DST = "E:temp" ;FileSelectFolder("Please Select Backup DESTINATION","") ;user prompt to select a destination
;MSgbox(0,"The SOURCE folder", $src)
;MSgbox(0,"The Destination folder", $DST,1)
$i = FileChangeDir("E:temp")
MsgBox(0,"",$i & "-" & @WorkingDir)
;#RequireAdmin
$x = FileCopy($SRC,@WorkingDir,1)
MsgBox(0,"",$x & "-" & @WorkingDir & $src)
If @error Then MsgBox(48 + 262144, "COM Error", "@error is set to COM error number." & @CRLF & "@error = " & @error)
$x = DirCreate("E:SAMPLE")
;MyErrFunc($x)
;#RequireAdmin
DirCreate("E:SAMPL")
;#RequireAdmin
FileCopy($SRC,"E:SAMPLpermit.txt",1)
#forceref $oMyError
Func MyErrFunc($oMyError)
    Local $HexNumber
Local $strMsg

$HexNumber = Hex($oMyError.Number, 8)
$strMsg = "Error Number: " & $HexNumber & @CRLF
$strMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF
$strMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF
MsgBox(0, "ERROR", $strMsg)
SetError(1)
Endfunc
; ===============================================================================================================================
Func _WinWaitActivate($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc

Func GetExplorerSelection()
Local $saveClip = ""
$filesFolders = ""
Local $handle = _WinAPI_GetForegroundWindow()
_WinWaitActivate("[CLASS:CabinetWClass]","")
;msgbox(0,"Handle",$handle)
Local $className = _WinAPI_GetClassName($handle)
;msgbox(0,"className",$className)
If $className = "ExploreWClass" Or $className = "CabinetWClass" Then
$saveClip = ClipGet()
Send("^c")
;msgbox(0,"IN",ClipGet())
Sleep(50) ; give clipboard time to react
$filesFolders = ClipGet()
ClipPut($saveClip)
; test if $filesFolders contains @LF and split if so etc..
; code to call StringSplit() etc..
EndIf
Return $filesFolders
EndFunc
 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Firstly the error appears to be that instead of CabinetWClass in the WinAPI_getClassName the msgbox displays Shell_TrayWnd.

Second, even when I  hardcode the src file to "E:orderfile.txt" it does not copy the file. It returns 0.

Posting my code using the code tags:

#include-once
#include <File.au3>
#include <WinAPI.au3>
Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$SRC = "" ;set the variable to nothing, string data
$DST = 0 ; sets it to 0

$SRC = GetExplorerSelection(); FileSelectFolder("Please Select Backup SOURCE","") ;user prompt to select a folder
;MSgbox(0,"file",GetExplorerSelection())
;if $SRC = "" then exit ;makes sure that there is data in the the variable before continuing
;$DST = "E:\temp\" ;FileSelectFolder("Please Select Backup DESTINATION","") ;user prompt to select a destination
MSgbox(0,"The SOURCE folder", $src)
;MSgbox(0,"The Destination folder", $DST,1)
$i = FileChangeDir("E:\temp")
MsgBox(0,"",$i & "-" & @WorkingDir)
;#RequireAdmin
$x = FileCopy($SRC,@WorkingDir,1)
MsgBox(0,"",$x & "-" & @WorkingDir & $src)
If @error Then MsgBox(48 + 262144, "COM Error", "@error is set to COM error number." & @CRLF & "@error = " & @error)
$x = DirCreate("E:\SAMPLE")
;MyErrFunc($x)
;#RequireAdmin
DirCreate("E:\SAMPL")
;#RequireAdmin
FileCopy($SRC,"E:\SAMPL\permit.txt",1)
#forceref $oMyError
Func MyErrFunc($oMyError)
    Local $HexNumber
Local $strMsg

$HexNumber = Hex($oMyError.Number, 8)
$strMsg = "Error Number: " & $HexNumber & @CRLF
$strMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF
$strMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF
MsgBox(0, "ERROR", $strMsg)
SetError(1)
Endfunc
; ===============================================================================================================================
Func _WinWaitActivate($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc

Func GetExplorerSelection()
Local $saveClip = ""
$filesFolders = ""
Local $handle = _WinAPI_GetForegroundWindow()
_WinWaitActivate("[CLASS:CabinetWClass]","")
;msgbox(0,"Handle",$handle)
Local $className = _WinAPI_GetClassName($handle)
msgbox(0,"className",$className)
If $className = "ExploreWClass" Or $className = "CabinetWClass" Then
$saveClip = ClipGet()
Send("^c")
msgbox(0,"IN",ClipGet())
Sleep(50) ; give clipboard time to react
$filesFolders = ClipGet()
ClipPut($saveClip)
; test if $filesFolders contains @LF and split if so etc..
; code to call StringSplit() etc..
EndIf
Return $filesFolders
EndFunc
Edited by Suma

Share this post


Link to post
Share on other sites

That's alot of code there.

I'll take the simplest approach on this.

FileCopy("permit.txt", "E:\SAMPL\permit.txt", 8)

This will copy the file 'permit.txt' (from the same directory the script is at) and copy it to 'E:SAMPLpermit.txt'.

If 'E:SAMPL' doesn't exist then it will be created. It will not overwrite an existing file.

Share this post


Link to post
Share on other sites

I finally got the code working for win 7 explorer. What I need to do is copy selected file from a ziped folder to a temp folder. While my code below is now working for files selected on the explorer. When I open the zipped file directly on the explorer and then run the script it doesn't copy. It is failing in the step send(^c). Nothing is put on the clipboard.  The class of the explorer when I open the zipped folder is still CabinetWClass. Manually pressing ctrl + c does copy it to clipboard.

#include <WinAPI.au3>
$SRC = "" ;set the variable to nothing, string data
$DST = 0 ; sets it to 0

$SRC = GetExplorerSelection()
;MSgbox(0,"file",GetExplorerSelection())
if $SRC = "" then exit 
$DST = "E:\temp\" ;FileSelectFolder("Please Select Backup DESTINATION","") ;user prompt to select a destination
;~if $DST = "" then exit
;MSgbox(0,"The SOURCE folder", $src)
;MSgbox(0,"The Destination folder", $DST)

FileCopy($SRC,"E:\temp\",8)

Func _WinWaitActivate($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc

Func GetExplorerSelection()
Local $saveClip = ""
$filesFolders = ""
Local $handle = _WinAPI_GetForegroundWindow()
_WinWaitActivate("[CLASS:CabinetWClass]","")
;msgbox(0,"Handle",$handle)
Local $className = _WinAPI_GetClassName($handle)
;msgbox(0,"className",$className)
    _WinWaitActivate("[CLASS:CabinetWClass]","")
    ;$saveClip = ClipGet()
Send("^c")
msgbox(0,"Else",ClipGet())
Sleep(50) ; give clipboard time to react
$filesFolders = ClipGet()
Return $filesFolders
EndFunc

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Suma,

Here is a small bit of code that will extract(unzip) *ZIP and *.CAB files.

Dim $cZipFile = "C:\Testing\MyArchive.zip"; you MUST give full path and file name.
Dim $cFolder = "C:\Testing"; you MUST give full path even if it's the current path. And the path MUST already exist.

$oShell = ObjCreate("Shell.Application")
For $ofile In $oShell.NameSpace($cZipFile).items
$oShell.NameSpace($cFolder).copyhere($ofile)
Next

Hope this can help you.

Edited by NewPlaza

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks a lot for the code NewPlaza. I was able to extract the folder from the zip file. But There is that folder inside the zip file. I need to copy the file inside that folder. How do I do that? Your code just copies the whole folder. I tried:

$oShell.NameSpace("E:\temp\").copyhere($ofile &"\Master\file.TXT")

Still copies the whole Master folder and all the content files.

Edited by Suma

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  
Followers 0