Ooops
October 28, 2020, 05:47:35 AM

Author Topic: [bmx] File Seeker.. Via a Fil  (Read 161 times)

Offline Hardcoal

  • Sr. Member
  • ****
  • Posts: 472
[bmx] File Seeker.. Via a Fil
« on: July 14, 2020, 09:33:58 PM »
Hi.. First time i post code here..
I simply missed this section..
now im gonna post some stuff.. hope will be helpful to others..
cheers

This process finds a file under a path you give it..
it only brings one reply.. but it can easily can be modified to return all found paths..
I just dont need that atm so I didnt do it.

The Var in the function Called "SerialNumber" means its number in the search..
so if its 1, it will return the first path he finds.. if its 2 than the second.. and so on..
I just didnt have a better name for it ;)

Code: [Select]

Strict

'Example
 Print FindFilePath("test.bmx")

'----------------Function----------------;

Global SerialCounter

Function FindFilePath:String(Filename:String, RootURL:String = "", SerialNumber = 1)
If Trim(Filename) = "" Then Return
SerialCounter = 0
Filename = Trim(Lower(Filename))
If Trim(RootURL) = "" Then RootURL = CurrentDir() + "\"
AddSlashToURLIfNeeded(RootURL)
Return FindFilePath_Req_Int(RootURL, Filename, SerialNumber)
End Function

Function FindFilePath_Req_Int:String(RootURL:String = "", Filename:String, SerialNumber = 1)
Local FileFound_Flg

If FileExists(RootURL + Filename) Then FileFound_Flg = True ; SerialCounter = SerialCounter + 1

If FileFound_Flg = True And SerialNumber = SerialCounter Then
Return RootURL + Filename
Else
Local DirList:TList, Str:String, Answer:String
DirList = ReadDirectory(RootURL)
For Str = EachIn DirList
If IsFolder(RootURL + Str + "\") Then
Answer = FindFilePath_Req_Int(RootURL + Str + "\", Filename, SerialNumber)
If Answer <> "" Then Return Answer
End If
Next
End If

End Function

'----------Externals-----------'

Function AddSlashToURLIfNeeded:String(URL:String)
If IsSlash(Right(URL, 1)) = False Then URL = URL + "\" 'Adds \ if needed
Return URL
End Function

Function FileExists(URLAndFilename:String = "")  'change name to fileexistsinfolder
Local TempStream:TStream
TempStream = OpenStream(URLAndFilename, False, False)
If TempStream <> Null Then
CloseStream(TempStream)
Return True
End If
End Function

Function ReadDirectory:TList(URL:String, RemoveExtention = False)
Local ALIST:TList = CreateList(), T:String, Files:String[]

Files = LoadDir(URL)

For T:String = EachIn Files

If RemoveExtention = False Then
ListAddLast(ALIST, T)
Else
ListAddLast(ALIST, StripAll(T))
End If

Next

Return AList
End Function

Function IsFolder(URL:String)   'works with or without slash
If ReadDir(URL) > 0 Then Return True
End Function

Function IsSlash(Char:String)
Return char = "/" Or char = "\"
End Function


Go with the Flow

Offline Derron

  • Hero Member
  • *****
  • Posts: 3236
Re: [bmx] File Seeker.. Via a Fil
« Reply #1 on: July 15, 2020, 06:04:19 AM »
Checkout FileType(). .. will save you some functions.

AddSlashToURLIfNeeded() ... If you use ExtractDir() you could skip that too (but yours will do too).



Bye
Ron

Offline Hardcoal

  • Sr. Member
  • ****
  • Posts: 472
Re: [bmx] File Seeker.. Via a Fil
« Reply #2 on: July 15, 2020, 10:05:28 AM »
Thanks for corrections.. Derron
Feel free to modify/improve functions and repost..
I do things on the base of what I know of.
I bet many time there are certain commands I'm not aware of,
that would save me lots of time in many of my processes

I have lots file commands that ive made for my own use..

Code: [Select]

Type FileCommands_Class

Const Enter_Str:String = "~n"

'--Get--'
 
Method GetNewFilename:String(URL:String, WantedFilename:String, NumberIsAttached = False, IgnoreLackOfExtention = False)
    Local Ext:String

    Ext = ExtractExt(WantedFilename)

If IgnoreLackOfExtention = False And Ext = "" Then Notify ("Filename must have an Extention") ; Return
    If Trim(WantedFilename) = "" Then Notify ("No Filename Given") ; Return
    If IsValidFilename(WantedFilename, True) = False Then Notify("Invalid Signs on Filename") ; Return

    WantedFilename = StripExt(WantedFilename)

    Repeat

        If FileExsists(URL + WantedFilename + "." + Ext) Then

            Local Counter = GetNumberStrngFromName(WantedFilename).ToInt()

            Counter = Counter + 1

            RemoveNumberFromName(WantedFilename, NumberIsAttached)

   'Add The Counter
If NumberIsAttached = False Then
            WantedFilename = WantedFilename + " " + Counter
Else
WantedFilename = WantedFilename + Counter
End If

        Else

            Return StripExt(WantedFilename) + "." + Ext
        End If

    Forever

End Method

Method GetFileByNum:String(Directory:String, Num = 1)
Local Dir, Counter, CrntFile:String
Dir = ReadDir(Directory)
Repeat
Counter = Counter + 1
CrntFile = NextFile(Dir)
If Counter = Num Then
CloseDir(Dir)
Return CrntFile
End If
Forever
End Method

Method GetWordFromDir:String(Dir:String, LocationInDir = 1) 'need to use tdir too..
Local I, Midder:String, Word:String, LocationCounter = 1

For I = 1 To Len (Dir)

Midder = Mid(Dir, I, 1)

If LocationCounter < LocationInDir Then
If Midder = "/" Or Midder = "\" Then LocationCounter = LocationCounter + 1
Else
If Midder = "/" Or Midder = "\" Or I = Len(Dir) Then
If Midder <> "/" And Midder <> "\" Then Word = Word + Midder
Return Trim(Word) '<--Answer
Else
Word = Word + Midder
End If
End If

Next

End Method

Method GetFolderNameFromCurrentDir:String()
Return ExtractRightFolderNameFromPath (CurrentDir())
End Method

Method GetFolderNameFromDir:String(Dir:String)
Local SaveCurrentDir:String = CurrentDir(), Folder:String
ChangeDir(Dir)
Folder = GetFolderNameFromCurrentDir()
ChangeDir(SaveCurrentDir)
Return Folder
End Method

Method GetFilenamesListOfASpecificType:TList(URL:String, AType:String)  'Like.. exe ..
Local files:String[], FilesList:TList = CreateList(), T:String
files = LoadDir(URL)
For t:String = EachIn files
If ExtractExt(t) = AType Then ListAddLast(FilesList, t)
Next
Return FilesList
End Method

Method GetDesktopURL:String()
Return (getenv_("USERPROFILE") + "\Desktop") + "/"
End Method

'--Add--'

Method AddSlashToURLIfNeeded:String(URL:String)
If IsSlash(Right(URL, 1)) = False Then URL = URL + "\" 'Adds \ if needed
Return URL
End Method

Method AddFileExtentionIfThereIsnt:String(File:String, ExtentionWithoutPoint:String) 'For Example A png file without .png will be added a .png
For Local I = Len(File) To 1 Step - 1
If Mid(File, I, 1) = "." Then Return
Next
Return File + "." + ExtentionWithoutPoint
End Method

'--Directory--'

Method FolderIsEmpty(URL:String) 'under Test
Local Files:String[]
Files = LoadDir(URL)
If Files.Length = 0 Then Return True
End Method

Method ReadDirectory:TList(URL:String, RemoveExtention = False)
Local ALIST:TList = CreateList(), T:String, Files:String[]

Files = LoadDir(URL)

For T:String = EachIn Files

If RemoveExtention = False Then
ListAddLast(ALIST, T)
Else
ListAddLast(ALIST, ExtractFilenameWithoutExtention(T))
End If

Next

Return AList
End Method

Method DirectoryExsists(Dir:String)
Local TempDirHandle
TempDirHandle = ReadDir(Dir)
If TempDirHandle > 0 Then
CloseDir(TempDirHandle)
Return True
End If
End Method

Method MyCopyDir(SourceURL:String, DestURL:String, PrintRemarks = True)
Local Answer

If IsSlash(Right(DestURL, 1)) Then DestURL = Left(DestURL, Len(DestURL) - 1)

Answer = DirectoryExsists(SourceURL)
If Answer = False and PrintRemarks Then Print "Directory Does not Exist:" + SourceURL
Answer = CopyDir(SourceURL, DestURL)
If Answer = 0 And PrintRemarks Then Print "CopyDir From: " + SourceURL + " To: " + DestURL + " Was Unsucceful"

Return Answer
End Method

  'Delete
 
  Method ClearFolder(URL:String)
DeleteDir(URL, True)  'Recursive Important!
CreateDir(URL, True)
End Method

Method DeleteFilesFromFolderByExtention_Req(URL:String, Extention:String, Quit = False)
Local CrntDir:TList, Strng:String, SURL:String

   'Quiter
If KeyDown(KEY_ESCAPE) Then Print "Quitting" ; Quit = True

If Quit = True Then
Return True
Else

CrntDir = ReadDirectory(URL)

RemoveSlashesFromDirectoryEnding(URL)

For Strng = EachIn CrntDir
SURL = URL + Strng
If ExtractExt(Strng) = Extention Then
DeleteFile(SURL)
Else If IsFolder(SURL) Then
Quit = DeleteFilesFromFolderByExtention_Req(SURL + "\", Extention)
If Quit = True Then Return True
End If
Next

End If

End Method

Method DeleteFileOrFolderWithThisName_Req(URL:String, Filename:String)
Local CrntDir:TList, Strng:String, FTD:String = "Deletexx"

If Trim(Filename) = "" Then Return

RenameFile(URL + Filename, URL + FTD)
DeleteDir(URL + FTD, True)
DeleteFile(URL + FTD)

CrntDir = ReadDirectory(URL)

For Strng = EachIn CrntDir
If IsFolder(URL + Strng + "\") Then DeleteFileOrFolderWithThisName_Req(URL + Strng + "\", Filename)
Next

End Method

Method DeleteAllFilesThatArntDirectories(URL:String)
Local DR:TList, Strng:String
DR = ReadDirectory(URL)
For Strng = EachIn DR
If IsFolder(URL + Strng) = False Then
DeleteFile(URL + Strng)
End If
Next
End Method

Method DeleteFolder(Path:String) 'No need Slash in the end.
DeleteDir(Path)
End Method

   '--Checks--'
   
Method SameFile_Probably:String(FirstURL:String, SecondURL:String)
Local A = FileSize(FirstURL)
Local B = FileSize(SecondURL)
If A > 0 And A = B Then
If ExtractFilenameFromURL(FirstURL) = ExtractFilenameFromURL(SecondURL) Then Return True
End If
End Method
   
    Method IsFolder(URL:String)   'works with or without slash
Return FileType(URL) = 2
   'If ReadDir(URL) > 0 Then Return True
End Method

Method IsImage(Filename:String)
Select Lower(ExtractExt(Filename))
Case "jpg", "bmp", "png"
Return True
End Select
End Method

Method Is3D(Filename:String)
Select Lower(ExtractExt(Filename))
Case "3ds", "fbs", "b3d", "obj", "x"
Return True
End Select
End Method

Method IsSound(Filename:String)
Select Lower(ExtractExt(Filename))
Case "wav", "mp3", "ogg"
Return True
End Select
End Method

Method SameURL(URL1:String, URL2:String, RemoveSlashAtEnd = True)
URL1 = NeatURL(URL1)
URL2 = NeatURL(URL2)
If RemoveSlashAtEnd
If Right(url1, 1) = "/" Or Right(url1, 1) = "\" Then url1 = Left(url1, Len(url1) - 1)
If Right(url2, 1) = "/" Or Right(url2, 1) = "\" Then url2 = Left(url2, Len(url2) - 1)
End If
Return URL1 = URL2
End Method

   'File Exsists
Method FileExists(URLAndFilename:String = "")  'change name to fileexistsinfolder
Local TempStream:TStream
TempStream = OpenStream(URLAndFilename, False, False)
If TempStream <> Null Then
CloseStream(TempStream)
Return True
End If
End Method

Method FolderExists(URL:String)
Return ReadDir(URL) > 0
End Method

Method IsSlash(Char:String)
Return char = "/" Or char = "\"
End Method

Method URLIsUnderThisURL(MainURL:String, SubURL:String) 'Needs Check! Not In Use ATM..  Checks if URL is insie the Parent URL Exmp: If Parent = Moshe/Dada And URL = Dada.   Answer will be True
Local Limit

   'Neat
MainURL = NeatURL(MainURL)
SubURL = NeatURL(SubURL)

Repeat

If Len(SubURL) < Len(MainURL) Then Return

If SameURL(MainURL, SubURL) Then Return True

SubURL = GoBackInDir(SubURL, True) 'This may need check
SubURL = NeatURL(SubURL)

If SubURL = "" Then Return

Limit = Limit + 1
If Limit = 100 Then Exit

Forever

End Method

Method IsValidFilename(Text:String, IgnoreLackOfFileType = False) 'a valid file name has also ending like .png
Local I, Counter, Counter2, Counter3

   'Check that this chars dont appear in the name
If Text <> "" Then If ChrarectersFound_si(Text, "\/:*?<>|") = False And IsAQuotationMark(Text) = False Then

   'Checks if the name has a dot and also If on both sides it has Valid Text
For I = 1 To Len(Text)
If Mid(Text, I, 1) = "." Then
counter = counter + 1
Else
If counter = 1 Then Counter2 = Counter2 + 1
If Counter = 0 Then Counter3 = Counter3 + 1
End If
Next

If Counter > 0 Then
If Counter = 1 And Counter2 > 0 And Counter3 > 0 Then Return True
Else
Return IgnoreLackOfFileType
End If

End If

End Method

'--Neat--'

Method NeatURL:String(url:String ) 'Make the URL Lower Case Trimmed and Slash to \ only
Return ChangeSlashesToLeftOnURL(LowerTrim(url))
End Method

'--Remove--'
   
    Method RemoveSlashesFromDirectoryEnding:String(URL:String)
Repeat
If Right(URL, 1) = "/" Or Right(URL, 1) = "\" Then
URL = Left(URL, Len(URL) - 1)
Else
Return URL
End If
Forever
End Method

'--Debug--'
   
    Method PrintDirFromURL(URL:String, OnlyFolders = False)
Local DR:TList, Strng:String
DR = ReadDirectory(URL)
For Strng = EachIn DR
If IsFolder(URL + Strng) Then
Print Strng + " [Folder]"
Else
Print Strng
End If
Next
End Method

'--Switch--'

Method SwitchWordInDir:String(Dir:String, NewWord:String, LocationInDir = 1)  'not in use atm
Local DirLst:TList
DirLst = TakeApartDirToList(Dir)
DirLst = SwitchWordInDirList(DirLst, NewWord)
Return TurnTListDirToDir(DirLst)
End Method

Method TakeApartDirToList:TList(Dir:String)
Local I, DirLst:TList = CreateList(), Word:String, Midder:String
For I = 1 To Len (Dir)
Midder = Mid(Dir, I, 1)
If Midder = "/" Or Midder = "\" Or I = Len(Dir) Then
If Midder <> "/" And Midder <> "\" Then Word = Word + Midder
ListAddLast(DirLst, Word)
Else
Word = Word + midder
End If
Next
Return DirLst
End Method

Method SwitchWordInDirList:TList(DirList:TList, NewWord:String, LocationInList = 1) 'this is command "Replace" .. something on blitzmax
Local NewLst:TList = CreateList(), Strng:String, LocationCntr
For Strng = EachIn DirList
LocationCntr = LocationCntr + 1
If LocationCntr = LocationInList Then
ListAddLast(NewLst, NewWord)
Else
ListAddLast(NewLst, DirList.ValueAtIndex(LocationCntr - 1))
End If
Next
Return NewLst
End Method

Method TurnTListDirToDir:String(TlistDir:TList)
Local BuildDir:String, Strng:String, Cntr
For Strng = EachIn TlistDir
Cntr = Cntr + 1
BuildDir = BuildDir + Strng
If Cntr < TlistDir.Count() Then BuildDir = BuildDir + "/"
Next
Return BuildDir
End Method

Method ChangeDirDownUntilFolderFound:String(FolderName:String) 'Changes CurrentDir
Local DepthLimit = 128, DepthCounter

While DepthCounter < DepthLimit
DepthCounter = DepthCounter + 1
If FolderName = GetFolderNameFromCurrentDir() Then Exit
ChangeDir("..\")
Wend

Return CurrentDir()
End Method

  '--Change--'

Method ChangeSlashesToLeftOnURL:String(url:String)   'Changes all Slashes in URL into a \ Slash
Local Char:String, I, NewDir:String
For I = 1 To Len(URL)
Char = Mid(URL, i, 1)
If Char = "/" Then
NewDir = NewDir + "\"
Else
NewDir = NewDir + Char
End If
Next
Return Trim(Lower(NewDir))
End Method

   '--Extract--'

       'i think stripall does the job better
Method ExtractFilenameFromURL:String(FileNameAndPath:String, ReturnWithoutFileEnding = False, ReturnEvenIfNoFileEnding = True) 'Extracts with Extention 'Lola/pupu/kakam.png 'return: kakam.png 'need to check ExtractFilenameFromPath maybe its not ok!!
Local I, MidFNA:String, LenFNA, MaybeFileName:String

   'Return of no file extention
If ExtractExt(FileNameAndPath) = "" And ReturnEvenIfNoFileEnding = False Then Return

LenFNA = Len(FileNameAndPath)

For I = LenFNA To 1 Step - 1

MidFNA = Mid(FileNameAndPath, I, 1)

If MidFNA = "/" Or MidFNA = "\" Then

MaybeFileName = Right(FileNameAndPath, LenFNA - I)

If ReturnWithoutFileEnding = True Then MaybeFileName = ExtractFilenameWithoutExtention(MaybeFileName)

Return MaybeFileName

End If

Next

Return FileNameAndPath
End Method

Method ExtractPath:String(FileNameAndPath:String)  'If filename does not have file ending [.xxx] then this command thinks the path has no filename and returns the same result
Return ExtractDir(FileNameAndPath)
End Method

Method ExtractRightFolderNameFromPath:String(URL:String) 'Example: Dada\mada\ will reutrn mada. Dada\mada will also return mada
Local I, LenCurrentDir, BuildWord:String, MidChr:String

LenCurrentDir = Len(URL)

   'Remove Last Slash
If IsSlash(Right(URL, 1)) Then URL = Left(URL, Len(URL) - 1)

   'Goes back until encountering Slash
For I = LenCurrentDir To 0 Step - 1
MidChr = Mid(URL, I, 1)
If IsSlash(MidChr) Or I = 0 Then
Exit
Else
BuildWord = MidChr + BuildWord
End If
Next

Return BuildWord
End Method

Method ExtractFilenameWithoutExtention:String(FileName:String) 'Exmaple:"Moshe.png" Return:"Moshe"
Return StripAll(FileName)
End Method

Method ExtractFileExtention:String(FileName:String) 'Like Wav,Jpg ...
Return Lower(ExtractExt(FileName))
End Method

'--Rename--'

Method RenameFile_fc(File:String, URL:String, NewName:String) 'not sure works well.. with folders..
RenameFile(URL + File, URL + NewName)
End Method

Method RenameFolder:String(UR:String, NewName:String) 'You cant rename a folder it its being used by an open program..
Local KeptDir:String, FolderName:String, NewDir:String, Answer

KeptDir = CurrentDir()

FolderName = ExtractRightFolderNameFromPath(UR)

UR = GoBackInDir(UR)

ChangeDir(UR)

NewDir = UR + "/" + NewName

Answer = RenameFile(FolderName, NewName)

ChangeDir(KeptDir)

If Answer = True Then Return NewDir
End Method

'--Return--'

Method ReturnParentPath:String(FilePath:String) 'not in use atm
Local I, LenCurrentDir
LenCurrentDir = Len(FilePath)
For I = LenCurrentDir To 1 Step - 1
If (Mid(FilePath, I, 1) = "\" Or Mid(FilePath, I, 1) = "/") And I <> LenCurrentDir Then Exit
Next
Local Answer:String = Left(FilePath, LenCurrentDir - (LenCurrentDir - I))
If Answer <> "" Then Answer = Answer + "\"
Return Answer
End Method

'--GoBack--'

Method GoBackInDir:String(Dir:String, AddSlash = False) 'Dir is something without a filename like Danny/Moshe/Pipi/
If IsSlash(Right(Dir, 1)) Then Dir = Left(Dir, Len(Dir) - 1) 'Remove Last Slash
Dir = ExtractDir(Dir)
If Dir <> "" And AddSlash Then Dir = Dir + "\"
Return NeatURL(Dir)
End Method

Method GoBackInCurrentDirUntilEncounteringFolder:String(FolderName:String)
Local CrntDir:String = CurrentDir(), NewDir:String  'not in use
ChangeDirDownUntilFolderFound(FolderName)
Newdir = CurrentDir()
ChangeDir(crntdir)
Return Newdir
End Method

'--Clear--'

Method ClearFile(URL:String)
Local Strm:TStream
Strm = WriteStream(URL)
CloseStream Strm
End Method

'--Move--'

Method MoveFile(SrcURL:String, DSTURL:String)
CopyFile(SrcURL:String, DSTURL:String)
DeleteFile(SrcURL:String)
End Method

'--TextFile Editing--'

Method AddLineToFile(FileURL:String, Line:String)  'Goes Down in Line on a String "~n"
Local LoadedText:String
If FileExists(FileURL) = False Then Return
LoadedText = LoadText(FileURL)
LoadedText = LoadedText + Enter_Str + Line
SaveText(LoadedText, FileURL)
End Method

Method ReplaceTextInFile:String(FileURL:String, TextToReplace:String, ReplaceWith:String)
Local LoadedText:String, NewText:String
If FileExists(FileURL) = False Then Return
LoadedText = LoadText(FileURL)
NewText = ReplaceText(LoadedText, TextToReplace, ReplaceWith)
SaveText(NewText, FileURL)
End Method

Method ReturnAnUnusedFileNameInFolder:String(WantedURL:String, WantedFileName:String)
Local NewFilename:String, Counter, NameWithoutExt:String
NewFilename = WantedFileName
NameWithoutExt = ExtractFilenameWithoutExtention(NewFilename)
While FileExists(WantedURL + NewFilename)
Counter = Counter + 1
NewFilename = NameWithoutExt + " " + Counter + "." + ExtractExt(WantedFileName)
Wend
Return NewFilename
End Method

'--Externals--'

Method RemoveSentenceSpaces:String(Text:String, AlsoLower = False) 'Remove Spaces and Tabs From a Sentence..
Local I, NewText:String, Midder:String
Text = Trim(Text)
For I = 1 To Len(Text)
Midder = Mid(Text, I, 1)
If SpaceOrTab(Midder) = False Then
NewText = NewText + Midder
End If
Next
If AlsoLower Then NewText = Lower(NewText)
Return NewText
End Method

Method SpaceOrTab(Char:String)
Return Char = " " Or Char = Chr(9) Or Asc(Char) = 30  'not sure why i need asc 30
End Method

Method LowerTrim:String(AString:String)
Return Trim(Lower(AString))
End Method

Method ChrarectersFound_si(Text:String, ChrsList:String)  'Char Is
Local I,J,LenChrs,CurrentChr$

LenChrs=Len(ChrsList)

For I=1 To LenChrs
CurrentChr=Mid(ChrsList,I,1)
For J=1 To Len(Text)
If lower(Mid(Text,J,1))=lower(CurrentChr) Then Return True
Next
Next

End Method

Method IsAQuotationMark(FileName:String) 'Char 34 "
Local I
For I = 1 To Len(FileName)
If Asc(Mid(FileName, I, 1)) = 34 Then Return True
Next
End Method

Field SerialCounter

Method FindFilePath:String(Filename:String, RootURL:String = "", SerialNumber = 1)
If Trim(Filename) = "" Then Return
SerialCounter = 0
Filename = Trim(Lower(Filename))
If Trim(RootURL) = "" Then RootURL = CurrentDir() + "\"
AddSlashToURLIfNeeded(RootURL)
Return FindFilePath_Req_Int(RootURL, Filename, SerialNumber)
End Method

Method FindFilePath_Req_Int:String(RootURL:String = "", Filename:String, SerialNumber = 1)
Local FileFound_Flg

If FileExists(RootURL + Filename) Then FileFound_Flg = True ; SerialCounter = SerialCounter + 1

If FileFound_Flg = True And SerialNumber = SerialCounter Then
Return RootURL + Filename
Else
Local DirList:TList, Str:String, Answer:String
DirList = ReadDirectory(RootURL)
For Str = EachIn DirList
If IsFolder(RootURL + Str + "\") Then
Answer = FindFilePath_Req_Int(RootURL + Str + "\", Filename, SerialNumber)
If Answer <> "" Then Return Answer
End If
Next
End If

End Method

'-------------------------Sub Functions--------------------------------------'

Method FileExsists(url:String = "")
    Local TempStream:TStream
    TempStream = OpenStream(url, False, False)
    If TempStream <> Null Then
        CloseStream(TempStream)
        Return True
    End If 
End Method

'---- Externals --'

Method GetNumberStrngFromName:String(Name:String, ReturnWithoutTrim = False)  'Returns the Right number but ignore numbers that are attached to the name. Example "Moshe34 12" will return 12. the answer is in string that includes the spaces.
    Local I, MidName:String, NumbeBldrStartedFlg

    For I = Len(Name) To 1 Step - 1
        MidName = Mid(Name, I, 1)
        Select True
            Case SpaceOrTab(MidName)
                If NumbeBldrStartedFlg = True And ReturnWithoutTrim = False Then Exit
            Case IsNumber(MidName)
                NumbeBldrStartedFlg = True                          'Indicates Number building start
            Default                                                 'If its not a number and not A Space
                Exit                                                'Anything else from Right, It returns. (concidered as part of the name)
        End Select
    Next

    Local RgtName:String
    RgtName = Right(Name, Len(Name) - I)

   'Return
    If ReturnWithoutTrim = True Then
        Return RgtName
    Else
        Return LowerTrim(RgtName)
    End If

End Method

Method IsNumber(Sign:String)          'should be called is number chr..
    Local I
    Const NumberStr:String = "0123456789"   
    If Len(Sign) > 1 Then Return - 1
    For I = 1 To Len NumberStr
        If Sign = Mid(NumberStr, I, 1) Then Return True
    Next
End Method

Method RemoveNumberFromName(Filename:String Var, AlsoIfAttached = False)
    Local I, Char:String, NewName:String

    Trim(Filename)

    For I = 1 To Len(Filename)

        Char = Mid(Filename, I, 1)

   'Return
        If SpaceOrTab(Char) Then
            Filename = NewName
            Return
        Else
If AlsoIfAttached = True And IsNumber(Char) = True Then Filename = NewName ; Return
            NewName = NewName + Char
        End If

    Next

    Filename = NewName

End Method

Method ReplaceText:String(SourceText:String, TextToReplace:String, ReplaceWith:String)
Local LenSourceText, I, BuiledAnswer:String
LenSourceText = Len(TextToReplace)
For I = 1 To Len(SourceText)
If Mid(SourceText, I, LenSourceText) = TextToReplace Then
BuiledAnswer = BuiledAnswer + ReplaceWith
I = I + Len(TextToReplace) - 1
Else
BuiledAnswer = BuiledAnswer + Mid(SourceText, I, 1)
End If
Next
Return BuiledAnswer
End Method

Method LoadText_Mine:String(URL:String)  'LoadText stops the program when file is not found. so better use this..
Local Stream:TStream = OpenStream(URL), Text:String
If Stream = Null Then Return
Repeat
Text = Text + ReadLine(Stream) + "~n"
Until Eof(Stream)
Return Text
End Method

End Type
Go with the Flow

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal