[Solved] Why won't this simple file loop work?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dipstick5000
Posts: 31
Joined: 21 Jan 2020, 22:01

[Solved] Why won't this simple file loop work?

24 Jan 2020, 03:52

I'm freaking out because yesterday this worked perfectly, but for the past 6 hours it doesn't work.

Code: Select all

counter := 15
Loop, Files, C:\2 sounds\*.mp3, F
{
    MsgBox, 262144, , 
    (LTrim
    About to copy sound named "%A_LoopFileName%"
    New name will be "%counter%.%A_LoopFileExt%"
    )
    FileCopy, C:\2 sounds\*.mp3, C:\2 sounds copied\%counter%.%A_LoopFileExt%
    counter += 1
}
The message box tells me it's about to copy the proper sound, but then once copied, both sounds are the first sound, but named as expected:

About to copy sound named "does everybody want soup.mp3"
New name will be "16.mp3"

Code: Select all

Directory of C:\2 sounds\
do I lie.mp3           
does everybody want soup.mp3

Total 0 folder(s); 2 file(s)

Directory of C:\2 sounds copied\
15.mp3                 
16.mp3                 

Total 0 folder(s); 2 file(s)
I'm completely stumped. And yes I've quadruple checked that the 2 original sounds are actually different. Can you please help?

[Mod edit: [code][/code] tags added]
Last edited by dipstick5000 on 24 Jan 2020, 14:49, edited 1 time in total.
dipstick5000
Posts: 31
Joined: 21 Jan 2020, 22:01

Re: Why won't this simple file loop work?

24 Jan 2020, 04:24

I just tried it with 3 text files. It does the same thing. All are named as expected, but all are the first file.
gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Why won't this simple file loop work?

24 Jan 2020, 05:05

dipstick5000 wrote:
24 Jan 2020, 03:52

Code: Select all

FileCopy, C:\2 sounds\*.mp3, C:\2 sounds copied\%counter%.%A_LoopFileExt%
Try to replace C:\2 sounds\*.mp3 in the FileCopy line with %A_LoopFilePath%:
https://www.autohotkey.com/docs/commands/LoopFile.htm#Special_Variables_Available_Inside_a_File-Loop wrote:The path and name of the file/folder currently retrieved. [...]
That's what the file-loop variables are for. I think C:\2 sounds\*.mp3 will, in this case, always just match - and use - the first matching file, every loop iteration again, so that you end up with the same audio or text or whatever, just with different names (because the destination parameter actually uses variables).

It would be different, if the DestPattern parameter would be just a folder (but then you couldn't rename at the same time).
dipstick5000
Posts: 31
Joined: 21 Jan 2020, 22:01

Re: Why won't this simple file loop work?

24 Jan 2020, 05:42

gregster wrote:
24 Jan 2020, 05:05
I think C:\2 sounds\*.mp3 will, in this case, always just match - and use - the first matching file, every loop iteration again, so that you end up with the same audio or text or whatever, just with different names
But if that's true why would the message box tell me it's about to use the proper file, not the first one? I guess that's happening in the line before the file copy, so I suppose... I don't know, I'll play around with A_LoopFilePath. Thank you for the idea.
gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Why won't this simple file loop work?

24 Jan 2020, 05:52

Why play around? Try this:

Code: Select all

FileCopy, %A_LoopFilePath%, C:\2 sounds copied\%counter%.%A_LoopFileExt%
(not tested, though)
But if that's true why would the message box tell me it's about to use the proper file, not the first one?
In the msgbox, you are clearly using A_LoopFileName which is meant to be used in loops for things like this, just like A_LoopFilePath. Only difference, the latter contains the file path.
dipstick5000
Posts: 31
Joined: 21 Jan 2020, 22:01

Re: Why won't this simple file loop work?

24 Jan 2020, 06:07

gregster wrote:
24 Jan 2020, 05:05
Try to replace C:\2 sounds\*.mp3 in the FileCopy line with %A_LoopFilePath%:
FileCopy, %A_LoopFilePath%\*.*, C:\3 docs copied\%counter%.%A_LoopFileExt%

This produces properly named empty files (0 bytes).
gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Why won't this simple file loop work?

24 Jan 2020, 06:12

dipstick5000 wrote:
24 Jan 2020, 06:07
FileCopy, %A_LoopFilePath%\*.*, C:\3 docs copied\%counter%.%A_LoopFileExt%

This produces properly named empty files (0 bytes).
Well, that's not what I recommended.

Like this, It will most likely find no source file at all, hence 0 bytes.
dipstick5000
Posts: 31
Joined: 21 Jan 2020, 22:01

Re: Why won't this simple file loop work?

24 Jan 2020, 06:13

FileCopy, %A_LoopFilePath%, C:\3 docs copied\%counter%.%A_LoopFileExt%

this does nothing,

Also message box tells me %A_LoopFilePath% is blank, should that happen?
User avatar
AlexL
Posts: 14
Joined: 13 Dec 2019, 15:30
Contact:

Re: Why won't this simple file loop work?

24 Jan 2020, 06:13

dipstick5000 wrote:
24 Jan 2020, 05:42
But if that's true why would the message box tell me it's about to use the proper file, not the first one? I guess that's happening in the line before the file copy, so I suppose... I don't know, I'll play around with A_LoopFilePath. Thank you for the idea.
Friend, your code is very clear in this; when you use the command MsgBox, you use variables that reference the specific file active in the loop:

Code: Select all

    About to copy sound named "%A_LoopFileName%"
    New name will be "%counter%.%A_LoopFileExt%"
But, when you use the FileCopy command, you are calling another reference, to the list of files where you start the loop:

Code: Select all

    FileCopy, C:\2 sounds\*.mp3, C:\2 sounds copied\%counter%.%A_LoopFileExt%
FileCopy will do either; copy all .mp3 files in that reference and stack them elsewhere with the same filename (because you make destination so), meaning you end up with one file copied (the last on the folder), or just pick the first file in that reference and copy it to destination.
And since the Filecopy command always uses the same origin reference that doesn't specify 1 file, you will end up with the same file copied many times.
The correct way to do it is using

Code: Select all

    FileCopy, C:\2 sounds\%A_LoopFileName%.%A_LoopFileExt%, C:\2 sounds copied\%counter%.%A_LoopFileExt%
Let me show you with a very simple but rude code draft:
You did this:

Code: Select all

Loop item in original-List
{
filecopy original-list onto destination-item
}
When you should have done this:

Code: Select all

Loop item in original-List
{
filecopy item onto destination-item
}
Cheers.
gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Why won't this simple file loop work?

24 Jan 2020, 06:16

Did you use the correct loop line? mp3 extension for mp3 ? txt for text files ?

Please show your complete updated code, if the problem persists.

Or have a look at what this says:

Code: Select all

Loop, Files, C:\2 sounds\*.mp3, F
    MsgBox, %A_LoopFileName% `nvs`n %A_LoopFilePath%
See the difference?

@AlexL: %A_LoopFilePath% should already contain the whole file path of the current loop iteration.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Why won't this simple file loop work?

24 Jan 2020, 06:30

Try:

Code: Select all

counter := 15
Loop, Files, C:\2 sounds\*.mp3, F
    FileCopy, % A_LoopFileLongPath, % "C:\2 sounds copied\" . counter++ . ".mp3"
If this doesnt work then your dest folder does not exist or there were no .mp3 file in the source folder.
dipstick5000
Posts: 31
Joined: 21 Jan 2020, 22:01

Re: Why won't this simple file loop work?

24 Jan 2020, 14:48

What a relief. A big thank you to everyone who helped. Either I wouldn't have figured it out without you guys, or it would have taken weeks instead of days.

This does nothing:
FileCopy, %A_LoopFilePath%, C:\2 sounds copied\%counter%.%A_LoopFileExt%

This produces empty files:
FileCopy, C:\2 sounds\%A_LoopFilePath%, C:\2 sounds copied\%counter%.%A_LoopFileExt%

This works:
FileCopy, %A_LoopFileLongPath%, C:\2 sounds copied\%counter%.%A_LoopFileExt%

I don't know if I feel more, or less stupid now. I guess I know more, but is knowledge synonymous with intelligence?
User avatar
AlexL
Posts: 14
Joined: 13 Dec 2019, 15:30
Contact:

Re: [Solved] Why won't this simple file loop work?

10 Feb 2020, 06:00

As Aristotles said once:
“The more you know, the more you know you don't know.”

Be at ease. We live to learn, love, create, share and evolve. The rest... are tears in the rain. :salute:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: catquas, Google [Bot], jpsala and 179 guests