Moving files and creating folders by condition. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Destructoid
Posts: 4
Joined: 17 Nov 2022, 15:43

Moving files and creating folders by condition.

Post by Destructoid » 30 Nov 2022, 08:27

Hi all. I am new to using AutoHotKey. And here is one of my first problems that I can't solve. I would like to ask the AutoHotKey community to help me figure it out.

My goal: I have, for example, 100 files. I want the script to create a folder named 1 in the same folder where those files are located, then move 10 files out of 100 there, then create folder 2, and move 10 files out of the remaining 90 there again, and so on. Until there are no files left. But the total number of files does not have to be 100. The number can be odd, so the last folder can have fewer files.

Here is my code with explanations:

Code: Select all

FileSelectFile, SourceFile, 3,, OPEN THE LIST WITH FILE NAMES, Text File (*.txt) ; Here I specify a list of files ordered in ascending order, by size. I don't yet know how to write code so that AHK itself will order the files this way and move the files from smaller to larger. Moreover, it will increase my code a lot. Therefore, I decided to create a list of files in advance through CMD - dir /s /b /OS > list.txt
if (SourceFile = "")
    Return

folderName := 0
increaseDirName:			; I assume that because of this label, the script creates an infinite number of folders after successfully moving 10 files to folder 1.
++folderName
FileCreateDir, D:\%folderName%

Loop, read, %SourceFile%		; The loop reads lines from my file, one at a time, then sends to the move subroutine.
{
FileNameLine := A_LoopReadLine
	Gosub, MoveFiles
		If (A_Index = 11)
			Break		; I stop the loop after moving 10 files.
}
Goto, increaseDirName		; After moving the 10 files, I send the script to label increase the variable by 1 and create a folder with a new name.

MoveFiles:                        ;  Move subroutine.
	FileMove, %FileNameLine%, D:\%folderName%
	Return
The script above creates folder 1, moves 10 files into it, then creates an infinite number of folders until the script is terminated. This doesn't solve my problem.

I suppose I can do without GoTo, but I don't know how to reset the A_Index in the loop, after moving, especially if the loop is stopped and started again, then the script will look for files from the list that are already moved, in that case the loop does not need to be stopped?

If there is a way to solve my problem without creating a list.txt, moving the files directly, as long as they will be ordered by increasing size, then I am willing to consider this solution.

Thanks in advance to anyone who can help me solve this problem.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Moving files and creating folders by condition.

Post by boiler » 30 Nov 2022, 08:53

Loop through all the files at once, and put this in the loop:

Code: Select all

if !Mod(A_Index, 10)
	++folderName
…which will increase the folder name by one every time A_Index is divisible by 10 (without remainder).

User avatar
Destructoid
Posts: 4
Joined: 17 Nov 2022, 15:43

Re: Moving files and creating folders by condition.

Post by Destructoid » 30 Nov 2022, 15:46

boiler wrote:
30 Nov 2022, 08:53
Loop through all the files at once, and put this in the loop:

Code: Select all

if !Mod(A_Index, 10)
	++folderName
…which will increase the folder name by one every time A_Index is divisible by 10 (without remainder).
Thank you very much for your response and for your help. This is the code I got and it works.

Code: Select all

FileSelectFile, SourceFile, 3,, OPEN THE LIST WITH FILE NAMES, Text File (*.txt)
if (SourceFile = "")
    Return

folderName := 1
FileCreateDir, D:\%folderName%

Loop, read, %SourceFile%
{
	FileNameLine := A_LoopReadLine
		Gosub, MoveFiles
		if !Mod(A_Index, 10)
			++folderName
			FileCreateDir, D:\%folderName%
}

MoveFiles:
	FileMove, %FileNameLine%, D:\%folderName%
	Return
Lastly, tell me. Does my code work correctly? Without hidden errors that I can't see from the results through windows explorer?

One last question, is there any way to optimize my code and use the FileCreateDir, D:\%folderName%, just once, the one in the loop? Or not? Create folder 1 manually, then lines 5-6 are not needed? And through a script?

Thanks in advance for the answer.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Moving files and creating folders by condition.  Topic is solved

Post by boiler » 30 Nov 2022, 16:41

Destructoid wrote:
30 Nov 2022, 15:46
Lastly, tell me. Does my code work correctly? Without hidden errors that I can't see from the results through windows explorer?
You have a problem with this part of the code. If it works, it's because it's creating the directory every time and ignores it when it already exists.

Code: Select all

		if !Mod(A_Index, 10)
			++folderName
			FileCreateDir, D:\%folderName%
The reason is that indenting doesn't group lines into code blocks. Braces do, like this:

Code: Select all

		if !Mod(A_Index, 10)
		{
			++folderName
			FileCreateDir, D:\%folderName%
		}

Destructoid wrote:
30 Nov 2022, 15:46
One last question, is there any way to optimize my code and use the FileCreateDir, D:\%folderName%, just once, the one in the loop? Or not? Create folder 1 manually, then lines 5-6 are not needed? And through a script?
Your script can be reduced to this:

Code: Select all

FileSelectFile, SourceFile, 3,, OPEN THE LIST WITH FILE NAMES, Text File (*.txt)
if (SourceFile = "")
    Return

Loop, Read, %SourceFile%
{
	if !Mod(A_Index - 1, 10)
		FileCreateDir, % "D:\" Floor(A_Index / 10) + 1
	FileMove, %A_LoopReadLine%, % "D:\" Floor((A_Index - 1) / 10) + 1
}
Run it to double-check my math.

User avatar
Destructoid
Posts: 4
Joined: 17 Nov 2022, 15:43

Re: Moving files and creating folders by condition.

Post by Destructoid » 30 Nov 2022, 17:26

Thanks for the explanation of the code blocks. In the future, after IF, I will always take subsequent expressions in code blocks.

And lastly, since you have changed my code considerably, could you please explain, through comments as in the example, the meaning of these lines? This is the first time I've seen these mathematical operators and I'm not good at math.

Code: Select all

Loop, read, %SourceFile%
{
	if !Mod(A_Index - 1, 10)									;  If A_Index minus 1 divided by 10, gives the answer with no remainder. Then:
		FileCreateDir, % "D:\" Floor(A_Index / 10) + 1				; Create a directory on drive D that is called a rounded number, iteration of a loop divided by 10 plus 1? For example Floor(88 / 10) + 1 = create a folder named 9. Сorrect?
	FileMove, %A_LoopReadLine%, % "D:\" Floor((A_Index - 1) / 10) + 1	; ? I can't figure it out.
}

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Moving files and creating folders by condition.

Post by boiler » 30 Nov 2022, 18:41

See the comments:

Code: Select all

Loop, read, %SourceFile%
{
	if !Mod(A_Index - 1, 10)									;  If A_Index minus 1 divided by 10, gives the answer with no remainder. Then:
		FileCreateDir, % "D:\" Floor(A_Index / 10) + 1				; Correct except it would never happen with 88 because of the prior "if" statement, so it would be Floor(80 / 10) + 1 = create a folder named 9
	FileMove, %A_LoopReadLine%, % "D:\" Floor((A_Index - 1) / 10) + 1	; This subtracts 1 from A_Index because it makes the math work out since I believe you want 10 to go into folder 1 instead of 2, so it makes the math work out
}

User avatar
Destructoid
Posts: 4
Joined: 17 Nov 2022, 15:43

Re: Moving files and creating folders by condition.

Post by Destructoid » 30 Nov 2022, 19:38

Thanks for the explanation, boiler. I will take your version of the script to use. Because it is more optimized, and it works properly, I checked. And these mathematical operators, I will study them in more detail to better understand how they work. Thank you for your support. Good luck.

Post Reply

Return to “Ask for Help (v1)”