append file names with the current folder names

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nicrox
Posts: 14
Joined: 14 Mar 2016, 19:05

append file names with the current folder names

27 Aug 2017, 21:11

hi all,
I am trying to write a script that does this:
1- I navigate to a folder
2- I right click in that folder and click "rename all"
3- script reads the folder name and grabs the folder name only (eg C:\user\name\currfolder in this case 'currfolder')
4- rename all files within this folder as below:
file_1.pdf -> file_1.currfolder.pdf
file_2.pdf -> file_2.currfolder.pdf
.
.
file_n.pdf to file_n.currfolder.pdf

appreciate some guidelines or if anyone aware of a similar script which I may be able to modify and use.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: append file names with the current folder names

28 Aug 2017, 03:47

Code: Select all

#SingleInstance, Force
#Persistent

fExt	:= "ahk"																								; change accordingly
fPath	:= A_ScriptDir																							; change accordingly

F10::																											; press F10 to start renaming files 
	Loop, Files,% fPath . "\*." fExt
		{
		SplitPath, A_LoopFileFullPath, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
		fDir := StrSplit(OutDir,"\")
		FileMove,% A_LoopFileFullPath,% OutNameNoExt "_" . fDir[fDir.MaxIndex()] . "." . OutExtension, 0		; specify a different destination path to move instead of renaming files
		}
	Return	
Not tested.
nicrox
Posts: 14
Joined: 14 Mar 2016, 19:05

Re: append file names with the current folder names

12 Sep 2017, 18:46

Thanks Bobo,
The A_ScriptDir in this context returns the path of the ahk script and not the folder I am in. I am trying to find a way to extract the current folder path based on "mouse right click' event.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: append file names with the current folder names

12 Sep 2017, 19:27

nicrox, if you need to get the title and path of an active folder, you may use this:

Code: Select all

$F10:: 
   title      := GetShellFolder().Title
   folderPath := GetShellFolder().Self.Path
   MsgBox, % title . "`n" . folderPath
   return

GetShellFolder(hwnd := "")
{
   ( !hwnd && hwnd := WinExist("A") )
   WinGet, process, ProcessName, ahk_id %hwnd%
   WinGetClass, winClass, ahk_id %hwnd%

   if !( process = "explorer.exe" && winClass ~= "(Cabinet|Explore)WClass" )
      Return
      
   for window in ComObjCreate("Shell.Application").Windows
      if ( window.hwnd = DllCall("GetAncestor", Ptr, hwnd, "UInt", GA_ROOT := 2, Ptr) )
         return window.document.Folder
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: wilkster and 311 guests