Jump to content

help with FileReadLine/FileWriteLine..


Recommended Posts

i am a n00b with no programming knowledge but i managed to make a couple of working scripts with autoit ... but now i am stuck...

i am trying to get a script that would write all the possible number combinations consisting of X digits to a file...

for example all possible 4-digit combinations and write them to a file like this...

0000
0001
...etc
9998
9999

i think it like permutations ... but i dont know how to do it ...

so, i though that the closest method (that i can do) is to write a script in which i choose a number to start with and get the script to add 1 until reaching a number that i chose to finish with ...

so in the mentioned example i would set 1000 as the start and 9999

here is what i came up with....

#include <File.au3>

$Start= InputBox("Enter Number to Start With", "Start With:")
$End= InputBox("Enter Number to End With", "End With:")
$File = @ScriptDir & "\file.txt"
_FileCreate($File)
FileOpen ($File, 1)
FileWriteLine ($File, $Start)
FileClose ($File)
Do
FileOpen ($File, 0)
$Finish= FileReadLine ($File,1)
FileClose ($File)
FileOpen ($File, 1)
FileWriteLine ($File, $Finish + 1)
FileClose ($File)
Until $Finish= $End
MsgBox(1,"Finished", "DONE!")
Exit

but it's not working and it stays in a loop writing 1 in every line of the file

1
1
1
1
1
1
1
1
1
...etc

also, that way i would be missing 0001 to 0999 but i thought if i can get the script to work i will do from 0-9 ,10-99 and 100-999 and write another script to add the missing zeros to the start...

can someone help me make the file it with a better method or at least get this one to work....

thanks in advance

Link to comment
Share on other sites

Try this method instead:

$Start = InputBox("Enter Number to Start With", "Start With:")
$End = InputBox("Enter Number to End With", "End With:")
$File = @ScriptDir & "\file.txt"
$WriteFile = FileOpen($File, 2)
FileWriteLine($WriteFile, "Starting Number = " & $Start)
For $I = $Start To $End
    FileWriteLine($WriteFile, $I)
Next
FileClose($WriteFile)
MsgBox(1, "Finished", "DONE!")

If you want to have the 0s in front of the number for those lower than 1000, try this:

$Start = InputBox("Enter Number to Start With", "Start With:")
$End = InputBox("Enter Number to End With", "End With:")
$File = @ScriptDir & "\file.txt"
$WriteFile = FileOpen($File, 2)
FileWriteLine($WriteFile, "Starting Number = " & $Start)
For $I = $Start To $End
    Switch $I
        Case 1 to 9
            $I = "000" & $I
        Case 10 to 99
            $I = "00" & $I
        Case 100 to 999
            $I = "0" & $I
    EndSwitch
    FileWriteLine($WriteFile, $I)
Next
FileClose($WriteFile)
MsgBox(1, "Finished", "DONE!")

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

Link to comment
Share on other sites

This script answers your original intention. Someone please check my logic/math to make sure I am thinking correctly.

#include <File.au3>

Local $String
$Digits = InputBox("Enter Number digits to permutate", "Number of Digits:")
If @error Then Exit
$File = @ScriptDir & "\file.txt"
$End = 10 ^ $Digits - 1
For $i = 0 To $End
    $String &= StringFormat("%0" & $Digits & "i", $i)
    If $i <> $End Then $String &= @CRLF
Next

_FileCreate($File)
FileOpen($File, 1)
FileWrite($File, $String)
FileClose($File)
MsgBox(1, "Finished", "DONE!")

Link to comment
Share on other sites

Of course you could have eliminated your Switch with

For $i = $Start To $End
    FileWriteLine($writefile, StringFormat("%03u", $i))
Next

Edit: you can substitute "d" for the "u" in the StringFormat call

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I always forget about StringFormat. I probably could have used it in a few places in my media player...hmmm maybe I will do that after all. ;)

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

Link to comment
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
 Share

  • Recently Browsing   0 members

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