December 04, 2020, 11:43:02 AM

Author Topic: [bb] FixPath by Picklesworth [ 1+ years ago ]  (Read 573 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] FixPath by Picklesworth [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : FixPath
Author : Picklesworth
Posted : 1+ years ago

Description : Creates every nonexistent folder in a file path. This function is useful for saving files.
It saves you from about 5 minutes of typing, which is always good :)

Thanks to Perturbatio for the ExtractFilePath() function.


Code :
Code: BlitzBasic
  1. Function FixPath(path$)
  2. ;Creates every missing folder in a path. (Fills in gaps in a filepath)
  3. ;Handy for file saving.
  4. ;Written by Dylan McCall (Mr. Picklesworth)
  5.         path$=extractfilepath(path$)
  6.         Local c=1,pathTo$
  7.         Repeat
  8.                 slash=Instr(path$,"",c)
  9.                 If slash=0
  10.                         If c>=Len(path$)+1
  11.                                 Exit
  12.                         Else
  13.                                 slash=Len(path$)+1
  14.                         EndIf
  15.                 EndIf
  16.                
  17.                 folder$=Mid(path$,c,slash-c)
  18.                 If FileType(pathTo$+folder)=0 Then CreateDir(pathTo$+folder)
  19.                 c=slash+1
  20.                 pathTo$=pathTo$+folder+""
  21.         Forever
  22.         Return 1
  23. End Function
  24.  
  25. ;FUNCTION ExtractFilePath
  26. ;Accepts a filepath with filename and returns only the path
  27. ;i.e. pass c:   emp     est.txt the return value will be c:     emp
  28. Function ExtractFilePath$(sFilePath$) ;Written by Perturbatio
  29.     ;LOCAL VARS
  30.     Local iStartPos% = 0
  31.     Local iSearchPos% = 0
  32.     Local iFilePathLength = 0
  33.     Local sFileExt$ = ""
  34.    
  35.     ;BEGIN FUNCTION CODE
  36.     iFilePathLength = Len(sFilePath$)
  37.     iSearchPos% = iFilePathLength
  38.    
  39.     While (iStartPos% < 1) And (iSearchPos% > 1)
  40.        
  41.         iStartPos% = Instr(sFilePath$, "", iSearchPos%)
  42.         iSearchPos% = iSearchPos% - 1
  43.        
  44.     Wend
  45.    
  46.     If iStartPos = 0 Then ;if the filepath contains no backslashes
  47.         sFileExt$ = sFilePath$
  48.     Else
  49.         sFileExt$ = Left$(sFilePath$, iStartPos%)
  50.     EndIf
  51.    
  52.    
  53.     Return sFileExt$    
  54. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal