Page 1 of 1

Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 7:36 am
by marcoagpinto
Heya, guys,

I have a folder full of .csv and .txt files and I want to store each .csv file in an array with the full path to it.

First, I open a file selector so that the user clicks in a file from the folder to set it as the current folder.

But, the:

Code: Select all

Debug "NextDirectoryEntry(1):"+Str(NextDirectoryEntry(1))
returns zero, so, it is not working.

What is wrong with my code?

Code: Select all

Procedure batch_reprocess_open_reprocess_save()
   
   ; Select the .csv source folder
   file$=OpenFileRequester("Select a .csv file to know the source folder", "", "csv (.csv)|*.csv", 0)
   If file$=""
     MessageRequester("Warning!","No file selected.",#PB_MessageRequester_Warning)
     ProcedureReturn #False
   EndIf
   
   
   ; If no .csv file is found in the folder
   flag=ExamineDirectory(1,GetCurrentDirectory(),".csv")
   FinishDirectory(1)
   If flag=#False
     MessageRequester("Warning!","No file found.",#PB_MessageRequester_Warning)
     ProcedureReturn #False
   EndIf
   
   
   ; count the .csv number of files
   counter=0
   flag=ExamineDirectory(1,GetCurrentDirectory(),".csv")
   Debug "ExamineDirectory:"+Str(flag)
   Debug "NextDirectoryEntry(1):"+Str(NextDirectoryEntry(1))
   While NextDirectoryEntry(1)=#True
     counter+1
   Wend
   FinishDirectory(1)
   
   Debug "counter:"+Str(counter)
   Debug "GetCurrentDirectory():"+GetCurrentDirectory()
   
   ; Store all .csv files in the array
   Dim files_to_reprocess$(counter)
   flag=ExamineDirectory(1,GetCurrentDirectory(),".csv")
   For f=1 To counter
     flag=NextDirectoryEntry(1)
     file$=DirectoryEntryName(1)
     files_to_reprocess$(f)=file$
   Next f
   FinishDirectory(1)
   SortArray(files_to_reprocess$(),#PB_Sort_Ascending|#PB_Sort_NoCase,1,counter)
   
   
   For f=1 To counter
     Debug Str(f)+"->"+files_to_reprocess$(f)
   Next f
   
 EndProcedure

Thanks!

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 7:52 am
by jacdelad
Hi Marco,
NextDirectoryEntry is used with ExamineDirectory, not the file selector. You want to use NextSelectedFilename.

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 8:01 am
by marcoagpinto
jacdelad wrote: Tue Aug 22, 2023 7:52 am Hi Marco,
NextDirectoryEntry is used with ExamineDirectory, not the file selector. You want to use NextSelectedFilename.
Heya,

Thank you, I will test it soon after a short break!

PureBasic and its community is the best one can find.

I have it written in my thesis so that everyone knows after I finish the course.

People will say if I pass in the exam:
“That schizophrenic guy coded a brilliant software using PureBasic.”

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 9:56 am
by marcoagpinto
Heya,

If in the file selector I start selecting the files from below to above it gets the wrong filenames:

Code: Select all

 Procedure batch_reprocess_open_reprocess_save()
   
   ; Select the .csv source folder
   file$=OpenFileRequester("Select the .csv files to process","","csv (.csv)|*.csv",0,#PB_Requester_MultiSelection)
   If file$=""
     MessageRequester("Warning!","No file selected.",#PB_MessageRequester_Warning)
     ProcedureReturn #False
   EndIf
   
   
   ; Store all .csv files selected in the array
   counter=1
   Dim files_to_reprocess$(1)
   files_to_reprocess$(1)=file$
   Repeat
     file$=NextSelectedFileName() 
     If file$<>""
       counter+1
       ReDim files_to_reprocess$(counter)
       files_to_reprocess$(counter)=file$
     EndIf
   Until file$=""
   SortArray(files_to_reprocess$(),#PB_Sort_Ascending|#PB_Sort_NoCase,1,counter)
   
   Debug "counter:"+Str(counter)
   For f=1 To counter
     Debug Str(f)+"->"+files_to_reprocess$(f)
   Next f
   
 EndProcedure
What am I doing wrong?

Thanks!

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 10:23 am
by idle

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 10:34 am
by marcoagpinto
idle wrote: Tue Aug 22, 2023 10:23 am Try this.
viewtopic.php?t=51878
Idle, thank you for your help, but your code is huge, and I can't understand most of it.

Can't it just be done in one line or two?

Write a 1000 lines just to open the selected files?

:cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry:

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 11:03 am
by spikey
marcoagpinto wrote: Tue Aug 22, 2023 9:56 am If in the file selector I start selecting the files from below to above it gets the wrong filenames:
It seems to work ok for me. What exactly makes you think you aren't getting the right results?

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 11:07 am
by marcoagpinto
spikey wrote: Tue Aug 22, 2023 11:03 am
marcoagpinto wrote: Tue Aug 22, 2023 9:56 am If in the file selector I start selecting the files from below to above it gets the wrong filenames:
It seems to work ok for me. What exactly makes you think you aren't getting the right results?
Well,

You have 10 .csv files and 10 .txt files in a folder.

When the file selector opens with the .csv filter, you select file 9 and then file 6 using the CONTROL KEY, and it shows different files in the DEBUG command.

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 11:35 am
by spikey

Code: Select all

 Directory of P:\Forum Replies\NextSelectedFIle Problem

[.]          [..]         file1.csv    file1.txt    file10.csv   file10.txt   file11.txt   file12.txt   file2.csv
file2.txt    file3.csv    file3.txt    file4.csv    file4.txt    file5.csv    file5.txt    file6.csv    file6.txt
file7.csv    file7.txt    file8.csv    file8.txt    file9.csv    file9.txt
              22 File(s)            198 bytes
               2 Dir(s)  690,459,152,384 bytes free

Code: Select all

counter:2
1->P:\Forum Replies\NextSelectedFIle Problem\file6.csv
2->P:\Forum Replies\NextSelectedFIle Problem\file9.csv
I've checked it on Win 11 X64 6.02 LTS C and ASM backend and 6.03 B5 C and ASM backend. What's different about your setup and results?

Re: Getting files one by one from folder with specific file extension

Posted: Tue Aug 22, 2023 12:02 pm
by marcoagpinto
Now it is working!

It is very strange.

There seems to be some random issues happening.

I don't know what is going on.

Re: Getting files one by one from folder with specific file extension

Posted: Thu Aug 24, 2023 4:27 am
by electrochrisso
Been a while since I been on the Forum, getting back into Purebasic programming after some time.

Here is my two cents worth. :D

Code: Select all

NewList FileList.s()
Path$ = OpenFileRequester("Select a File", GetHomeDirectory(), "All Files|*.*", 1)
PathPart$ = GetPathPart(Path$)
Extension$ = GetExtensionPart(Path$)
If ExamineDirectory(0, PathPart$, "*."+Extension$)
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      AddElement(FileList())
      FileList() = DirectoryEntryName(0)
    EndIf
  Wend
  FinishDirectory(0)
  ForEach FileList()
    Debug FileList()
  Next
  NumOfFiles$ = Str(ListSize(FileList()))+" Files of type "+Extension$+" in "+PathPart$
  Debug""
  Debug NumOfFiles$
EndIf

Re: Getting files one by one from folder with specific file extension

Posted: Thu Aug 24, 2023 11:12 am
by boddhi
wrote:

Code: Select all

Path$ = OpenFileRequester("Select a file", GetHomeDirectory(), "All Files|*.*", 1)
PathPart$ = GetPathPart(Path$)
Extension$ = GetExtensionPart(Path$)
If ExamineDirectory(0, PathPart$, "*."+Extension$)
You can use PathRequeter() too :

Code: Select all

Path$ = PathRequester("Select a directory", GetHomeDirectory())
If Path$
  If ExamineDirectory(0, Path$, "*.csv")
:wink:

Re: Getting files one by one from folder with specific file extension

Posted: Thu Aug 24, 2023 11:28 am
by marcoagpinto
Thank you, guys!

:twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: