㈠ VB獲取文件夾路徑
給你個VB遍歷文件夾的代碼吧
Private
Declare
Function
FindFirstFile
Lib
"kernel32"
Alias
"FindFirstFileA"
(ByVal
lpFileName
As
String,
lpFindFileData
As
WIN32_FIND_DATA)
As
Long
'查找下一個文件的API
Private
Declare
Function
FindNextFile
Lib
"kernel32"
Alias
"FindNextFileA"
(ByVal
hFindFile
As
Long,
lpFindFileData
As
WIN32_FIND_DATA)
As
Long
'獲取文件屬性的API
Private
Declare
Function
GetFileAttributes
Lib
"kernel32"
Alias
"GetFileAttributesA"
(ByVal
lpFileName
As
String)
As
Long
'關閉查找文件的API
Private
Declare
Function
FindClose
Lib
"kernel32"
(ByVal
hFindFile
As
Long)
As
Long
Const
MAX_PATH
=
260
Const
MAXDWORD
=
&HFFFF
Const
FILE_ATTRIBUTE_DIRECTORY
=
&H10
Private
Type
FILETIME
dwLowDateTime
As
Long
dwHighDateTime
As
Long
End
Type
Dim
tempstr
As
String
'定義類(用於查找文件)
Private
Type
WIN32_FIND_DATA
dwFileAttributes
As
Long
ftCreationTime
As
FILETIME
ftLastACCESSTime
As
FILETIME
ftLastWriteTime
As
FILETIME
nFileSizeHigh
As
Long
nFileSizeLow
As
Long
dwReserved0
As
Long
dwReserved1
As
Long
cFileName
As
String
*
MAX_PATH
cAlternate
As
String
*
14
End
Type
Dim
filecount
As
Integer
Dim
dirs()
As
String
Dim
curr
As
Long
Dim
ss()
As
String
Private
Sub
Command1_Click()
tempstr
=
"c:"
searchdir
tempstr
filecount
=
0
End
Sub
Public
Function
searchdir(path
As
String)
Dim
WFD
As
WIN32_FIND_DATA
Dim
i
As
Long
Dim
temp
As
String
Dim
h
As
Long
Dim
zhao
As
Long
Dim
iindex
As
Integer
Dim
dirs()
As
String
Dim
l
As
Long
zhao
=
1
h
=
FindFirstFile(path
&
"\*.*",
WFD)
If
h
<>
-1
Then
While
zhao
zhao
=
1
temp
=
Left(WFD.cFileName,
InStr(WFD.cFileName,
Chr$(0))
-
1)
If
temp
<>
"."
And
temp
<>
".."
Then
If
WFD.dwFileAttributes
And
vbDirectory
Then
ReDim
Preserve
dirs(iindex)
dirs(iindex)
=
path
&
"\"
&
temp
iindex
=
iindex
+
1
ReDim
Preserve
ss(filecount)
ss(filecount)
=
path
&
"\"
&
temp
filecount
=
filecount
+
1
End
If
End
If
zhao
=
FindNextFile(h,
WFD)
Wend
End
If
FindClose
(h)
If
iindex
>
0
Then
For
i
=
0
To
iindex
-
1
Call
searchdir(dirs(i))
Next
i
End
If
End
Function
㈡ vb 完整路徑獲取文件夾路徑
Path = "c:\soft\中國人\setup.exe"
For i = 1 To Len(Path)
If Mid(Path, i, 1) = "\" Then Print Left(Path, i)
Next i
或者:
Path = "c:\soft\中國人\setup.exe"
For i = 1 To Len(Path)
If Mid(Path, i, 1) = "\" Then Text1.Text = Left(Path, i)
Next i
將路徑存放於text1內
㈢ VB 瀏覽文件夾獲取文件夾路徑
commondialog控制項只能選擇文件,不能選擇文件夾,用下面的代碼可以選文件夾:
Option Explicit
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Public Function BrowseForFolder(Optional sTitle As String = "請選擇文件夾") As String
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
.hWndOwner = 0 ' Me.hWnd
.lpszTitle = lstrcat(sTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
BrowseForFolder = sPath
End Function
Private Sub Command1_Click()
MsgBox BrowseForFolder()
End Sub
㈣ VB查找指定文件夾並獲取文件路徑的代碼
1.首先打開Excel電子表格,然後在開發工具中打開VBA編輯器,如下圖。
㈤ VB中如何獲取當前程序的絕對路徑
System.Environment.CurrentDirectory;
//例:c:/test/
Application.ExecutablePath;(包括名稱)
//例:c:/test/myapp.exe
Application.StartupPath;(不包括名稱)
//例:c:/test/
絕對路徑是直接到達目標位置,通常是從盤符開始的路徑。完整的描述文件位置的路徑就是絕對路徑,以web站點根目錄為參考基礎的目錄路徑。
絕對路徑名的指定是從樹型目錄結構頂部的根目錄開始到某個目錄或文件的路徑,由一系列連續的目錄組成,中間用斜線分隔,直到要指定的目錄或文件,路徑中的最後一個名稱即為要指向的目錄或文件。之所以稱為絕對,意指當所有網頁引用同一個文件時,所使用的路徑都是一樣的。
(5)vb語言怎麼讀取文件夾路徑擴展閱讀
幾種編程語言獲取程序所在路徑的方法:
1、在golang程序裡面獲取程序所在路徑:
package main
import (
"path/filepath"
"os"
"fmt"
"log"
)
func main() {
execDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err = nil {
log.Fatal(err)
}fmt.Println(execDir)
}
2、python腳本所在路徑:
import os
print(os.path.split(os.path.realpath(__file__))[0])
3、shell腳本獲取腳本的絕對路徑:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
㈥ vb 如何通過一個文件的全路徑獲得該文件所在文件夾
解決這個問題有兩種方法。
第一種:知道了文件的全路徑,那麼路徑中當然也包含文件所在的文件夾信息,只要從中提取即可。例如,已知文件全路徑為「C:WindowsSystem32abc.dll」並賦予變數strPt,可用過下面語句獲取文件夾。
left(strPt,instrrev(strPt,""))
instrrev函數的作用是從右側開始查找指定字元串,並返回數值,此處返回值為20.left函數的作用是從左往右取N個字元,此例中取20個,最後結果為:C:WindowsSystem32。
第二種方法:使用FileSystemObject對象。代碼如下:
dimfsoasobject,strFolderasobject
setfso=createobject("scripting.filesystemobject")
setstrFolder=fso.getfolder("C:WindowsSystem32abc.dll")
msgboxstrFolder.path
文件系統對象FSO的英文全稱是File System Object ,這種對象模型提出了有別於傳統的文件操作語句處理文件和文件夾的方法。通過採用object.method這種在面向對象編程中廣泛使用的語法,將一系列操作文件和文件夾的動作通過調用對象本身的屬性直接實現。
㈦ VB獲取文件路徑是怎麼樣的
VB2008測試通過 Imports System Imports System.IO Imports System.Threading Imports System.Windows.Forms
Public Class FrmMain
Dim newSearcher As getFileByName
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
rball.Checked = True
tbpath.Text = ""
tbpath.Enabled = False
btndlg.Enabled = False
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
tv.Nodes.Clear()
btnOK.Enabled = False
btnExit.Enabled = False
If rball.Checked Then
run()
Else
If tbpath.Text = "" Then
MsgBox("請輸入或者選擇一個路徑")
Exit Sub
Else
run(tbpath.Text)
End If
End If
End Sub
Private Sub btndlg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndlg.Click
fbdlg.RootFolder = Environment.SpecialFolder.MyComputer
fbdlg.ShowDialog()
tbpath.Text = fbdlg.SelectedPath
End Sub
Private Sub rbchs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbchs.CheckedChanged
If rbchs.Checked = True Then
tbpath.Enabled = True
btndlg.Enabled = True
Else
tbpath.Enabled = False
btndlg.Enabled = False
End If
End Sub
Private Sub run()
If tbfileName.Text = "" Then
MsgBox("請輸入文件名稱")
btnOK.Enabled = True
btnExit.Enabled = True
Exit Sub
Else
newSearcher = New getFileByName(tbfileName.Text)
newSearcher.GetDrivers()
tmr.Enabled = True
tmr.Interval = 10
End If
End Sub
Private Sub run(ByVal path As String)
If tbfileName.Text = "" Then
MsgBox("請輸入文件名稱")
btnOK.Enabled = True
btnExit.Enabled = True
Exit Sub
Else
newSearcher = New getFileByName(tbfileName.Text)
newSearcher.SearchFolder(path)
tmr.Enabled = True
tmr.Interval = 50
End If
End Sub
Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick
Lblshow.Text = newSearcher.currentPath
If newSearcher.cntThread = 0 And newSearcher.isDone Then
'MsgBox(newSearcher.getFile.ToString)
'MsgBox("全部線程結束")
For Each Str As String In newSearcher.getFile
tv.Nodes.Add(Str)
Next
'For i As Integer = 0 To newSearcher.getFile.Length - 1
'tv.Nodes.Add(newSearcher.getFile(i))
'Next
tmr.Enabled = False
btnExit.Enabled = True
btnOK.Enabled = True
End If
My.Application.DoEvents()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub End Class
㈧ VB怎樣快速獲取當前文件夾下的所有文件的路徑(包括子目錄)
有兩種方法:
1、使用DIR遞歸,不過這是微軟明確不推薦的
2、最快但不太正規的
使用 Shell "cmd /k dir c:\windows /s/b/a >>d:\jg.txt"
c:\windows 目標文件夾,當前文件夾為 app.path,你可以根據要求生成命令字元串
d:\jg.txt 為臨時文件
執行這行後再讀取臨時文件即可。極快!