Sign in to follow this  
Followers 0
skippynz

Need help with my "For" Loop

8 posts in this topic

#1 ·  Posted (edited)

the idea is to create a webpage based on the jpg files you tell it too use. the problem im having is where it seems to only create 1 webpage and thats the last file in the array. but when i put a message box at the end of the filewriteline's its going through each of the array variables like it should. why doesnt it create all webpages - ie if you select 5 files only the last gets created.... this is soooo fustrating.

heres my code

#include <Array.au3>
#include <file.au3>
FileDelete("test.txt")

$message = "Hold down Ctrl or Shift to choose multiple files."

$var = FileOpenDialog($message, @MyDocumentsDir , "JPG Images (*.jpg)", 4 )
Dim $test

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else    
    $var = StringReplace($var, "|", @CRLF)
    $var = StringReplace($var, ".jpg","")
    FileWrite ( "test.txt", $var )
    _FileReadToArray("test.txt",  $test)
    _ArrayDisplay( $test, "Results of the array" )

EndIf

For $x = 2 To $test[0]
    FileOpen ( $test[$x] & ".htm", 2 )
    FileWriteLine( $test[$x] & ".htm" , '<html>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<head>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</head>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<body>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<div align="center"><table width="100%" height ="100%" border="1" cellspacing="0" cellpadding="0">' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<tr><td><img src="' & $test[$x] & '.jpg"></td></tr>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</table></div>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</body>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</html>' & @CRLF)  
Next

thanks for your help in advance.....PS AUTOIT is the BOMB

Edited by craig.gill

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

the idea is to create a webpage based on the jpg files you tell it too use. the problem im having is where it seems to only create 1 webpage and thats the last file in the array. but when i put a message box at the end of the filewriteline's its going through each of the array variables like it should. why doesnt it create all webpages - ie if you select 5 files only the last gets created.... this is soooo fustrating.

heres my code

#include <Array.au3>
#include <file.au3>
FileDelete("test.txt")

$message = "Hold down Ctrl or Shift to choose multiple files."

$var = FileOpenDialog($message, @MyDocumentsDir , "JPG Images (*.jpg)", 4 )
Dim $test

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else    
    $var = StringReplace($var, "|", @CRLF)
    $var = StringReplace($var, ".jpg","")
    FileWrite ( "test.txt", $var )
    _FileReadToArray("test.txt",  $test)
    _ArrayDisplay( $test, "Results of the array" )

EndIf

For $x = 2 To $test[0]
    FileOpen ( $test[$x] & ".htm", 2 )
    FileWriteLine( $test[$x] & ".htm" , '<html>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<head>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</head>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<body>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<div align="center"><table width="100%" height ="100%" border="1" cellspacing="0" cellpadding="0">' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '<tr><td><img src="' & $test[$x] & '.jpg"></td></tr>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</table></div>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</body>' & @CRLF)
    FileWriteLine( $test[$x] & ".htm" , '</html>' & @CRLF)  
Next

thanks for your help in advance.....PS AUTOIT is the BOMB

Move your file open to before the for loop, I think its wiping it every time

Edit... is it supposed to create a new file for each image is that what your doing?

Edited by ChrisL

Share this post


Link to post
Share on other sites

Move your file open to before the for loop, I think its wiping it every time

Edit... is it supposed to create a new file for each image is that what your doing?

I tried it without the file open, makes no difference - i was just trying that to see if it would resolve my issue but it didnt.

with each file having a different name it should have pulled the name and used that for the HTML file name. ie if you select the jpg files "background12.jpg" and "background13.jpg" the script strips out the .jpg and should create 2 files, one called "background12.htm" and the other should be called "background13.jpg"

Share this post


Link to post
Share on other sites

I think this works

#include <Array.au3>
#include <file.au3>
FileDelete("test.txt")

$message = "Hold down Ctrl or Shift to choose multiple files."

$var = FileOpenDialog($message, @MyDocumentsDir , "JPG Images (*.jpg)", 4 )
Dim $test

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else    
    $var = StringReplace($var, "|", @CRLF)
    $var = StringReplace($var, ".jpg","")
    FileWrite ( "test.txt", $var )
    _FileReadToArray("test.txt",  $test)
    _ArrayDisplay( $test, "Results of the array" )

EndIf

For $x = 2 To $test[0]
    $file = FileOpen ( $test[$x] & ".htm", 2 )
    FileWriteLine( $file , '<html>' & @CRLF)
    FileWriteLine( $file, '<head>' & @CRLF)
    FileWriteLine( $file , '</head>' & @CRLF)
    FileWriteLine( $file , '<body>' & @CRLF)
    FileWriteLine( $file , '<div align="center"><table width="100%" height ="100%" border="1" cellspacing="0" cellpadding="0">' & @CRLF)
    FileWriteLine( $file , '<tr><td><img src="' & $test[$x] & '.jpg"></td></tr>' & @CRLF)
    FileWriteLine( $file , '</table></div>' & @CRLF)
    FileWriteLine( $file , '</body>' & @CRLF)
    FileWriteLine( $file , '</html>' & @CRLF)
FileClose ($File)   
Next

Share this post


Link to post
Share on other sites

I think this works

i just tried but now i get this error message

New AutoIt v3 Script (2).au3 (35) : ==> Invalid file handle used.:

FileWriteLine( $file , '<html>' & @CRLF)

Share this post


Link to post
Share on other sites

if i'm understanding this right... you're just using the txt file to get them in any array. use StringSplit, it's much easier. I went ahead and tidied up your code and tested it. Works fine for me.

$message = "Hold down Ctrl or Shift to choose multiple files."

$var = FileOpenDialog($message, @MyDocumentsDir , "JPG Images (*.jpg)", 4 )
Dim $test

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else    
    $test = StringSplit($var, "|")
EndIf

For $x = 2 To $test[0]
    $myfile = FileOpen ( StringReplace($test[$x],".jpg","") & ".htm", 2 )
    FileWriteLine( $myfile , '<html>')
    FileWriteLine( $myfile, '<head>')
    FileWriteLine( $myfile, '</head>')
    FileWriteLine( $myfile , '<body>')
    FileWriteLine( $myfile , '<div align="center"><table width="100%" height ="100%" border="1" cellspacing="0" cellpadding="0">')
    FileWriteLine( $myfile , '<tr><td><img src="' & $test[$x] & '"></td></tr>')
    FileWriteLine( $myfile , '</table></div>')
    FileWriteLine( $myfile , '</body>')
    FileWriteLine( $myfile , '</html>')
Next

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

i just tried but now i get this error message

New AutoIt v3 Script (2).au3 (35) : ==> Invalid file handle used.:

FileWriteLine( $file , '<html>' & @CRLF)

It works perfectly on mine.. using the beta build though

Edit* works on the release build as well

Edited by ChrisL

Share this post


Link to post
Share on other sites

if i'm understanding this right... you're just using the txt file to get them in any array. use StringSplit, it's much easier. I went ahead and tidied up your code and tested it. Works fine for me.

$message = "Hold down Ctrl or Shift to choose multiple files."

$var = FileOpenDialog($message, @MyDocumentsDir , "JPG Images (*.jpg)", 4 )
Dim $test

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else    
    $test = StringSplit($var, "|")
EndIf

For $x = 2 To $test[0]
    $myfile = FileOpen ( StringReplace($test[$x],".jpg","") & ".htm", 2 )
    FileWriteLine( $myfile , '<html>')
    FileWriteLine( $myfile, '<head>')
    FileWriteLine( $myfile, '</head>')
    FileWriteLine( $myfile , '<body>')
    FileWriteLine( $myfile , '<div align="center"><table width="100%" height ="100%" border="1" cellspacing="0" cellpadding="0">')
    FileWriteLine( $myfile , '<tr><td><img src="' & $test[$x] & '"></td></tr>')
    FileWriteLine( $myfile , '</table></div>')
    FileWriteLine( $myfile , '</body>')
    FileWriteLine( $myfile , '</html>')
Next
this works a treat

thanks for your help guys.

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