如何选择文件夹中的文件?

时间:2009-10-06 06:12:18

标签: vbscript

我想推出一个可以通过从文件文件中读取路径来打开文件夹的程序。 打开时,代码将检查文件夹中是否有文件。对于第一个文件,执行一些调用函数,它将继续执行,直到文件夹中的最后一个文件可用。

例如,如果文件夹A内容为3个文件,1st.doc,2nd.txt,3rd.pdf,它将对每个文件进行操作,直到最后一个文件退出。

谢谢

我需要了解如何实现它,任何语言的参考或示例也非常有用。

我在Basic中编码:

;open textfile
$dir = "C:\Documents and Settings\admin\My Documents\AutoItCodes\"
$file = FileOpen($dir & "Setting.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$source = FileReadLine($file,1)
$dest = FileReadLine($file,2)
$pass = FileReadLine($file,3)
FileClose($file)

;check files available in the folder $source
$search = FileFindFirstFile($source & "*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

;Check for the first file and display in message box
;***I belived it all start here!!***

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop 
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

问题FIX。谢谢。但另一个问题仍然无法帮助

Dim objShell: Set objShell = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objShell.Namespace(source) 
Dim colItems: Set colItems = objFolder.Items
Dim i
For i = 0 to colItems.Count - 1
    colItems.Item(i).InvokeVerbEx("Encrypt")
    'do my execution
    call moveToFolder()
Next

我有这个函数moveToFolder(),它将所有* .pac移动到不同的文件夹。

  • 错误:操作在调用函数之前停止。声称被拒绝
  • 如果存在相同名称,我如何添加移动文件和覆盖?

Sub moveToDest()
 dim newfolder
 Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
 If  Not objFSO.FolderExists(dest) Then
    newfolder = objFSO.CreateFolder (dest)
    WScript.Echo "A new folder '" & newfolder & "' has been created" 
 End If
 objFSO.MoveFile source & "*.pac" , dest, true
End Sub

1 个答案:

答案 0 :(得分:0)

你的标签是VBScript,所以这里有一些注意事项:

Option Explicit

Const ForReading = 1
Dim a, fs, f, fldr, i, s

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.OpenTextFile("C:\Docs\fileList.txt", ForReading)

a = Split(f.ReadAll,vbCrLf)

f.Close

If fs.FolderExists(a(0)) Then

    Set fldr = fs.GetFolder(a(0))
    i = fldr.Files.Count

    If i>0 Then
        For Each f In fldr.Files
            s=vbCrLf & f.Name
        Next

        MsgBox s
    Else
        MsgBox a(0) & " does not have any files."
    End If
Else
    MsgBox a(0) & " does not exists."
End If