tags:

views:

144

answers:

2

I want to came out with a program which can open folder by reading the path from textfile. When open, the code will check if there is files inside the folder. for the first file, do some calling function, it will keep doing until the last file available in the folder.

for example, if the folder A content 3 files, 1st.doc, 2nd.txt, 3rd.pdf, it will doing an operation to each files until the last file then exit.

thanks

I need idea on how to implement it, references or example from any language also are very helpful.

Im coding it in 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)
A: 

Your tag says VBScript, so here are a few notes:

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
Remou
Let's say, i want to get a hold at the file and doing some calling function instead of call for display in the message box, how can i do that?
A: 

Problem FIX. thank you. But another problem that still nd u'all help

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

I have this function moveToFolder() that will move all *.pac to different folder.

  • Error: the operation stopped before the function called. Said permission denied
  • how can i add to move the files and overwrite if same name exist?

.

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