Page 1 of 1

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

Posted: 04 Dec 2022, 04:38
by user99000
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'

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

Posted: 04 Dec 2022, 09:09
by mikeyww
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%

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

Posted: 05 Dec 2022, 20:40
by user99000
Thanks for the welcome! sadly I wasn't able to get the code to work.

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

Posted: 05 Dec 2022, 20:48
by mikeyww
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?

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

Posted: 05 Dec 2022, 22:13
by user99000
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

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

Posted: 05 Dec 2022, 23:24
by user99000
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

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

Posted: 06 Dec 2022, 06:03
by mikeyww
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