Hotkey to copy file and paste with a different name Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Hotkey to copy file and paste with a different name

Post by Avastgard » 10 Oct 2023, 11:12

I constantly have to work on files other people created and make changes to it. So every time I download the file, make a copy of it, rename it and then open the file to make changes.

After some digging in the forums, I came up with this script that does part of the job, but doesn't change the name of the file - it only copies and pastes the file and turns on renaming mode

Code: Select all

^+b:: ;explorer - copy and paste focused file, focus new file, edit new file name
hWnd := WinExist("A")
for oWin2 in ComObjCreate("Shell.Application").Windows
	if (oWin2.HWND = hWnd)
	{
		oWin := oWin2
		break
	}
oWin2 := ""
if !oWin
|| ((vPath := oWin.Document.FocusedItem.path) = "")
|| !FileExist(vPath)
{
	oWin := ""
	return
}
vDate := A_Now
Clipboard := ""
SendInput, ^c
vCount := oWin.Document.Folder.Items.Count
vDir1 := oWin.Document.Folder.Self.Path
oArray := []
for oItem in oWin.Document.Folder.Items
	if !(oItem.path = "")
		oArray["z" oItem.path] := 1
ClipWait, 3
if ErrorLevel
{
	MsgBox, % "error: failed to copy file"
	return
}
SendInput, ^v
Loop, 300
{
	vCount2 := oWin.Document.Folder.Items.Count
	if (vCount2 > vCount)
		break
	Sleep, 10
}
for oItem in oWin.Document.Folder.Items
	if !(oItem.path = "")
	&& !oArray["z" oItem.path]
	{
		;SVSI_FOCUSED = 0x10 ;SVSI_ENSUREVISIBLE := 0x8
		;SVSI_DESELECTOTHERS := 0x4 ;SVSI_EDIT := 0x3
		;SVSI_SELECT := 0x1 ;SVSI_DESELECT := 0x0
		oWin.Document.SelectItem(oItem, 0x1F)
		break
	}
return
How can I tell it to append text to the end of the copied file's name?

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Hotkey to copy file and paste with a different name  Topic is solved

Post by mikeyww » 10 Oct 2023, 14:38

Code: Select all

#Requires AutoHotkey v1.1.33
fnSuffix := "-new"

#If WinActive("ahk_class CabinetWClass")
^+b:: ; Explorer - copy and paste focused file, focus new file, edit new file name
For each, filePath in getSelected() {
 SplitPath filePath, fn, dir, ext, fnBare
 fileWasCopied := False
 Loop {
  InputBox newFN, New file name, Enter a new file name,, 300, 125,,,,, % fnBare fnSuffix "." ext
  If !ErrorLevel && newFN != "" {             ; User entered a new file name
   If !FileExist(newPath := dir "\" newFN) {  ; If file does not already exist,
    FileCopy % filePath, % newPath            ;  then copy the file
    If ErrorLevel
     MsgBox 48, Error, % "An error occurred while copying the file.`n`n" newPath
    Else fileWasCopied := True
   } Else MsgBox 48, Exists, % "File already exists.`n`n" newPath
  } Else Return ; User did not enter a new file name
 } Until fileWasCopied
 If fileWasCopied
  Run % newPath ; Open the new file
}
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: Hotkey to copy file and paste with a different name

Post by andymbody » 10 Oct 2023, 15:08

Mike beat me to it, of course, and his is very similar. But here is an alternative as well... I tried to include some of your original code

Code: Select all

#SingleInstance force

return
esc::ExitApp

^+b:: ;explorer - copy and paste focused file, focus new file, edit new file name

	; make sure active window is explorer?
	hWnd := WinExist("A")	; explorer I assume?
	for curWin in ComObjCreate("Shell.Application").Windows
	{
		if (curWin.HWND == hWnd)
		{
			eWin := curWin
			break
		}
	}
	curWin := ""

	; make sure explorer is active and at least one item is listed in current folder
	if (!eWin || ((vPath := eWin.Document.FocusedItem.path) == "") || (!FileExist(vPath)))
	{
		eWin := ""
		return
	}

	; copy selected files, (also grabs filePaths)
	Clipboard := ""
	SendInput, ^c
	ClipWait

	; copy files renaming in the process
	selFilePaths := Clipboard
	dir := eWin.Document.Folder.Self.Path
	cDate := A_Now	; provides a unique name appendage
	Loop, parse, selFilePaths, `n, `r
	{
		curPath := A_LoopField
		ToolTip, % "Copying...`n" . curPath
		Sleep, 200		; allow tooltip to fully show
		SplitPath, curPath,,,ext,bfn
		nfn := bfn . " " . cDate . "." . ext
		newPath := dir . "\" . nfn
		FileCopy, % curPath, % newPath
		if (!pathExist(newPath))
		{
			ToolTip
			MsgBox % "Unable to verify `n" . newPath . "`n`nAborting copy"
			return
		}		
	}
	ToolTip
	MsgBox % "Copy Complete"
	;ExitApp
	return


;################################################################################
																   isDir(srcPath)
{
	return (InStr(FileExist(srcPath), "D")) ? true : false
}
;################################################################################
															   pathExist(srcPath)
{
	if (isDir(srcPath))
		return true						; ok - is a folder and path does exist
	else
	{
		fObj := FileOpen(srcPath, "r")	; this is more reliable than FileExist()
		if (!IsObject(fObj))
			return false
		Sleep, 100						; probably not needed
		fObj.Close()					; make sure file is closed
	}
	return true
}


Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Hotkey to copy file and paste with a different name

Post by Avastgard » 26 Oct 2023, 20:56

Thanks for the codes, @mikeyww and @andymbody! They are both great!

@mikeyww, your code seems to suit me slightly better because it asks me to confirm the name of the new file in an inputbox. However, I can't seem to change fnSuffix := "-new" in line 2 to show current date and time in this format: yyyyMMdd. How can I do that?
mikeyww wrote:
10 Oct 2023, 14:38

Code: Select all

#Requires AutoHotkey v1.1.33
fnSuffix := "-new"

#If WinActive("ahk_class CabinetWClass")
^+b:: ; Explorer - copy and paste focused file, focus new file, edit new file name
For each, filePath in getSelected() {
 SplitPath filePath, fn, dir, ext, fnBare
 fileWasCopied := False
 Loop {
  InputBox newFN, New file name, Enter a new file name,, 300, 125,,,,, % fnBare fnSuffix "." ext
  If !ErrorLevel && newFN != "" {             ; User entered a new file name
   If !FileExist(newPath := dir "\" newFN) {  ; If file does not already exist,
    FileCopy % filePath, % newPath            ;  then copy the file
    If ErrorLevel
     MsgBox 48, Error, % "An error occurred while copying the file.`n`n" newPath
    Else fileWasCopied := True
   } Else MsgBox 48, Exists, % "File already exists.`n`n" newPath
  } Else Return ; User did not enter a new file name
 } Until fileWasCopied
 If fileWasCopied
  Run % newPath ; Open the new file
}
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}


Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Hotkey to copy file and paste with a different name

Post by Avastgard » 27 Oct 2023, 07:49

Neat, thank you very much! Here is my final script that works as intended:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#SingleInstance force

#Requires AutoHotkey v1.1.33
FormatTime, CurrentDateTime,, yyyyMMdd



#If WinActive("ahk_class CabinetWClass")
^+b:: ; Explorer - copy and paste focused file, focus new file, edit new file name
For each, filePath in getSelected() {
 SplitPath filePath, fn, dir, ext, fnBare
 fileWasCopied := False
 Loop {
  InputBox newFN, New file name, Enter a new file name,, 300, 125,,,,, % fnBare "_" CurrentDateTime "." ext
  If !ErrorLevel && newFN != "" {             ; User entered a new file name
   If !FileExist(newPath := dir "\" newFN) {  ; If file does not already exist,
    FileCopy % filePath, % newPath            ;  then copy the file
    If ErrorLevel
     MsgBox 48, Error, % "An error occurred while copying the file.`n`n" newPath
    Else fileWasCopied := True
   } Else MsgBox 48, Exists, % "File already exists.`n`n" newPath
  } Else Return ; User did not enter a new file name
 } Until fileWasCopied
 If fileWasCopied
  Run % newPath ; Open the new file
}
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

Post Reply

Return to “Ask for Help (v1)”