Sign in to follow this  
Followers 0
Tigerweld

Loop Batch Files

13 posts in this topic

how can I loop thru a directory and run any batch files that may be present? Can you use a wild card for the filename and use the .bat extension?

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

How about good ol' DOS style commands.

Send this to @comspec and it will only cost you one line of code:

for %f in (*.bat) do "%f"

*edit: you don't need 'start', plus start seems to screw up with double-quotes

Edited by Ascend4nt

Share this post


Link to post
Share on other sites

_FileListToArray()

See help file.

;)

OK I am using the FileListToArray, but when I try and run it I'm not getting the actually file name. I do if I uncomment the

_ArrayDisplay, but on the line with the msgbox it is showing the filename as *.bat. HELP!

#Include <File.au3>
#Include <Array.au3>
$sPath = "\\serverName\wol$\"
$sFilter = "*.bat"
$FileList=_FileListToArray($sPath, $sFilter)
;_ArrayDisplay($FileList,"$FileList")

    For $i = 1 To UBound ($FileList) - 1
        run($sPath & $sFilter)
        MsgBox(4096, "Workstation", $sPath & $sFilter)
    Next

Share this post


Link to post
Share on other sites

$FileList is an array. You need to learn how to reference the contents of an array. See "Variables" in the help file, and there is a good wiki on it.

;)


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

Share this post


Link to post
Share on other sites

OK I am using the FileListToArray, but when I try and run it I'm not getting the actually file name. I do if I uncomment the

_ArrayDisplay, but on the line with the msgbox it is showing the filename as *.bat. HELP!

#Include <File.au3>
#Include <Array.au3>
$sPath = "\\serverName\wol$\"
$sFilter = "*.bat"
$FileList=_FileListToArray($sPath, $sFilter)
;_ArrayDisplay($FileList,"$FileList")

    For $i = 1 To UBound ($FileList) - 1
        run($sPath & $sFilter)
        MsgBox(4096, "Workstation", $sPath & $sFilter)
    Next

You created the array $FileList, but then in your For..Next loop you referenced the original $sPath and $sFilter, so of course that's what the MsgBox is going to display. You need to change the MsgBox code to this:

MsgBox(4096, "Workstation", $FileList[$i])

This will show you what's contained in each element of the $FileList array, one element at a time.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Again, the simplest solution: ;)

$sFolderToUse="C:\batchfiles"
Run(@ComSpec&' /c for %f in (*.bat) do "%f"',$sFolderToUse)

Share this post


Link to post
Share on other sites

Again, the simplest solution: ;)

$sFolderToUse="C:\batchfiles"
Run(@ComSpec&' /c for %f in (*.bat) do "%f"',$sFolderToUse)

Hey I'm all about simple solutions, however when I use this I get an error about an internal command not recognized because the batch file has command lines in it. I can run them individually and it works fine. Thanks for helping!

Share this post


Link to post
Share on other sites

$FileList is an array. You need to learn how to reference the contents of an array. See "Variables" in the help file, and there is a good wiki on it.

;)

Am I closer? lol This is killing me!

#Include <File.au3>
#Include <Array.au3>
$sPath = "\\serverName\wol$\"
$sFilter = "*.bat"
$FileList=_FileListToArray($sPath, $sFilter)
;_ArrayDisplay($FileList,"$FileList")

For $i = 1 To $FileList[0]
        Run($FileList[$i])      
        Sleep(100)
        ;MsgBox(0, $i, $FileList[$i])
Next

Share this post


Link to post
Share on other sites

Hey I'm all about simple solutions, however when I use this I get an error about an internal command not recognized because the batch file has command lines in it. I can run them individually and it works fine. Thanks for helping!

I'm curious what internal command isn't recognized? I've tried it with multiple batch files with varying internal and external commands without any problems. Could you change the /c to a /k and see what it is reporting in the command prompt?

Share this post


Link to post
Share on other sites

Am I closer? lol This is killing me!

#Include <File.au3>
#Include <Array.au3>
$sPath = "\\serverName\wol$\"
$sFilter = "*.bat"
$FileList=_FileListToArray($sPath, $sFilter)
;_ArrayDisplay($FileList,"$FileList")

For $i = 1 To $FileList[0]
        Run($FileList[$i])      
        Sleep(100)
        ;MsgBox(0, $i, $FileList[$i])
Next

No better using the /k. An example of one of the batch files is

wolcmd 00:0F:FE:35:86:FC 10.152.1.36 255.255.255.0 8900

It's not recognizing the wolcmd, but it works if I manually run the batch file so I know there isn't anything wrong with the command.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1
 Author:         ZacUSNYR

 Script Function:
    Get Batch Files from a Directory

#ce ----------------------------------------------------------------------------

#Include <File.au3>

Global $sourceDirFileList
Global $sourceDir = "C:\temp"
Dim $MsgBoxAnswer

$sourceDirFileList = _FileListToArray($sourceDir, "*.bat", 1)

For $i = 1 To $sourceDirFileList[0]
    $MsgBoxAnswer = MsgBox(36, "Run Batch File?", "Do you wish to run batch file : " & $sourcedir & Chr(92) & $sourceDirFileList[$i])
    Select
       Case $MsgBoxAnswer = 6
            Run(@ComSpec & ' /c "' & $sourcedir & Chr(92) & $sourceDirFileList[$i] & '"')
       Case $MsgBoxAnswer = 7
           MsgBox(0, "Skipped", "Skipped batch file : " & $sourceDirFileList[$i])
    EndSelect
Next

That's how i'd go about it. Need to remember the Array is saving the file name but it's not saving the path. So if you're not running your script in that path it will not work.

Edited by ZacUSNYR

Share this post


Link to post
Share on other sites

No better using the /k. An example of one of the batch files is

wolcmd 00:0F:FE:35:86:FC 10.152.1.36 255.255.255.0 8900

It's not recognizing the wolcmd, but it works if I manually run the batch file so I know there isn't anything wrong with the command.

wolcmd.exe is not a built-in command, and its location either needs to be in the "Path" environment variable, or the file needs to be where the batch files are at, or the batch files themselves could be edited to have full paths to that program. The reason it probably works when you run the batch file manually depends on where you are running the batch file from and where wolcmd.exe exists.

You can also keep the current folder and run the command prompt from there using a slight tweak:

Run(@ComSpec&' /c for %f in ("'&$sFolderToUse&'\*.bat") do "%f"')

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0