AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multiple Copy of a file

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
kardus



Joined: 17 Jul 2008
Posts: 9

PostPosted: Thu Jul 17, 2008 7:30 pm    Post subject: Multiple Copy of a file Reply with quote

When I need some copy of a file, I am tired to get: Copy of file.JPG, Copy (2) of file.JPG, etc ... (May be on Mac it is different?)
Any contribution it is welcome.

Code:
/*
This script to duplicate files.
When you need copies of a file, normally you press CTRL+C and CTRL+V several time.
Suppose you have "file.JPG", you'll get:
Copy of file.JPG
Copy (2) of file.JPG
Copy (3) of file.JPG
...
Copy (10) of file.JPG
Copy (11) of file.JPG
I think sometimes it is useless, due to lack of alpabetical order;
also, you can't specify how many copy you need in a single operation.
With the hotkey "d" below (demo only, change to an unused key according to your keyboard layout) you start the duplicate operation,
but only if you are in Windows Explorer or Xplorer˛ (I use it, see www.zabkat.com), and only if a file is selected;
of course, you can add Total Commander or similar.
You get, in a single operation:
file-Copy 001.JPG
file-Copy 002.JPG
file-Copy 003.JPG
...
file-Copy 010.JPG
file-Copy 011.JPG
*/


#SingleInstance ignore                                ; not really necessary, script with Gui are single-instance by default
Menu, Tray, Icon, %A_WinDir%\system32\Shell32.dll, 42 ; tree icon (on XP SP2)
CoordMode ToolTip



; "$" to prevent an infinite loop (since the hotkey may "sends itself" in the IF statement below)
$d::  ; this is the hotkey to start the action. CHANGE LETTER "d" TO YOUR TASTE



; only if you are in Xplorer˛ or Windows Explorer it start the duplicate operation (add any File Manager)
; Windows Explorer: ahk_class CabinetWClass ahk_class ExploreWClass
if not (WinActive("xplorer˛ -") or WinActive("ahk_class CabinetWClass") or WinActive("ahk_class ExploreWClass"))
 {
  send d  ; CHANGE LETTER "d" TO YOUR TASTE
  return
 }



send ^c      ; copy the file to duplicate
sleep 100    ; this appear to be necessary on my system



; the IF statement below to be sure you have copied a file, not a directory or some text
if not FileExist(clipboard)
 {
  ;msgbox ,,, INVALID clipboard `n(it is not a file), 2  ; test only
  send d  ; CHANGE LETTER "d" TO YOUR TASTE
  return
 }
if InStr(FileExist(clipboard), "D")
 {
  ;msgbox ,,, INVALID clipboard `n(it is a directory), 2  ; test only
  send d  ; CHANGE LETTER "d" TO YOUR TASTE
  return
 }



SplitPath clipboard, name
ToolTip DUPLICATING:`n%name%, (A_ScreenWidth/2.4), (A_ScreenHeight/3.2), 3
gui +AlwaysOnTop -MinimizeBox
gui font, s10, Arial
Gui, Add, Text,, How many Copy?`n (max.100)
Gui, Add, Edit, vMyEdit w60 h30 -VScroll Center Limit -Multi Number -WantReturn
Gui, Add, UpDown, vMyUpDown Range1-100, 1
Gui, Add, Button, Default xp+80, OK
Gui, Add, Button,, Cancel
Gui, Show
return



ButtonOK:  ; _____________________________________ OK _____________________________________________________
Gui +OwnDialogs
Gui Submit, NoHide

; again the IF statement to be sure you still have a file in the clipboard (just in case you have copied something else when GUI was active)
if not FileExist(clipboard)
 {
  msgbox ,,, INVALID clipboard `n(it is not a file), 2
  gosub ButtonCancel
  return
 }
if InStr(FileExist(clipboard), "D")
 {
  msgbox ,,, INVALID clipboard `n(it is a directory), 2
  gosub ButtonCancel
  return
 }

; this is to check the number of the copy selected
if MyEdit <> %MyUpDown%
 {
  msgbox ,,, INVALID number of Copy (%MyEdit%), 1
  return
 }
;msgbox ,,, you selected %MyUpDown% Copies, 1

; here we set a max size of the whole operation
FileGetSize size, %clipboard% ;the option M does'nt get decimal place
size := size / 1048576        ;to transform bytes in megabytes (1024x1024)
;msgbox ,,, size of the file is (%size%) MegaBytes, 5
if size * MyUpDown > 1024     ;(safety) stop if the output is bigger than 1 Giga
 {
  msgbox ,,, INVALID (copies size more than 1 Giga), 2
  return
 }

Gui Cancel    ; from here we start the copy operation
BlockInput on
ToolTip `n wait... COPY in progress `n`n (BlockInput)`n, (A_ScreenWidth/3), (A_ScreenHeight/3), 3
SplitPath clipboard, name, dir, ext, name_no_ext
number =
loop %MyUpDown%
 {
  number += 1
  number = 00%number%
  StringRight number, number, 3
  loop  ;if the copy we are creating already exist (for previous operation), search the next free number
  {
   IfNotExist %dir%\%name_no_ext%-Copy %number%.%ext%
     break
   number += 1
   number = 00%number%
   StringRight number, number, 3
  }
  filecopy %clipboard%, %dir%\%name_no_ext%-Copy %number%.%ext%
 }
SplitPath A_AhkPath ,, A_AhkDir
IfNotExist %A_AhkDir%\LOG.txt
  FileAppend note: it is possible to delete this file`, will be created again if necessary`n`n`n, %A_AhkDir%\LOG.txt
FileAppend %A_DD%-%A_MM%-%A_YYYY% h%A_Hour%.%A_Min%.%A_Sec% Copied %clipboard% %MyUpDown% times`n, %A_AhkDir%\LOG.txt
gosub ButtonCancel
return



ButtonCancel:  ; __________________________ CANCEL ________________________________________________________
GuiEscape:
GuiClose:
tooltip ,,,, 3
Gui Destroy
BlockInput off
return



!r::reload       ; test only
!q::exitapp      ; test only
[/list]
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Jul 18, 2008 4:18 pm    Post subject: Reply with quote

I do agree on the idea behind this script. Nice work!

Only one comment at this moment: use a variable (or even a constant) for the key the user has to press to 'manycopy'. Now there are several places to replace the key.
Code:
; CHANGE LETTER "d" TO YOUR TASTE
Back to top
kardus



Joined: 17 Jul 2008
Posts: 9

PostPosted: Fri Jul 18, 2008 7:44 pm    Post subject: Reply with quote

Thanks for your reply, it's my first post add I am happy that someone answer!
I know it is not the best for customization (need to replace the letter "d" in 3 line), but I have to figure how is the simplest way (this is one of my first script, I'm learning ...).
I love AHK!
Back to top
View user's profile Send private message
kardus



Joined: 17 Jul 2008
Posts: 9

PostPosted: Sat Jul 19, 2008 1:57 pm    Post subject: Reply with quote

Now there is only one place to replace the hotkey (letter "d")

Code:
/*
This script to duplicate files.
When you need copies of a file, normally you press CTRL+C and CTRL+V several time.
Suppose you have "file.JPG", you'll get:
Copy of file.JPG
Copy (2) of file.JPG
Copy (3) of file.JPG
...
Copy (10) of file.JPG
Copy (11) of file.JPG
I think sometimes it is useless, due to lack of alpabetical order;
also, you can't specify how many copy you need in a single operation.
With the hotkey "d" below (demo only, change to an unused key according to your keyboard layout) you start the duplicate operation,
but only if you are in Windows Explorer or Xplorer˛ (I use it, see www.zabkat.com), and only if a file is selected;
of course, you can add Total Commander or similar.
You get, in a single operation:
file-Copy 001.JPG
file-Copy 002.JPG
file-Copy 003.JPG
...
file-Copy 010.JPG
file-Copy 011.JPG
*/


#SingleInstance ignore                                ; not really necessary, script with Gui are single-instance by default
Menu, Tray, Icon, %A_WinDir%\system32\Shell32.dll, 42 ; tree icon (on XP SP2)
CoordMode ToolTip


; this is the hotkey to start the action.
; "$" to prevent an infinite loop (since the hotkey may "sends itself" in the IF statement below)
; ***********************************************************************************************
; ********* CHANGE LETTER "d" BELOW TO YOUR TASTE ***********************************************
; ***********************************************************************************************
mc_hotkey = d
Hotkey $%mc_hotkey%, McLabel
return


McLabel:



; only if you are in Xplorer˛ or Windows Explorer it start the duplicate operation (add any File Manager)
; Windows Explorer: ahk_class CabinetWClass ahk_class ExploreWClass
if not (WinActive("xplorer˛ -") or WinActive("ahk_class CabinetWClass") or WinActive("ahk_class ExploreWClass"))
 {
  send %mc_hotkey%
  return
 }



send ^c      ; copy the file to duplicate
sleep 100    ; this appear to be necessary on my system



; the IF statement below to be sure you have copied a file, not a directory or some text
if not FileExist(clipboard)
 {
  ;msgbox ,,, INVALID clipboard `n(it is not a file), 2  ; test only
  send %mc_hotkey%
  return
 }
if InStr(FileExist(clipboard), "D")
 {
  ;msgbox ,,, INVALID clipboard `n(it is a directory), 2  ; test only
  send %mc_hotkey%
  return
 }



SplitPath clipboard, name
ToolTip DUPLICATING:`n%name%, (A_ScreenWidth/2.4), (A_ScreenHeight/3.2), 3
gui +AlwaysOnTop -MinimizeBox
gui font, s10, Arial
Gui, Add, Text,, How many Copy?`n (max.100)
Gui, Add, Edit, vMyEdit w60 h30 -VScroll Center Limit -Multi Number -WantReturn
Gui, Add, UpDown, vMyUpDown Range1-100, 1
Gui, Add, Button, Default xp+80, OK
Gui, Add, Button,, Cancel
Gui, Show
return



ButtonOK:  ; _____________________________________ OK _____________________________________________________
Gui +OwnDialogs
Gui Submit, NoHide

; again the IF statement to be sure you still have a file in the clipboard (just in case you have copied something else when GUI was active)
if not FileExist(clipboard)
 {
  msgbox ,,, INVALID clipboard `n(it is not a file), 2
  gosub ButtonCancel
  return
 }
if InStr(FileExist(clipboard), "D")
 {
  msgbox ,,, INVALID clipboard `n(it is a directory), 2
  gosub ButtonCancel
  return
 }

; this is to check the number of the copy selected
if MyEdit <> %MyUpDown%
 {
  msgbox ,,, INVALID number of Copy (%MyEdit%), 1
  return
 }
;msgbox ,,, you selected %MyUpDown% Copies, 1

; here we set a max size of the whole operation
FileGetSize size, %clipboard% ;the option M does'nt get decimal place
size := size / 1048576        ;to transform bytes in megabytes (1024x1024)
;msgbox ,,, size of the file is (%size%) MegaBytes, 5
if size * MyUpDown > 1024     ;(safety) stop if the output is bigger than 1 Giga
 {
  msgbox ,,, INVALID (copies size more than 1 Giga), 2
  return
 }

Gui Cancel    ; from here we start the copy operation
BlockInput on
ToolTip `n wait... COPY in progress `n`n (BlockInput)`n, (A_ScreenWidth/3), (A_ScreenHeight/3), 3
SplitPath clipboard, name, dir, ext, name_no_ext
number =
loop %MyUpDown%
 {
  number += 1
  number = 00%number%
  StringRight number, number, 3
  loop  ;if the copy we are creating already exist (for previous operation), search the next free number
  {
   IfNotExist %dir%\%name_no_ext%-Copy %number%.%ext%
     break
   number += 1
   number = 00%number%
   StringRight number, number, 3
  }
  filecopy %clipboard%, %dir%\%name_no_ext%-Copy %number%.%ext%
 }
SplitPath A_AhkPath ,, A_AhkDir
IfNotExist %A_AhkDir%\LOG.txt
  FileAppend note: it is possible to delete this file`, will be created again if necessary`n`n`n, %A_AhkDir%\LOG.txt
FileAppend %A_DD%-%A_MM%-%A_YYYY% h%A_Hour%.%A_Min%.%A_Sec% Copied %clipboard% %MyUpDown% times`n, %A_AhkDir%\LOG.txt
gosub ButtonCancel
return



ButtonCancel:  ; __________________________ CANCEL ________________________________________________________
GuiEscape:
GuiClose:
tooltip ,,,, 3
Gui Destroy
BlockInput off
return



!r::reload       ; test only
!q::exitapp      ; test only
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group