[BMX] Create CSV files!

Started by Filax, April 09, 2025, 10:01:13

Previous topic - Next topic

Filax

Code: BASIC
' ------------------------------------------------
' Name : Create CSV files
' Date : (C)2025
' Site:https://github.com/BlackCreepyCat
' -----------------------------------------------

Global newDir:String
Global oldDir:String
Global Separator:String="	"

Local MyOutput:TStream = WriteStream("test.csv")
ScanDir(CurrentDir(), True, False, MyOutput)
CloseStream MYOutput

' -----------------------------------
' Function to scan directory structur
' -----------------------------------
Function ScanDir:Int(Path:String, Recursive:Byte = True, Lvl:Byte = False, Stream:TStream)

	If path="" Then path=CurrentDir()
	If Right(path,1)<>"\" Then path=path+"\"
	
	Local Mydir:Byte Ptr = ReadDir(path)
	Local Counter:Int=0
	
	Repeat
		Local File:String=NextFile(mydir)
		
		If File="" Then Exit
		
		Local Filename:String = Path + File
		
		If FileType(Filename) = 1
		
			counter=counter+1
		'	Print ">" + Filename
			
			If Stream <> Null Then
				WriteLine(Stream, StripDir(Filename) + Separator + FileSize(Filename) + Separator + "Octets")
			End If
			
		Else
		
			If recursive=True
				If File <> "." And File <> ".."
				
					If FileType(filename$)=2
						Lvl = Lvl + 1
						
							WriteLine(Stream, Separator)
							WriteLine(Stream, Filename + Separator + "[Folder]")
						
						ScanDir(Filename, True, Lvl, Stream)
				
					EndIf

				EndIf
			EndIf
			
		EndIf
		
	Forever
		
	CloseDir Mydir
	Return Counter
End Function

Function CreateString:String(char:String, num:Int)
	
	Local buf:String

	For Local i:Int = 1 To num
	
		buf = buf + char
	
	Next
	
	Return buf
	
End Function

Scan the current directory and create a CSV file...

Derron

in one case you check
Code: BASIC
If Stream <> Null Then
in the other case you don't.

simply check once at the beginning and avoid scanning the directory at all if it is null (return false or so)

Also you could check if the passed path is even an existing dir (and skip then too - or create an empty csv). no need to close a dir which you failed to open :)



bye
Ron