Rename a .jpg file using the name of the containing folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Rename a .jpg file using the name of the containing folder

Post by siomosp » 18 Jan 2022, 10:28

Hi,
my first post here..
( From a quick search i cannot find an answer...)
I have over 500 folders.
Each one contains one .jpg and one .psd file
I want to rename only the .jpg inside the folder using the name of the containing folder
It is possible?

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

Re: Rename a .jpg file using the name of the containing folder

Post by mikeyww » 18 Jan 2022, 11:21

Welcome to this AutoHotkey forum.

Code: Select all

dir = %A_ScriptDir%\test
SplitPath, dir, name
Loop, Files, %dir%\*.jpg
{ FileMove, %A_LoopFilePath%, %dir%\%name%.jpg
  If ErrorLevel
   MsgBox, 48, Error, An error occurred while renaming the file.`n`n%A_LoopFilePath%
  Break
}
MsgBox, 64, Done, Done!
Return

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 19 Jan 2022, 02:40

Hello, thank you for the welcome and the code!
It is possible to edit your code for every selected folder?
I found a similar code witch works on every selected file

Code: Select all

#IfWinActive ahk_class CabinetWClass
^F6:: ;explorer - focused file get path, create folder, copy file
WinGet, hWnd, ID, A
oWin := ""
for oWin2 in ComObjCreate("Shell.Application").Windows
	if (oWin2.HWND = hWnd)
	{
		oWin := oWin2
		break
	}
oWin2 := ""
if !oWin
	return
vPath := oWin.Document.FocusedItem.path
oWin := ""
if !(vAttrib := FileExist(vPath))
|| InStr(vAttrib, "D")
	return
SplitPath, vPath, vName, vDir, vExt, vNameNoExt, vDrive
vDirNew := vDir "\" vNameNoExt
vPathNew := vDirNew "\" vName
FileCreateDir, % vDirNew
;MsgBox, % vPath "`r`n`r`n" vPathNew
;FileCopy, % vPath, % vPathNew
FileMove, % vPath, % vPathNew
return

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

Re: Rename a .jpg file using the name of the containing folder  Topic is solved

Post by mikeyww » 19 Jan 2022, 07:18

Code: Select all

F3::
For each, dir in getSelected() {
 If !Instr(FileExist(dir), "D")
  Continue
 SplitPath, dir, name
 Loop, Files, %dir%\*.jpg
 { FileMove, %A_LoopFilePath%, %dir%\%name%.jpg
   If ErrorLevel
    MsgBox, 48, Error, An error occurred while renaming the file.`n`n%A_LoopFilePath%
   Break
 }
}
MsgBox, 64, Done, Done!
Return

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
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
 Return selection
}

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 19 Jan 2022, 09:20

Thank you for the new code
I don't know if i do something wrong, but although i get the "done" message, the renaming is not done
Please see the screen recording
https://screencast-o-matic.com/watch/c3Vq2FVoLj2
I was expecting the jpg inside the "Test dir" to be renamed as "Test dir.jpg"

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 19 Jan 2022, 09:31

Ignore my latest reply,
your code works fine everywhere except at Windows desktop!
Thank you!

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

Re: Rename a .jpg file using the name of the containing folder

Post by mikeyww » 19 Jan 2022, 10:29

OK. The script is for File Explorer, as you made no prior mention of the desktop. Below is a way to get the desktop selection if you need that.

viewtopic.php?p=154836#p154836

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 28 Jan 2022, 07:41

oups,
i posted the question at wrong topic!
Please ignore it!

Hello again!
i can use your help again?
Your code works fine of one selection
Can be edited to work for all selected folders?

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 28 Jan 2022, 12:59

Hi, the correct question is:
I have found a script to copy selected file name / create a new folder / copy the file to folder
viewtopic.php?f=76&t=60237
Can be adapted to work with all selected files, not one by one?
Thank you!
(I am trying many hours to accomplish it, without success...)

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

Re: Rename a .jpg file using the name of the containing folder

Post by mikeyww » 28 Jan 2022, 13:02

The script works to rename one file in each selected folder.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 28 Jan 2022, 13:32

It is possible to work with multiple files selection ?

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

Re: Rename a .jpg file using the name of the containing folder

Post by mikeyww » 28 Jan 2022, 14:03

Since you want to name the file according to its directory, it would not work for multiple files within a directory, since they would have the same name.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 28 Jan 2022, 14:27

May be i had to post new topic because my new question is not about "Rename a .jpg file using the name of the containing folder"
It is about the topic viewtopic.php?f=76&t=60237
"copy selected file name / create a new folder / copy the file to folder"
I am asking for:
Select multiple files
For each file
copy selected file name / create folder using this file name / copy the file to created folder
Go to next file
(There isn't same named folders problem)
I hope it s more clear now!
(Sorry for my not so good English, me native language is Greek)

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

Re: Rename a .jpg file using the name of the containing folder

Post by mikeyww » 28 Jan 2022, 14:36

If your question does not match this thread's topic, then please put it into a thread where it does match. At least that is my advice. Thank you.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Rename a .jpg file using the name of the containing folder

Post by siomosp » 28 Jan 2022, 15:27

I will, thank you!

Post Reply

Return to “Ask for Help (v1)”