Jump to content

Loop through files and open up to 4 instances of notepad.

Recommended Posts

Can someone give me an idea on how to make a script? Let me give you the layout and i'll post where i'm at and perhaps you can help me fill in the blanks.


I have a directory called c:\reports\ in this directory is X number of txt files. could be 1, could be 210. I want to make a script that will loop the files in this directory and load notepad with this file opened. Up to 4 instances at at time. At this point the script will just sit and wait while I do my work in notepad. I may or may not edit the file and close it. Once the file is closed it will open another window on the following file. I do not want to open more then 4 windows because I do not lose track of what i'm doing in each window before closing it out.

Once all the files are done eventually i'll only have the remaining three, two or one window open in which case the script should have ended since there were no more files to go through.


So this is where i'm at...

Dim $aResult
Dim $sFileName
Local $hSearch = FileFindFirstFile("C:\received\*.txt") ; the text files are what i'm interested in.
while 1 ; loop through files until there are no more to loop through?
    $sFileName = FileFindNextFile($hSearch) ; find the file?
        ; If there is no more file matching the search.
        If @error Then ExitLoop
$aResult = ProcessList("notepad.exe") ; count instances of notepad
while $aResult[0][0]    < 4 ; continue doing the below till we hit 4 instances
    $sFileName = FileFindNextFile($hSearch) ; start searching for the first txt file?
$aResult = ProcessList("notepad.exe") ; again count the instances to make sure we don't go over 4
        Run('"C:\windows\notepad.exe" "c:\received"'& $sFileName) ; launch notepad and open that text file?
        $aResult = ProcessList("notepad.exe") ; again count the instances to make sure we don't go over 4



In my mind I can see this making sense but in reality it does almost nothing I am trying to achieve.

1. It opens up 4 instances regardless if there are less than 4 files to be opened.

2. It opens blank pages and not the file I am interested in.

3. Once it opens 4 instances, it closes out like there are no more files to be parsed.


:( can anyone help me decipher this or am I asking for something too complicated.




Thank you for your help!

Link to post
Share on other sites

You want to open up notepad based on the number of files, not instances of notepad, right?   Look at _FileListToArray with a text filter.  Loop those and run them.  Also, you are missing a "\" before filename.  It should be:

"c:\received\"& $sFileName"

With the file list from the above function you can do:

Local $aFileList = _FileListToArray(@DesktopDir, "*txt*",1)
  for $a=1 to ubound($aFileList)-1; first value in array at index 0 is # of files
   [do something]


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to post
Share on other sites

No not quite. What I'm trying to do is loop through the files and open up notepad on each file but to not open up more then 4 instances at a time. Once 4 windows are opened it will wait until I close one or more of the notepads in which case it will continue looping through the files and open more notepads but never exceed 4. 


Example there are 82 txt files in a folder. They can be named anything. The script will loop files and open notepad then find the next file and open notepad etc . Once it hits 4 processes (thus opened 4 files) it will sit and idle. When I'm done making my changes I will save and exit. The script will see that there are now only three instances and open another file (as it loops through all the files) and this process continues until all files have been looped. So in essence loop through all files and open all files in notepad but once the process count of notepad.exe equals 4 then pause the loop until the process count is less then 4 at which point resume till we hit 4 again. 

Link to post
Share on other sites

I'm not sure of what you need, but try this (look at the console in Scite)

#Include <File.au3>

Local $iMaxNotepad = 4
Local $aPids[$iMaxNotepad], $iFileIndex = 0

Local $aFiles = _FileListToArray("c:\received", "*txt", 1, True)

While $iFileIndex < $aFiles[0]
    For $i = 0 To $iMaxNotepad - 1
        If $aPids[$i] <> "" AND Not ProcessExists($aPids[$i]) Then
            $aPids[$i] = ""
            ConsoleWrite("PID=" & $aPids[$i] & " closed." & @CRLF)
        If $aPids[$i] = "" Then
            $iFileIndex += 1
            $aPids[$i] = Run("notepad " & $aFiles[$iFileIndex])
            ConsoleWrite("Opening " & $aFiles[$iFileIndex] & " (PID=" & $aPids[$i] & ")" & @CRLF)


Edited by jguinch
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...