求教:如何根据excel表中的n个文件名找到指定目录下的文件,找到后复制到指定文件夹中?例如:在t2.xls的sheet1工作表中单元格 a1=“文件名”,a2、a3、a4、a5、a6分别为“1.txt”、“2.txt”、“3.txt”、“4.txt”、“5.txt”。现在要在文件夹“c:\abc"中找到这5个文件,找到后复制到“c:\123"文件夹中.请问高手用vb如何实现?谢谢

解决方案 »

  1.   

    在excel中文件操作可以使用fso对象Scripting Runtime Library   FileSystemObject MethodsIn This Section
    Add Method (Dictionary) 
    Adds a key and item pair to a Dictionary object. 
    Add Method (Folders) 
    Adds a new folder to a Folders collection. 
    BuildPath Method 
    Appends a name to an existing path. 
    Close Method 
    Closes an open TextStream file. 
    Copy Method 
    Copies a specified file or folder from one location to another. 
    CopyFile Method 
    Copies one or more files from one location to another. 
    CopyFolder Method 
    Recursively copies a folder from one location to another. 
    CreateFolder Method 
    Creates a folder. 
    CreateTextFile Method 
    Creates a specified file name and returns a TextStream object that can be used to read from or write to the file. 
    Delete Method 
    Deletes a specified file or folder. 
    DeleteFile Method 
    Deletes a specified file. 
    DeleteFolder Method 
    Deletes a specified folder and its contents. 
    DriveExists Method 
    Returns true if the specified drive exists; false if it does not. 
    Exists Method 
    Returns true if a specified key exists in the Dictionary object, false if it does not. 
    FileExists Method 
    Returns true if a specified file exists; false if it does not. 
    FolderExists Method 
    Returns true if a specified folder exists; false if it does not. 
    GetAbsolutePathName Method 
    Returns a complete and unambiguous path from a provided path specification. 
    GetBaseName Method 
    Returns a string containing the base name of the last component, less any file extension, in a path. 
    GetDrive Method 
    Returns a Drive object corresponding to the drive in a specified path. 
    GetDriveName Method 
    Returns a string containing the name of the drive for a specified path. 
    GetExtensionName Method 
    Returns a string containing the extension name for the last component in a path. 
    GetFile Method 
    Returns a File object corresponding to the file in a specified path. 
    GetFileName Method 
    Returns the last component of specified path that is not part of the drive specification. 
    GetFileVersion Method 
    Returns the version number of a specified file. 
    GetFolder Method 
    Returns a Folder object corresponding to the folder in a specified path. 
    GetParentFolderName Method 
    Returns a string containing the name of the parent folder of the last component in a specified path. 
    GetSpecialFolder Method 
    Returns the special folder object specified. 
    GetStandardStream Method 
    Returns a TextStream object corresponding to the standard input, output, or error stream. 
    GetTempName Method 
    Returns a randomly generated temporary file or folder name that is useful for performing operations that require a temporary file or folder. 
    Items Method 
    Returns an array containing all the items in a Dictionary object. 
    Keys Method 
    Returns an array containing all existing keys in a Dictionary object. 
    Move Method 
    Moves a specified file or folder from one location to another. 
    MoveFile Method 
    Moves one or more files from one location to another. 
    MoveFolder Method 
    Moves one or more folders from one location to another. 
    OpenAsTextStream Method 
    Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file. 
    OpenTextFile Method 
    Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file. 
    Read Method 
    Reads a specified number of characters from a TextStream file and returns the resulting string. 
    ReadAll Method 
    Reads an entire TextStream file and returns the resulting string. 
    ReadLine Method 
    Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string. 
    Remove Method 
    Removes a key, item pair from a Dictionary object. 
    RemoveAll Method 
    Removes all key, item pairs from a Dictionary object. 
    Skip Method 
    Skips a specified number of characters when reading a TextStream file. 
    SkipLine 
    Skips the next line when reading a TextStream file. 
    Write Method 
    Writes a specified string to a TextStream file. 
    WriteBlankLines Method 
    Writes a specified number of newline characters to a TextStream file. 
    WriteLine Method 
    Writes a specified string and newline character to a TextStream file. 
      

  2.   


       Dim FSO   Set FSO = CreateObject("Scripting.FileSystemObject")if FSO.FileExist("C:\abc\"+MyfileName) then
     FSO.CopyFile("C:\abc\"+MyfileName,"C:\123\"+MyfileName)
    end if
      

  3.   

    a1,a2、a3、a4、a5、a6 是指Excel里面的A1 A2 A3 A4 A5 A6 吗? 意思就是第一笔的A,第二笔的A,,第三笔的A......还是指第一笔的 A B C D E F ??
      

  4.   

    非常感谢各位的回复。
        在excel表中某列数据都是以文件名为内容,如上例中的A列除第1个单元格a1作为字段名,其他单元格a2、a4、a5、a6…的内容分别为“1.txt”、“2.txt”、“3.txt”、“4.txt”。在工作中,我要根据表中的文件名一个一个地到指定文件夹(如上例中“c:\abc")去找出来并复制到其他地方去。文件名有很多,一个一个找累死我了,于是就想用vb做一个程序做到按一下按钮那些要找的文件都到目的地去了,而且找不到的文件也能显示出来。这样多好,一劳永逸。可是我是个vb初学者,要做到这样有点困难,所以求助了。
       我的思路是:先用循环把excel表中的那些文件名赋值给一个变量(这个我一点不会),如filename,然后再用len(dir(“c:\abc",filename))判断文件名长度是否为0,不为0则复制该文件。不知道这样行不行?请各位指教,
    。如果行,谁能给我写一下代码;如果不行或还有更好的办法,请赐教,在此万感谢。