㈠ 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 为临时文件
执行这行后再读取临时文件即可。极快!