Jump to content

Recommended Posts

First thing I notice is this line in multiple places...


Remove them, $ProjectLocation is a string path to a folder, not an open file handle.

Share this post

Link to post
Share on other sites

Here is a quick example on how to do something on a couple of files. Based on the examples of _FileListToArray, this loops through "o:\tmp\*.txt", and for each file does:

  1. read file contents into string
  2. convert the string to uppercase
  3. write the uppercased content to a copy of the file (x.txt -> x.txt.uppercase)

Hope this helps to answer your question.

You will need (and want) to re-organize the code. Your code goes through the motions once, and then exits. You'll want to be able to repeat the code for all of your files. To do this, you'll need to put whatever you want to do in a file into a function, which you can then call for each file. Like the function doStuffWithFile below: note that that code is in there only once, but can be called as many times as you want. Then if that is a lot of stuff, divide that into further functions. Delegating functionality into separate functions has lots of advantages: it helps keeping your code readable, it makes reorganizing functionality much easier, reduces changes of programmer error, helps troubleshooting...

My rule of thumb is to have a maximum of about half a page code in a function, say 25 lines. Hope this helps a bit.

#include <File.au3>
#include <MsgBoxConstants.au3>


Func Example()
    ; List all the files and folders in the desktop directory using the default parameters and return the full path.
    Local $aFileList = _FileListToArray("o:\tmp", "*.txt", Default, True)
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
    ; Display the results returned by _FileListToArray.
    _ArrayDisplay($aFileList, "$aFileList")

        ; let's loop through the files
    For $i = 1 To $aFileList[0]
EndFunc   ;==>Example

Func doStuffWithFile($fileName)
    $fileContent = FileRead($fileName)
    ; No need to fileClose() as $fileName was a string rather than a file handle - see FileRead "Remarks" section
    $fileContent = StringUpper($fileContent)
    $newFileName = $fileName & ".uppercase"
    FileWrite($newFileName, $fileContent)
    ; Again, no need to fileClose() for the same reason - see FileWrite "Remarks" section
EndFunc   ;==>doStuffWithFile


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post

Link to post
Share on other sites


Thanks a lot for you responses :) I am not sure respond to the each user so I am gonna respond in this post : 

@JohnOne- Thanks for the tip, yes you are correct. I am not sure why I did that (still learning), I will make the changes. 
@SadBunnyThanks a lot! I did figure our a way too loop through the files using _FileListToArray but the method you described looks neat. I am gonna make some changes and share my script so you guys can provide some feedback. 

Below given are some more issues that I have come across : 

First major issue I am running this script via remote desktop and when I minimize the remote desktop the script pauses - It wasn't doing this before it just started and I am not sure why- How can I keep the script going once started I need to process 500-600 files and I someone has to monitor the screen then that defies the whole purpose. How can I have this exception handled or have a workaround that this unwanted pause doesn't break the processing or ensure that the right window gets the focus? 

Second issue- I have searched it a lot but couldn't find a straight forward solution - How can I run this if my computer locks up when idle or run it when the computer is locked- Any workaround?

Third issue - I have random delays in the code that I don't really need I started adding them when I started writing this so I can see what's going on but now when I remove those or even remove spaces in the code to clean it up the whole thing breaks- this might sound stupid but this is happening (Tidy didnt make any difference as far as spaces are concerned) 

 Thanks in advance! 

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

  • Create New...