一些常用的vb函式 過程 操作FSO方面

2021-04-02 12:18:05 字數 4621 閱讀 4651

'此函式從字串中分離出路徑

public function parsepath(spathin as string) as string

dim i as integer

for i = len(spathin) to 1 step -1

if instr(":/", mid$(spathin, i, 1)) then exit for

next

parsepath = left$(spathin, i)

end function

'此函式從字串中分離出檔名

public function parsefilename(sfilein as string) as string

dim i as integer

for i = len(sfilein) to 1 step -1

if instr("/", mid$(sfilein, i, 1)) then exit for

next

parsefilename = mid$(sfilein, i + 1, len(sfilein) - i)

end function

'判斷字串中是否含有空格,單引號,雙引號等特殊字元

public function checkinput(istr as string) as boolean

if instr(istr, " ") > 0 or instr(istr, "'") > 0 or instr(istr, """") > 0 then

checkinput = false

exit function

else

checkinput = true

exit function

end if

end function

'fso的幾個應用函式

'1.讀取檔案中所有字元的函式

'其實就是通過readline(讀取行),通過 while not cnrs.atendofstream 的條件進行迴圈讀取行,

'來達到讀取檔案中所有字元。當然也可以使用readall代替多個readline,但主要缺點是將格式進行換行等問題需要再次解決。

'引用函式 call fsofileread("***檔案") 即可

function filereadall(filename as string) as string

on error goto errlabel

dim fso as new filesystemobject

if not fso.fileexists(filename) then

filereadall = ""

exit function

else

dim cnrs as textstream

dim rsline as string

rsline = ""

set cnrs = fso.opentextfile(filename, 1)

while not cnrs.atendofstream

rsline = rsline & cnrs.readline

wend

filereadall = rsline

exit function

end if

errlabel:

filereadall = ""

end function

'2讀取檔案中某一行中所有字元的函式

'這次即使用了readall方法,通過split函式將讀取的內容以換行為條件,進行陣列的定義,

'提取 linenum-1(陣列從0記數) 所對應的陣列值即為 讀取的該行值 ,也就是該行中所有的字元了。

'函式的呼叫 call fsolinedit("***檔案",35) 表示顯示***檔案的第35行內容

function lineedit(filename as string, linenum as integer) as string

on error goto errlabel

if linenum < 1 then

lineedit = ""

exit function

end if

dim fso as new filesystemobject

if not fso.fileexists(filename) then

lineedit = ""

exit function

else

dim f as textstream

dim tempcnt as string

dim temparray

set f = fso.opentextfile(filename, 1)

if not f.atendofstream then tempcnt = f.readall

f.close

set f = nothing

temparray = split(tempcnt, chr(13) & chr(10))

if linenum > ubound(temparray) + 1 then

lineedit = ""

exit function

else

lineedit = temparray(linenum - 1)

end if

end if

exit function

errlabel:

lineedit = ""

end function

'3.讀取檔案中最後一行內容的函式

'其實和讀取某一行的函式類似,主要即是 陣列的上界ubound值 就是最末的值 ,故為最後一行。函式的引用也很簡單。

function lastline(filename as string) as string

on error goto errlabel

dim fso as new filesystemobject

if not fso.fileexists(filename) then

lastline = ""

exit function

end if

dim f as textstream

dim tempcnt as string

dim temparray

set f = fso.opentextfile(filename, 1)

if not f.atendofstream then

tempcnt = f.readall

f.close

set f = nothing

temparray = split(tempcnt, chr(13) & chr(10))

lastline = temparray(ubound(temparray))

end if

exit function

errlabel:

lastline = ""

end function

'在asp中自動建立多級資料夾的函式

'fso中有個方法是createfolder,但是這個方法只能在其上一級資料夾存在的情況下建立新的資料夾,

'所以我就寫了乙個自動建立多級資料夾的函式,在生成靜態頁面等方面使用非常方便.

'--------------------------------

' 自動建立指定的多級資料夾

' strpath為絕對路徑

function autocreatefolder(strpath) as boolean

on error resume next

dim astrpath

dim ulngpath as integer

dim i as integer

dim strtmppath as string

if instr(strpath, "/") <= 0 or instr(strpath, ":") <= 0 then

autocreatefolder = false

exit function

end if

dim objfso as new filesystemobject

if objfso.folderexists(strpath) then

autocreatefolder = true

exit function

end if

astrpath = split(strpath, "/")

ulngpath = ubound(astrpath)

strtmppath = ""

for i = 0 to ulngpath

strtmppath = strtmppath & astrpath(i) & "/"

if not objfso.folderexists(strtmppath) then

' 建立

objfso.createfolder (strtmppath)

end if

next

set objfso = nothing

if err = 0 then

autocreatefolder = true

else

autocreatefolder = false

end if

end function

一些常用的函式

設定文字 test.getdlgitem text的id setwindowtext 顯示文字1 獲得當前目錄 getcurrentdirectory max path,buf 獲得當前程式檔名等 getmodulefilename getsystemdirectory getwindowsdire...

一些常用的函式

這是一些使用頻率比較高的函式,有的來自別人的程式.1.產生隨機字串函式 function random length return hash 2.擷取一定長度的字串 注 該函式對gb2312使用有效 function wordscut string,length sss 0 for i 0 i le...

git常用的一些操作

一 你是第乙個提交 到github gitlab上的人 git init 初始化本地倉庫 git add 將檔案加到暫存區 git commit m first commit 將檔案提交到本地倉庫 git remote add origin git github.com test.git 鏈結git...