vba中要获取文件夹里excel文件的数量,要用什么语句

vba中要获取文件夹里excel文件的数量,要用什么语句,第1张

1GetAttr 函数

语法:GetAttr(pathname)

功能:获取一个文件、目录、或文件夹的属性。返回一个 Integer值。

返回值

由 GetAttr 返回的值,是下面这些属性值的总和:

常数 值 描述

vbNormal 0 常规

vbReadOnly 1 只读

vbHidden 2 隐藏

vbSystem 4 系统文件

vbDirectory 16 目录或文件夹

vbArchive 32 存档文件

vbalias 64 指定的文件名是别名。只在Macintosh中可用。

说明:若要判断是否设置了某个属性,在 GetAttr 函数与想要得知的属性值之间使用 And 运算符与逐位比较。如果所得的结果不为零,则表示设置了这个属性值。

示例:

DebugPrint GetAttr("F:\testtxt") '若为存档文件,在立即窗口可看到值为32

DebugPrint GetAttr("F:\testtxt") '将属性—高级—可存档文件的勾去掉后,值为0

为判断一个文件是否只读,可用下法:

DebugPrint GetAttr("F:\testtxt") And vbReadOnly

若值非零,说明时只读的。

2复制

'

' (1) 在不需要逐个打开工作簿的情况下,将其有效工作表依次复制到本工作簿的最后

' 新工作表名为:原工作簿名_原工作表名

'

' Sub 复制工作表()

Dim MyObject As Object

Dim strPath As String, strFileName As String, strMyName As String

Dim shtSheet As Worksheet, strShtName As String

Dim intCount As Integer, intShtCount As Integer, i As Integer

ApplicationScreenUpdating = False

strPath = ThisWorkbookPath

strMyName = ThisWorkbookName

intShtCount = ThisWorkbookSheetsCount

With ApplicationFileSearch

NewSearch

LookIn = strPath

SearchSubFolders = False

Filename = "xls"

FileType = msoFileTypeOfficeFiles

If Execute() > 0 Then

intCount = FoundFilesCount

For i = 1 To intCount

strFileName = Replace(FoundFiles(i), strPath & "\", "")

If strFileName <> strMyName Then

Set MyObject = GetObject(strPath & "/" & strFileName)

'下面进行复制工作

For Each shtSheet In MyObjectWorksheets

strShtName = shtSheetName

If MyObjectSheets(strShtName)UsedRangeCount > 1 Then

MyObjectSheets(strShtName)Copy After:=ThisWorkbookSheets(intShtCount)

intShtCount = intShtCount + 1

'重新命名

strShtName = Replace(strFileName, "xls", "_") & strShtName

ThisWorkbookSheets(intShtCount)Name = strShtName

ThisWorkbookSheets("目录")Cells(i + 1, 1) = strShtName

End If

Next shtSheet

End If

Next i

Else

MsgBox "没有找到符合指定文件,请修改参数后重新搜索!", ,"提示"

End If

End With

ThisWorkbookSheets("目录")Select

ApplicationScreenUpdating = True

End Sub

'

'(2) 逐个打开同一目录下的所有工作簿,将其有效工作表依次复制到本工作簿的最后复制完后关闭它

' 新工作表名为:原工作簿名_原工作表名

'

Sub 复制工作表_2()

Dim MyObject As Object

Dim strPath As String, strFileName As String, strMyName As String

Dim shtSheet As Worksheet, strShtName As String

Dim intCount As Integer, intShtCount As Integer, i As Integer

ApplicationScreenUpdating = False

strPath = ThisWorkbookPath

strMyName = ThisWorkbookName

intShtCount = ThisWorkbookSheetsCount

With ApplicationFileSearch

NewSearch

LookIn = strPath

SearchSubFolders = False

Filename = "xls"

FileType = msoFileTypeOfficeFiles

If Execute() > 0 Then

intCount = FoundFilesCount

For i = 1 To intCount

strFileName = Replace(FoundFiles(i), strPath & "\", "")

If strFileName <> strMyName Then

'WorkbooksOpen Filename:=strPath & "/" & strFileName

Set MyObject = GetObject(strPath & "/" & strFileName)

'下面进行复制工作

For Each shtSheet In Workbooks(strFileName)Worksheets

strShtName = shtSheetName

If Workbooks(strFileName)Sheets(strShtName)UsedRangeCount > 1 Then

Workbooks(strFileName)Sheets(strShtName)Copy After:=ThisWorkbookSheets(intShtCount)

intShtCount = intShtCount + 1

'重新命名

strShtName = Replace(strFileName, "xls", "_") & strShtName

ThisWorkbookSheets(intShtCount)Name = strShtName

ThisWorkbookSheets("目录")Cells(i + 1, 1) = strShtName

End If

Next shtSheet

'Workbooks(strFileName)Close

End If

Next i

Else

MsgBox "没有找到符合指定文件,请修改参数后重新搜索!", ,"提示"

End If

End With

ThisWorkbookSheets("目录")Select

ApplicationScreenUpdating = True

End Sub

@echo off

for /f %%a in ('type %1') do set /a v+=1

echo 执行的是 %1

echo 该文件有 %v% 行字符!

pause

直接把文件拖到该批处理图标上就行了,结果就会显示出来的,

不用先运行该批处理,在把文件拖到命令窗口中去,在按 回车那么麻烦。

递归获取本文件夹(包括子文件夹)中的文件:

int CountDirectory(CString path)

{

int count = 0;

CFileFind finder;

BOOL working = finderFindFile(path + "\\");

while (working)

{

working = finderFindNextFile();

if (finderIsDots())

continue;

if (finderIsDirectory())

count += CountDirectory(finderGetFilePath());

else

count++;

}

return count;

}

只获取本文件夹中的文件:

int CountDirectory(CString path)

{

int count = 0;

CFileFind finder;

BOOL working = finderFindFile(path + "\\");

while (working)

{

working = finderFindNextFile();

if (finderIsDots())

continue;

if (!finderIsDirectory())

count++;

}

return count;

}

vbs代码如下:

'══代══码══开══始════

i=0

set fso=CreateObject("ScriptingFileSystemObject")

set fs=fsogetfolder("")files

for each f in fs

i=i+1

next

msgbox "文件总数:"&i

'Coded By escortmnm from VBS团队

'══代══码══结══束════

说明:

使用folder对象下面的files属性,查到一个文件即可加1,最后输出结果。

最直接的方法是以文件夹名创建 File 对象,然后调用 listFiles( ) 生成一个装着文件夹内容的 File 数组,

最后遍历该数组并通过 isFile( ) 的调用计算文件数量。

应当一提的是,那个 Java 标准库里的 File 类代表的是文件和路径,所以它的名字并不贴切。

下面的例子将计算出 C:\Windows 下的文件数目:

import javaio;

class C {

    public static void main( String[] args ) {

        int count = 0;

        for( File file: new File( "C:/Windows" )listFiles( ) )

            if( fileisFile( ) ) ++count;

        Systemoutprintln( "Number of file(s): " + count );

    }

}

以上就是关于vba中要获取文件夹里excel文件的数量,要用什么语句全部的内容,包括:vba中要获取文件夹里excel文件的数量,要用什么语句、如何用BAT获取一个文本文件的行数最好要有能往命令行里拖放文件的功能.、vc如何获取文件夹中文件个数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9765882.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存