renaming files in the download folder

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pauloxoxe
Posts: 2
Joined: 18 Mar 2023, 09:20

renaming files in the download folder

18 Mar 2023, 09:29

I need to rename several downloaded files to a name that i have in a excel file.
i copy the filename but when i try to rename the file it doesn't do anything.

if i hardcode the filename it works.
What Im i doing wrong?
Please HELP!!

This is the code.

Code: Select all

F11:: 

Loop, 3
{
	;copy the text
	oApp := ComObjActive("Excel.Application")
	oApp.range("B"y).select
	oApp.Selection.Cut
	
	;paste the text
	IfWinExist, ahk_class Chrome_WidgetWin_1
	WinActivate
	
	;clicks hete to paste the text
	MouseClick, left,  354,348
	Sleep, 300
	Send ^a
	Send ^v

	;export the mp3 file
	MouseClick, left,  721, 715
	Sleep, 3000
		
	;copy the filename to rename
	oApp := ComObjActive("Excel.Application")
	oApp.range("A"y).select
	oApp.Selection.Cut
	
	clip = %Clipboard%
	msgBox, %clip%
	NewFileName = %clip%
	
	;slelect the download folder
	Folder = C:\Users\pcpi\Downloads

	;order by recent
	 Loop, %Folder%\*
	{
	     FileGetTime, Time, %A_LoopFileFullPath%, C
	     If (Time > Time_Orig)
	     {
	          Time_Orig := Time
	          File := A_LoopFileName
	     }
	}
	
	fileFullPath=%Folder%\%File%
	newfile=%Folder%\%NewFileName%
	Sleep, 300
	FileMove, %fileFullPath%,%newfile%
	y := y+1
}
MsgBox, Fim!!
log.finalizeLog("The End")
ExitApp
escape::
[Mod edit: [code][/code] tags added.]
[Mod edit: Topic moved to AHK v1 help.]
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: renaming files in the download folder

18 Mar 2023, 18:46

Welcome to this AutoHotkey forum!

Tips for debugging:
1. Remove the loop. Get a single iteration working first.
2. Assess what is being selected.
3. Assess what is being cut.
4. Why define oApp twice? Isn't once enough?
5. Use Loop, Files instead of the other deprecated looping command. See documentation.
6. Display the values of your variables, function calls, & conditional statements.
7. If the FileMove variables are wrong, work backwards to see how they became wrong. Although you currently have no idea what those values are, you can use the script to find out.
8. Simplify the script to focus on the most essential parts. Get those working first. Expand the script after that. If you don't need the inner loop for your testing, eliminate it. Instead of starting with a long script that does not work, start with a short script that does work. Expand it one step at a time, retesting iteratively.
cat dog fox war
Posts: 38
Joined: 15 Mar 2023, 10:18

Re: renaming files in the download folder

18 Mar 2023, 21:32

Because I don't have your excel and I don't have the line 54 log lib/obj, so I don't have a way to run it, here are some comments.

1. line 37 is old Syntax https://www.autohotkey.com/docs/v1/lib/LoopFile.htm#old, But since you said that there is no problem with the hardcoded case, I didn't change it
2. line 47~line 50 i recommend putting it after line43, specifically, if the if (Time > Time_Orig) at line 40 is not successful, the variable File may be "", or the value of File in the previous circle.
3. I did some checks before FileMove,
4. line 2 Loop, 3, only 3 seems unnecessary? You can add IfMsgBox break after line 29 msgBox, %clip%

Good luck!

Code: Select all

F11::
    Loop, 3
    {
        ;copy the text
        oApp := ComObjActive("Excel.Application")
        oApp.range("B"y).select
        oApp.Selection.Cut

        ;paste the text
        IfWinExist, ahk_class Chrome_WidgetWin_1
            WinActivate

        ;clicks hete to paste the text
        MouseClick, left, 354,348
        Sleep, 300
        Send ^a
        Send ^v

        ;export the mp3 file
        MouseClick, left, 721, 715
        Sleep, 3000

        ;copy the filename to rename
        oApp := ComObjActive("Excel.Application")
        oApp.range("A"y).select
        oApp.Selection.Cut

        clip = %Clipboard%
        msgBox, %clip%
        NewFileName = %clip%

        ;slelect the download folder
        Folder = C:\Users\pcpi\Downloads

        ;order by recent
        Loop, %Folder%\*
        {
            FileGetTime, Time, %A_LoopFileFullPath%, C
            If (Time > Time_Orig)
            {
                Time_Orig := Time
                File := A_LoopFileName
                fileFullPath=%Folder%\%File%
                newfile=%Folder%\%NewFileName%
                Sleep, 300
                isFileExist := FileExist(fileFullPath)
                If (isFileExist === "") {
                    MsgBox, % "old file is not exist !`n" fileFullPath
                    Continue ; Loop, %Folder%\*
                }
                isNewFileExist := FileExist(newfile)
                If (isNewFileExist !== "") {
                    MsgBox, % "new file is not exist !`n" newfile
                    Continue ; Loop, %Folder%\*
                }

                FileMove, %fileFullPath%,%newfile%
                Sleep, 100
                Break ; break ~Loop, %Folder%\*~ , Because I think you want to find the next excel cell
            }
        }
        y := y+1
    }
    MsgBox, Fim!!
    log.finalizeLog("The End")
ExitApp
escape:: ;What is this for? An unrelated piece uploaded by accident?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 91 guests