EXAMPLE SOURCE CODES


     This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey. Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.

Found #2 items in Files category

 GetAbsolutePathToFile

By: Kevin Picone Added: January 8th, 2023

Category: All,Files


  GetAbsolutePathToFile


    This function takes a base path and then computes the absolute path from this base from an assumed relative path in the file name





PlayBasic Code:
	BasePath$="D:\MyProjectFolder\Gfx\_examples"
	ink $ff0000
	print BasePath$	
	ink -1

	print ""	
	print  GetAbsolutePathToFile(BasePath$, "Stuff.txt")
	print  GetAbsolutePathToFile(BasePath$, "subfolder/Stuff.txt")
	print  GetAbsolutePathToFile(BasePath$, "../Stuff.txt")
	print  GetAbsolutePathToFile(BasePath$, "../../Stuff.txt")
	print  GetAbsolutePathToFile(BasePath$, "subfolder/../../Stuff.txt")
	
	

	Sync
	waitkey
	

Function GetAbsolutePathToFile(BasePath$, Filename$)

		BasePath$=replace$(BasePath$,"/","\")	
		Filename$=replace$(Filename$,"/","\")	

		Filename$=trim$(Filename$)
		BasePath$=trimright$(BasePath$,"\")+"\"
	
		local Device$=GetDevicename$(Filename$)	 
		if len(Device$)=0
			
			//  if the Filename is relative, we'll try and
			//  build an absolute path from BASE to this Filename
				local Path$=GetFolderName$(Filename$)	 

				 FileName$=BasePath$+Filename$

			// Check for "..\" in path
				if instring(Path$,"..\")
	
					Dim FolderChunks$(50)
					local Count=splittoarray(filename$,"\",FolderChunks$())
					
					local lp,s$
					local Output=0
					
					
					for lp =0 to count-1
						s$=FolderChunks$(lp)
						if s$<>".." 
							if output>=0  then FolderChunks$(Output)=s$
							Output++		
						else
							output--	
						endif	
					next	

					// Put it back together
					Filename$=""
					for lp =0 to output-1
						Filename$+=FolderChunks$(lp)
						if lp<output-1 then Filename$+="\"
					next	
												
				endif 				
	
		endif
	

EndFunction Filename$











   Example output:

CODE:

D:\MyProjectFolder\Gfx\_examples D:\MyProjectFolder\Gfx\_examples\Stuff.txt D:\MyProjectFolder\Gfx\_examples\subfolder\Stuff.txt D:\MyProjectFolder\Gfx\Stuff.txt D:\MyProjectFolder\Stuff.txt D:\MyProjectFolder\Gfx\Stuff.txt









 Get All Files In Folder (Sorted)

By: Kevin Picone Added: December 29th, 2010

Category: All,FIles,Sorting,Search

     This example is built from a few other tidbits on the forums, what it does is returns a sorted list of the files that appear within a folder. The GetAllFilesInFolderSorted() function is recursive, so it'll search folders within this folder, if you don't want this you'll have to manually switch this off within the function.

     This type of function can be handy when writing programs that need to not only locate if a file exists, but how old a file is. For example, I've been using a variation of it to build thumb nail galleries recently, but it's also used in PlayPackager.

Download: Login to Download




Viewing Page [1] of [1]



Want More Source Codes?:



Release Type: The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.

 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]