Sequential FileMove commands not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jazzster56
Posts: 4
Joined: 26 May 2023, 09:21

Sequential FileMove commands not working

Post by Jazzster56 » 02 Dec 2024, 09:52

I'm having trouble getting this file renaming script to work. If I select only one file, the FileMove (rename) works. But when I select two or more files, none of the FileMove commands work, even the first one. The message boxes that precede each FileMove command show the correct source and destination paths and files, but they don't execute. I put one second sleep commands in to give it time to execute the FileMove commands. I did another simple test script that contained three FileMove commands in sequence by just defining variables for paths and files with no sleep commands and all three files where renamed almost instantly. I'm stumped. This script is a simplified version of the script I'm working on. Yes, I know I'm an inefficient and sloppy coder!

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.

; Multi-File Renamer Script


#IfWinActive ahk_exe explorer.exe

^m::

;  CLEAR VARIABLES

clipboard =		; clear variable
Sleep, 100		; allow time for clipboard variable to empty


;  COPY FILENAMES (AND PATHS) & STRIP QUOTATION MARKS

; Line below: ^+c would not work. Required awkward method below
Send, {Control Down}{Shift Down}c{Shift Up}{Control Up}	; copies selected filenames in explorer to clipboard
Sleep, 100
StringReplace, QuotesStripped, Clipboard, `",,all  ; strip out quotation marks
; "QuoteStripped" variable now has all selected path with filenames listed w/o quotes


; SEPARATE EACH COPIED FILENAME/PATH INTO AN ARRAY VARIABLE

; Next: StrSplit lines (each copies Path/Filename) in the clipboard to array variables
	OrigFileArray := StrSplit(QuotesStripped, "`n")
	F_Array_Len := OrigFileArray.MaxIndex()
; At this point we have OrigFileArray[1] through [6]


; SPLIT EACH COPIED PATH/FILENAME INTO SEPARATE PATH ELEMENTS

; Next: Split the original path/filenames into separate elements
;	Line below: syntax of splitpath
;   SplitPath, input variable, filename only with ext, path w/o filename, file extension, filename w/o ext, drive

SplitPath, % OrigFileArray[1], fname_with_ext1, fdir1, fext1, fname_no_ext1, fdrv1
;MsgBox, fname_with_ext1: %fname_with_ext1%`nfdir1: %fdir1%`nfext1: %fext1%`nfname_no_ext1: %fname_no_ext1%`nfdrv1:  %fdrv1%

SplitPath, % OrigFileArray[2], fname_with_ext2, fdir2, fext2, fname_no_ext2, fdrv2 ;note fdir1, fdir2, etc. are the same
SplitPath, % OrigFileArray[3], fname_with_ext3, fdir3, fext3, fname_no_ext3, fdrv3
SplitPath, % OrigFileArray[4], fname_with_ext4, fdir4, fext4, fname_no_ext4, fdrv4
SplitPath, % OrigFileArray[5], fname_with_ext5, fdir5, fext5, fname_no_ext5, fdrv5
SplitPath, % OrigFileArray[6], fname_with_ext6, fdir6, fext6, fname_no_ext6, fdrv6

; Convert Array variables to normal variables because I don't like working with arrays
OrigPathFile1 := OrigFileArray[1]
OrigPathFile2 := OrigFileArray[2]
OrigPathFile3 := OrigFileArray[3]
OrigPathFile4 := OrigFileArray[4]
OrigPathFile5 := OrigFileArray[5]
OrigPathFile6 := OrigFileArray[6]


;MsgBox, OrigFilePath's:`n%OrigPathFile1%`n%OrigPathFile2%`n%OrigPathFile3%

; GUI TO LIST FILES (w/o PATHS TO AVOID GUI CLUTTER)

gui, Font, s12, Verdana		; smaller font for the hotstring/hotkey references
gui, add, text, x20, Ctrl-Alt-D - Today's Date
gui, add, text, x20 y+5, F10 - Date Picker
gui, add, text, x20 y+5, Ctrl-1 - 1_Prelim
gui, add, text, x20 y+5, Ctrl-2 - 2_PreClose
gui, add, text, x20 y+5, Ctrl-3 - 3_PostClose

gui, add, text, x350 y10, Number of files listed:  %F_Array_Len%
gui, add, text, x350 y40, Current Path:  %fdir1%

gui, Font, s16, Verdana		; bigger font for the edit boxes
gui, add, text, x350 y70, Prefix:
gui, add, edit, x430 w400 y70 vPrefix	; edit box for adding a common prefix to the filenames

gui, Font, s16, Verdana		; again, bigger font for the edit boxes
gui, add, edit, x20 w850 y120 vNewFile1, % fname_no_ext1 		; 1st edit box with existing filename as default entry
gui, add, text, x+10 w50, %fext1%						; add the file extenstion as a text after the filename edit box
gui, add, edit, x20 y+10 w850 vNewFile2, % fname_no_ext2
gui, add, text, x+10 w50, %fext2%
gui, add, edit, x20 y+10 w850 vNewFile3, % fname_no_ext3
gui, add, text, x+10 w50, %fext3%
gui, add, edit, x20 y+10 w850 vNewFile4, % fname_no_ext4
gui, add, text, x+10 w50, %fext4%
gui, add, edit, x20 y+10 w850 vNewFile5, % fname_no_ext5
gui, add, text, x+10 w50, %fext5%
gui, add, edit, x20 y+10 w850 vNewFile6, % fname_no_ext6
gui, add, text, x+10 w50, %fext6%

; add button
gui, add, button, x20 gRenameToSameFolder, Rename -Same Folder

gui, show, autosize, Multi-File Saver	; Gui name at top of window is Multi-File Saver

return		; return for gui

; Note: at this point, FileArray[n] is original filename, NewFile1, etc. is new filename


; GUI BUTTON SUBROUTINES

RenameToSameFolder:			; gui button subroutine
	gui, submit, nohide

	TargetFolder := fdir1 . "\"	; Note: fdir1, fdir2, etc. are all the same (it's the path only)

	; create a variable Proposed[n] to shorten new paths and destination into one variable
	;   but only for the number of files chosen, otherwise leave Proposed[n] variable blank
	; "Proposed[n]" variable is the filename plus the file extension

	If (NewFile1 != "")
		Proposed1 := Prefix . NewFile1 . "." . fext1

	If (NewFile2 != "")
		Proposed2 := Prefix . NewFile2 . "." . fext1

	If (NewFile3 != "")
		Proposed3 := Prefix . NewFile3 . "." . fext1

	If (NewFile4 != "")
		Proposed4 := Prefix . NewFile4 . "." . fext1

	If (NewFile5 != "")
		Proposed5 := Prefix . NewFile5 . "." . fext1

	If (NewFile6 != "")
		Proposed6 := Prefix . NewFile6 . "." . fext1

; RENAME COMMANDS.

	MsgBox, You've reached the rename commands.

	Sleep, 1000


	If (NewFile1 != "")
		{
			MsgBox, About to rename first file:`n%OrigPathFile1%`nto`n%TargetFolder%%Proposed1%
			Sleep, 1000
			FileMove, %OrigPathFile1%, %TargetFolder%%Proposed1%
			Sleep, 1000
		}

	If (NewFile2 != "")
		{
			MsgBox, About to rename second file:`n%OrigPathFile2%`nto`n%TargetFolder%%Proposed2%
			Sleep, 1000
			FileMove, %OrigPathFile2%, %TargetFolder%%Proposed2%
			Sleep, 2000
		}

	If (NewFile3 != "")
		{
			MsgBox, About to rename third file:`n%OrigPathFile3%`nto`n%TargetFolder%%Proposed3%
			Sleep, 1000
			FileMove, %OrigPathFile3%, %TargetFolder%%Proposed3%
			Sleep, 1000
		}

	If (NewFile4 != "")
		{
			MsgBox, About to rename fourth file:`n%OrigPathFile4%`nto`n%TargetFolder%%Proposed4%
			Sleep, 1000
			FileMove, %OrigPathFile4%, %TargetFolder%%Proposed4%
			Sleep, 1000
		}

	If (NewFile5 != "")
		{
			MsgBox, About to rename fifth file:`n%OrigPathFile5%`nto`n%TargetFolder%%Proposed5%
			Sleep, 1000
			FileMove, %OrigPathFile5%, %TargetFolder%%Proposed5%
			Sleep, 1000
		}

	If (NewFile6 != "")
		{
			MsgBox, About to rename sixth file:`n%OrigPathFile6%`nto`n%TargetFolder%%Proposed6%
			Sleep, 1000
			FileMove, %OrigPathFile6%, %TargetFolder%%Proposed6%
			Sleep, 1000
		}


	Gui, Destroy

return 	; return for RenameToSameFolder subroutine


GuiClose:
GuiEscape:
	Gui, Destroy
	return

return	; return for hotkey trigger (^m")

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

Re: Sequential FileMove commands not working

Post by mikeyww » 02 Dec 2024, 10:28

Hello,

Troubleshooting a file move can be tricky, but it's much easier if you do a few things.
  1. Examine the ErrorLevel from the move.
  2. Use FileExist to determine whether the source path exists.
  3. Use FileExist to determine whether the target path exists.
Any source path containing a carriage return does not exist.

Code: Select all

	OrigFileArray := StrSplit(QuotesStripped, "`n")
	MsgBox % InStr(OrigFileArray[1], "`r") "`nFileExist: " FileExist(OrigFileArray[1])
	OrigFileArray := StrSplit(QuotesStripped, "`n", "`r")
	MsgBox % InStr(OrigFileArray[1], "`r") "`nFileExist: " FileExist(OrigFileArray[1])

Jazzster56
Posts: 4
Joined: 26 May 2023, 09:21

Re: Sequential FileMove commands not working

Post by Jazzster56 » 02 Dec 2024, 12:55

Thank you Mikeyww!

I did not understand your two MsgBox lines in your replay, but I did check for `n using

Code: Select all

InStr(OrigFileArray[1], "`n")
and found nothing. However, I also checked for `r using InStr and found one at the end of the variable. So I edited my

Code: Select all

StrSplit(QuotesStripped, "`n")
to include the "`r" as per your suggestion and it now works fine.

What still confuses me is that when I selected only one file to be renamed in the unfixed script, it worked to rename it. When I selected two or more, none of them were renamed. I will tell you that that I never would have picked up on the issue of FileMove not working because of a hidden carriage return! I think a loop operation strips out the newline/carriage returns which would avoid this problem but loop syntax is difficult for me.

BTW, I've been programming for a few years with ahk and I have found many solutions to my programming problems in this forum by just searching and not having to ask. Many of my answers were your replies to other users! I'm still very amateurish but have created a number of very useful scripts. Thank you for your service to this forum and ahk users!

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

Re: Sequential FileMove commands not working

Post by mikeyww » 02 Dec 2024, 14:29

Good to hear. :)

It's unclear why you are confused, because you have already checked for `r using InStr and found one at the end of the variable. Did you also find it when only one file was selected?

With one selection, no newline characters are present. With multiple selections, the newlines separate the file paths, so this is where your original string split failed, by retaining the carriage returns. You can confirm this by examining your variable.

Jazzster56
Posts: 4
Joined: 26 May 2023, 09:21

Re: Sequential FileMove commands not working

Post by Jazzster56 » 02 Dec 2024, 15:22

The check for `r was not in the original script I posted. I added that in my testing after reading your first reply.

However, your explanation of a single selection not having any newline/return at the end makes sense (because they are not needed). Now that you mentioned it, I have a vague recollection of another script from long ago where a had to adjust for the fact that a multi-line variable had newline/returns after each line except for the last line which didn't have any.

Thanks again!

Post Reply

Return to “Ask for Help (v1)”