Getting files one by one from folder with specific file extension

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Getting files one by one from folder with specific file extension

Post 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!
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post by jacdelad »

Hi Marco,
NextDirectoryEntry is used with ExamineDirectory, not the file selector. You want to use NextSelectedFilename.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

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

Post 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.”
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

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

Post 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!
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

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

Post by idle »

User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

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

Post 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:
User avatar
spikey
Enthusiast
Enthusiast
Posts: 771
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

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

Post 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?
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

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

Post 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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 771
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

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

Post 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?
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

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

Post 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.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

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

Post 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
PureBasic! Purely the best 8)
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

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

Post 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:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
marcoagpinto
Addict
Addict
Posts: 1054
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

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

Post by marcoagpinto »

Thank you, guys!

:twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted: :twisted:
Post Reply