怎么用vba 做一个文件搜索程序

怎么用vba 做一个文件搜索程序,第1张

代码及注释如下:

sub main

dim fs

Set fs = Application.FileSearch'定义fs为文件搜索

With fs

.LookIn = "C:\"'搜索位置为C盘根目录

.FileName =worksheets("sheet1").cells(1,1) ’搜索名称为工作表1中的第一个单元格

If .Execute >0 Then'如果搜索结果不为空

For i = 1 To .FoundFiles.Count

worksheets("sheet1").cells(i,2)= .FoundFiles(i)'遍历所有文件,并将之放到第二列中

Next i

Else

MsgBox "There were no files found."'如果找不到,则提示没找到

End If

End With

end sub

 

 

你必须用 File 类()。

写了个简单的例子让你参考:

import java.io.*

import java.util.*

class SimpleLocalDriveSearch {

    private List result = new ArrayList( )

    private void recursiveSearch( File rootDir, String suffix ) {

        File[ ] files = rootDir.listFiles( )

        if ( files != null )

            for ( int i = 0i <files.lengthi++ ) {

                if ( files[ i ].isFile( ) &&files[ i ].getName( ).endsWith( suffix ) )

                    result.add( files[ i ] )

                recursiveSearch( files[ i ], suffix )

            }

    }

    public List search( File startDir, String matchingExtension ) {

        result.clear( )

        recursiveSearch( startDir, "." + matchingExtension )

        return result

    }

    public static void main( String[ ] args ) throws Exception {

        System.out.println( "Enter the extension of files to be searched for:" )

        String ext = new BufferedReader( new InputStreamReader( System.in ) ).readLine( )

        SimpleLocalDriveSearch slds = new SimpleLocalDriveSearch( )

        File[ ] roots = File.listRoots( )

        for ( int i = 0i <roots.lengthi++ ) {

            List found = slds.search( roots[ i ], ext )

            if ( found.size( ) >0 )

                for ( Iterator iter = found.iterator( )iter.hasNext( ))

                    System.out.println( iter.next( ) )

            else

                System.out.println( "No match on " + roots[ i ] )

        }

    }

}

 

 

 

 

 

你必须用 File 类(http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/io/File.html)。

写了个简单的例子让你参考:

import java.io.*

import java.util.*

class SimpleLocalDriveSearch {

    private List result = new ArrayList( )

    private void recursiveSearch( File rootDir, String suffix ) {

        File[ ] files = rootDir.listFiles( )

        if ( files != null )

            for ( int i = 0i <files.lengthi++ ) {

                if ( files[ i ].isFile( ) &&files[ i ].getName( ).endsWith( suffix ) )

                    result.add( files[ i ] )

                recursiveSearch( files[ i ], suffix )

            }

    }

    public List search( File startDir, String matchingExtension ) {

        result.clear( )

        recursiveSearch( startDir, "." + matchingExtension )

        return result

    }

    public static void main( String[ ] args ) throws Exception {

        System.out.println( "Enter the extension of files to be searched for:" )

        String ext = new BufferedReader( new InputStreamReader( System.in ) ).readLine( )

        SimpleLocalDriveSearch slds = new SimpleLocalDriveSearch( )

        File[ ] roots = File.listRoots( )

        for ( int i = 0i <roots.lengthi++ ) {

            List found = slds.search( roots[ i ], ext )

            if ( found.size( ) >0 )

                for ( Iterator iter = found.iterator( )iter.hasNext( ))

                    System.out.println( iter.next( ) )

            else

                System.out.println( "No match on " + roots[ i ] )

        }

    }

}

 

 

 


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

原文地址: http://outofmemory.cn/yw/11858750.html

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

发表评论

登录后才能评论

评论列表(0条)

保存