AHK Write Nth Filename from folder to Text File (iteration)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
user99000
Posts: 4
Joined: 04 Dec 2022, 04:21

AHK Write Nth Filename from folder to Text File (iteration)

Post by user99000 » 04 Dec 2022, 04:38

Hello I am trying to write all the odd and then the even filenames to a text file.

Code: Select all

FileOpen(A_MyDocuments "\combine.txt", "w")
Loop, %A_Desktop%\combine\*
 {
  SplitPath, A_LoopFileLongPath
  FileList = %FileList%file '%A_LoopFileLongPath%'`n
 }
 FileAppend, %FileList%, %A_MyDocuments%\combine.txt
Example: 01,02,03,04,05,06,07,08,09.png

Results
file 'C:\Users\Admin\Desktop\combine\01.png'
file 'C:\Users\Admin\Desktop\combine\03.png'
file 'C:\Users\Admin\Desktop\combine\05.png'
file 'C:\Users\Admin\Desktop\combine\07.png'
file 'C:\Users\Admin\Desktop\combine\09.png'
file 'C:\Users\Admin\Desktop\combine\02.png'
file 'C:\Users\Admin\Desktop\combine\04.png'
file 'C:\Users\Admin\Desktop\combine\06.png'
file 'C:\Users\Admin\Desktop\combine\08.png'

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

Re: AHK Write Nth Filename from folder to Text File (iteration)

Post by mikeyww » 04 Dec 2022, 09:09

Welcome to this AutoHotkey forum!

Code: Select all

dir = %A_Desktop%\combine
out = %A_MyDocuments%\combine.txt
FileRecycle, %out%
Loop, 2 {
 n := A_Index
 Loop, Files, %dir%\*.*
 { SplitPath, A_LoopFileName,,,, fnBare
   If fnBare is integer
    If !Mod(fnBare + n, 2)
     FileAppend, %A_LoopFilePath%`n, %out%
 }
}
Run, %out%

user99000
Posts: 4
Joined: 04 Dec 2022, 04:21

Re: AHK Write Nth Filename from folder to Text File (iteration)

Post by user99000 » 05 Dec 2022, 20:40

Thanks for the welcome! sadly I wasn't able to get the code to work.

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

Re: AHK Write Nth Filename from folder to Text File (iteration)

Post by mikeyww » 05 Dec 2022, 20:48

I find that "didn't work" is never an adequate problem statement. What actually happened? What was in your directory? Was the right directory specified? What is in the output file? Did the script run? Did it open your output file at the end?

user99000
Posts: 4
Joined: 04 Dec 2022, 04:21

Re: AHK Write Nth Filename from folder to Text File (iteration)

Post by user99000 » 05 Dec 2022, 22:13

I was able to comment out line 8 and get the code to produce the drives filenames twice in a text document.

Code: Select all

C:\Users\Admin\Desktop\combine\01.png
C:\Users\Admin\Desktop\combine\02.png
C:\Users\Admin\Desktop\combine\03.png
C:\Users\Admin\Desktop\combine\04.png
C:\Users\Admin\Desktop\combine\05.png
C:\Users\Admin\Desktop\combine\06.png
C:\Users\Admin\Desktop\combine\07.png
C:\Users\Admin\Desktop\combine\08.png
C:\Users\Admin\Desktop\combine\09.png
C:\Users\Admin\Desktop\combine\01.png
C:\Users\Admin\Desktop\combine\02.png
C:\Users\Admin\Desktop\combine\03.png
C:\Users\Admin\Desktop\combine\04.png
C:\Users\Admin\Desktop\combine\05.png
C:\Users\Admin\Desktop\combine\06.png
C:\Users\Admin\Desktop\combine\07.png
C:\Users\Admin\Desktop\combine\08.png
C:\Users\Admin\Desktop\combine\09.png

user99000
Posts: 4
Joined: 04 Dec 2022, 04:21

Re: AHK Write Nth Filename from folder to Text File (iteration)

Post by user99000 » 05 Dec 2022, 23:24

Okaii, it's solved, thanks

Code: Select all

FileOpen(A_MyDocuments "\combine.txt", "w")

oddFilenames =
evenFilenames =

Loop, %A_Desktop%\combine\*
{
  SplitPath, A_LoopFileLongPath
  if (Mod(A_Index, 2) != 0)
  {
    oddFilenames = %oddFilenames%file '%A_LoopFileLongPath%'`n
  }
  else
  {
    evenFilenames = %evenFilenames%file '%A_LoopFileLongPath%'`n
  }
}

FileAppend, %oddFilenames%%evenFilenames%, %A_MyDocuments%\combine.txt

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

Re: AHK Write Nth Filename from folder to Text File (iteration)

Post by mikeyww » 06 Dec 2022, 06:03

I like how you used a single loop. Be aware that Loop, FilePattern is deprecated.

If you do not examine the file name, the script could fail to provide the expected output if the file names do not follow a specific pattern in their numeric order, or if non-numeric file names are included.

Explained: Loop, Files

Post Reply

Return to “Ask for Help (v1)”