Jump to content

Recommended Posts

Posted

I could use some help converting this VBS It's been a long time since I've done anything with Auto IT (last used was in the 0.xx versions) and I'm lost... My goal is to limit the recursion to 2 folders (which I've gotten with VBS), and then display the output in a message box (I'ven't instituted yet in the VBScript)...

CODE
Dim objFSO, objFolder

Dim objexcel, r

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

set objFolder = objFSO.GetFolder("\\server\share")

set colSub = objFolder.subfolders

Set WS = WScript.CreateObject("Wscript.Shell")

strFilePath = "filename.txt"

Set strFile = objFSO.CreateTextFile(strFilePath, True)

r=2

For Each objsub In colSub

ShowSub objsub, r

Next

WS.Run strFilePath

Function ShowSub (objsub, r)

strFile.WriteLine "Folder " & objFolder & "\" & objsub.Name & ""

Set colFiles = objsub.Files

For Each objFile in colFiles

strFile.WriteLine ("****FILE FOUND**** " & objFile.Name)

Next

strFile.WriteLine

r = r+1

End Function

Posted (edited)

I could use some help converting this VBS It's been a long time since I've done anything with Auto IT (last used was in the 0.xx versions) and I'm lost... My goal is to limit the recursion to 2 folders (which I've gotten with VBS), and then display the output in a message box (I'ven't instituted yet in the VBScript)...

CODE
Dim objFSO, objFolder

Dim objexcel, r

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

set objFolder = objFSO.GetFolder("\\server\share")

set colSub = objFolder.subfolders

Set WS = WScript.CreateObject("Wscript.Shell")

strFilePath = "filename.txt"

Set strFile = objFSO.CreateTextFile(strFilePath, True)

r=2

For Each objsub In colSub

ShowSub objsub, r

Next

WS.Run strFilePath

Function ShowSub (objsub, r)

strFile.WriteLine "Folder " & objFolder & "\" & objsub.Name & ""

Set colFiles = objsub.Files

For Each objFile in colFiles

strFile.WriteLine ("****FILE FOUND**** " & objFile.Name)

Next

strFile.WriteLine

r = r+1

End Function

Welcome to 21st Century AutoIt! :D

Have you downloaded the latest version of AutoIt and SciTE?

If so,

1. Remove the "Set" commands.

2. Put a dollar sign in front of the variable names.

3. Change the VBS "WScript.CreateObject" to AutoIt's "ObjCreate".

4. Take the keyword "Each" out of the For/In/Next loops.

5. Change the function declaration to AutoIt's Func/EndFunc.

6. Read the help file entry for each of the AutoIt commands you are attempting to use here.

That will get you closer.

No one should just do this for you, that's not what this forum is about.

:D

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted

Doen't the word help imply you have done already some work? :D

Well, I actually have done everything PSaltyDS has suggested and if you'd like I can post that code up but it's still not working... And no, I'm not looking for someone to do the work for me but I was more looking for help with these two lines:

set objFolder = objFSO.GetFolder("\\server\share")

set colSub = objFolder.subfolders

As so far as I can tell there's no AutoIT functionality for that. But I'll repost my code that I did change tomorrow as it's time to leave now.

Posted (edited)

Here's my attempt at converting this script...

Dim $objFSO, $objFolder
Dim $objexcel, $r
Dim $objFSO = ObjCreate("Scripting.FileSystemObject")
Dim $objFolder = $objFSO.GetFolder("C:\Temp")
Dim $WS = ObjCreate("Wscript.Shell")

$strFilePath = "C:\results.txt"
 
Dim $strFile = $objFSO.CreateTextFile($strFilePath, 1)
$r=2

For $objsub In $colSub
    ShowSub ($objsub, $r)
Next

$WS.Run ($strFilePath)
 
Func ShowSub ($objsub, $r)
    $strFile.WriteLine ("Folder " & $objFolder & "\" & $objsub.Name & "")
    Dim $colFiles = $objsub.Files
    For $objFile in $colFiles
        $strFile.WriteLine ("****FILE FOUND****  " & $objFile.Name)
    Next
    $strFile.WriteLine()
    $r = $r+1
EndFunc
Edited by OverSeer
Posted (edited)

Here's my attempt at converting this script...

Dim $objFSO, $objFolder
Dim $objexcel, $r
Dim $objFSO = ObjCreate("Scripting.FileSystemObject")
Dim $objFolder = $objFSO.GetFolder("\\servername\FileRepository")
Dim $WS = ObjCreate("Wscript.Shell")

$strFilePath = "C:\WarrantyInternal.txt"
 
Dim $strFile = $objFSO.CreateTextFile($strFilePath, 1)
$r=2

For $objsub In $colSub
    ShowSub ($objsub, $r)
Next

$WS.Run ($strFilePath)
 
Func ShowSub ($objsub, $r)
    $strFile.WriteLine ("Folder " & $objFolder & "\" & $objsub.Name & "")
    Dim $colFiles = $objsub.Files
    For $objFile in $colFiles
        $strFile.WriteLine ("****FILE FOUND****  " & $objFile.Name)
    Next
    $strFile.WriteLine()
    $r = $r+1
EndFunc
to begin with you missed set colSub = objFolder.subfolders, so $colSub is not set.

Ok, if you know vbs then conversion to autoit is very easy as the languages in many respects are similar syntax.

No need for "Scripting.FileSystemObject" or "Wscript.Shell" objects. Only needed in vbs because vbs can't perform the tasks directly.

Look in examples for ant of the File...functions, eg FileReadLine()

Edited by Will66
Posted (edited)

Are you still stuck? You forget to convert this line...(set colSub = objFolder.subfolders).

I think the first thing people forget when they are converting VB is to look for exisiting functionality in AutoIt with less code.

A quick skim of your code brings me to this:

#include <file.au3>
 
 $folderName = "\\servername\FileRepository"
 
;Return array containing names of all subfolders
 $folderArray = _FileListToArray ($folderName, "*", 2)
 
 $strFilePath = "C:\WarrantyInternal.txt"
 
 $handle = FileOpen($strFilePath,2)
 
;Loop through all subfolders
 For $X = 1 to $folderArray[0]
     FileWriteLine($handle, "Folder " &  $folderName & "\" & $folderArray[$X])
     
   ;Retrieve list of files in subfolder
    $fileArray = _FileListToArray( $folderName & "\" & $folderArray[$X], "*", 1)
     
    ;Loop through all files in subfolder
     For $Y = 1 to $fileArray[0]
     FileWriteLine($handle, "****FILE FOUND****  " & $fileArray[$Y])
     Next
 Next
 
 FileClose($handle)
Edited by weaponx
Posted (edited)

Thank you for the assistance folks. I'll definitely look over your code WeaponX as I'm trying to get used to the new and updated AutoIT and all it's functionality... But yes, once I put my $colSub = $objFolder.subfolders line in, I was able to get it to work. I do have one more question though... I would like to put the output into a Msgbox and I'm thinking of using the StdoutRead function but I'm unsure as to how to get the information to "stream" to it (as I currently write to a file)... I'd prefer to not write any file and just produce the Msgbox... Any ideas?

Edited by OverSeer
Posted

I do have one more question though... I would like to put the output into a Msgbox and I'm thinking of using the StdoutRead function but I'm unsure as to how to get the information to "stream" to it (as I currently write to a file)... I'd prefer to not write any file and just produce the Msgbox... Any ideas?

One of many ways to do that:
;Loop through all subfolders
$sMsg = "Folder list:" & @CRLF
For $X = 1 to $folderArray[0]
    $sMsg &= "Folder " &  $folderName & "\" & $folderArray[$X] & @CRLF
    
   ;Retrieve list of files in subfolder
    $fileArray = _FileListToArray( $folderName & "\" & $folderArray[$X], "*", 1)
    
    ;Loop through all files in subfolder
     For $Y = 1 to $fileArray[0]
        $sMsg &= "****FILE FOUND****     " & $fileArray[$Y] & @CRLF
     Next
Next

MsgBox(64, "File list", $sMsg)

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted (edited)

Tons of help! Thanks guys... But I'm having issues with both WeaponX and PSaltyDS's scripts...

The line:

For $Y = 1 to $fileArray[0]

Produces this error:

Error: Subscript used with non-Array variable.

Any ideas?

Edited by OverSeer
Posted

Maybe something like this:

;Loop through all subfolders
$sMsg = "Folder list:" & @CRLF
For $X = 1 to $folderArray[0]
    $sMsg &= "Folder " &  $folderName & "\" & $folderArray[$X] & @CRLF
   
   ;Retrieve list of files in subfolder
    $fileArray = _FileListToArray( $folderName & "\" & $folderArray[$X], "*", 1)
   
If NOT @ERROR Then
    ;Loop through all files in subfolder
     For $Y = 1 to $fileArray[0]
        $sMsg &= "****FILE FOUND****     " & $fileArray[$Y] & @CRLF
     Next
EndIf
Next

MsgBox(64, "File list", $sMsg)
Posted

Maybe something like this:

;Loop through all subfolders
$sMsg = "Folder list:" & @CRLF
For $X = 1 to $folderArray[0]
    $sMsg &= "Folder " &  $folderName & "\" & $folderArray[$X] & @CRLF
   
   ;Retrieve list of files in subfolder
    $fileArray = _FileListToArray( $folderName & "\" & $folderArray[$X], "*", 1)
   
If NOT @ERROR Then
    ;Loop through all files in subfolder
     For $Y = 1 to $fileArray[0]
        $sMsg &= "****FILE FOUND****     " & $fileArray[$Y] & @CRLF
     Next
EndIf
Next

MsgBox(64, "File list", $sMsg)

Works like a charm! You guys rock.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...