Jump to content

FileWrite question


leevy
 Share

Recommended Posts

hello,

I have a question about FileWrite. I want to write a string into a file, the string is got from a xml file, please see the following:

aaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbbb

ccccccccccc

I useed ConsoleWrite and got the string, the value is the same as above. but, it will show as "aaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccccccccccc" when I write it into a file.

my script is:

$file = FileOpen("test.txt", 2)
FileWrite($file, $string)
FileClose($file)

hope your help, thanks.

Link to comment
Share on other sites

FileWrite just writes your string as-is. You need to insert @CR or @LF or @CRLF to write on a new line.

Either that or just use FileWriteLine. It's equivalent to FileWrite accompanied with a following line feed.

I used FileWriteLine, still show: "aaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccccccccccc"
Link to comment
Share on other sites

hello,

I have a question about FileWrite. I want to write a string into a file, the string is got from a xml file, please see the following:

aaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbbb

ccccccccccc

I useed ConsoleWrite and got the string, the value is the same as above. but, it will show as "aaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccccccccccc" when I write it into a file.

my script is:

$file = FileOpen("test.txt", 2)
FileWrite($file, $string)
FileClose($file)

hope your help, thanks.

How are you getting it from the xml file and how is it stored before you use ConsoleWrite.

I am guessing the new line or carriage returns are removed in the process. Post the xml (a sample will do) and how you extract the data. ConsoleWrite will write exactly what it is given so I don't think that is the problem.

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

this is an example:

#include <GuiConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <IE.au3>
#include <_XMLDomWrapper.au3>
#include <File.au3>
#include <GuiTab.au3>
#Include <string.au3>

Global $sFilepath, $readme

_Main()

Func _Main()
    
Global $parent, $tabitem2
    ; Create the GUI window and controls
    $parent = GUICreate("Wing 1.3", 400, 500, (@DesktopWidth - 400)/ 2, (@DesktopHeight - 500) / 2)
    $OLD = _SetThemes(0)
    GUISetIcon("Wing.ico")

    $tab = GUICtrlCreateTab(10, 10, 382, 440,0x00000020)
    _SetThemes($OLD)

    $tabitem1 = GUICtrlCreateTabItem("aaa")
    
    $tabitem2 = GUICtrlCreateTabItem("bbb")

;wing1.3
;------------------------------------------------------------------------------------------------------
    GUICtrlCreateTabItem("readme")

    $createreadme = GUICtrlCreateButton("Create readme", 60, 465, 130, 20)
;------------------------------------------------------------------------------------------------------

    
    GUICtrlCreateTabItem("")
    
    
    $pathlabel = GUICtrlCreateLabel("File path:", 22, 405, 80, 20, 0x0200)
    $hFile = GUICtrlCreateInput($sFilepath, 105, 405, 245, 20,0x0080)
    $hFileSel = GUICtrlCreateButton("...", 355, 405, 25, 20)

    
    $cancelbutton = GUICtrlCreateButton("Cancel", 260, 465, 60, 20)
    $sub = GUICreate("", 370, 340, 0, 0, $WS_POPUP + $WS_VSCROLL, $WS_EX_MDICHILD, $parent)
    
    ; Run the GUI until it is closed
    GUISetState(@SW_SHOW, $parent)

    While 1
        $msg = GUIGetMsg()
            
            Select
                Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
                    ExitLoop                
                case $Msg = $hFileSel
                    $sTmpFile = FileSelectFolder("Please Select the directory", "")
                    If @error Then ContinueLoop
                    GUICtrlSetData($hFile, $sTmpFile)                   
                Case $msg = $createreadme
                    $sActFile = GUICtrlRead($hFile)
                        
                    $file = FileOpen($sActFile&"\"&"readme.txt",2)
                    $readme = _Getxmlvalue("resource2.xml", "/wing/readme/page")
                    $header = _Getxmlvalue("resource2.xml", "/wing/readme/PM/aaaaa/header")
                    $readme = stringReplace($readme, "$$header##", $header)
                    ConsoleWrite($readme)
                    FileWriteLine($file, $readme)
                    FileClose($file)
                    if $sActFile = "" Then
                        MsgBox(4096,"message","Please select directory!",10)
                    Else
                        MsgBox(4096,"message","Create successfully",10) 
                    EndIf           
            
            EndSelect
    WEnd
    
    Exit
EndFunc   ;==>_Main

;~ get number from a string
Func _Getnumber($string)
    $numberstring = ""
    $j = StringLen($string)
    for $i = 1 to $j
        $tmpstring = StringTrimRight ($string, $j - $i)
        $char = StringTrimLeft($tmpstring, $i - 1)
        If StringIsDigit($char) > 0 Then
            $numberstring = $numberstring&$char
        EndIf
    Next
    Return $numberstring
EndFunc


Func _Getxmlnodes($xmlfilename,$path)
    $template = ""
    $tmpvalue = ""
    $sXML = $xmlfilename    
    If _XMLFileOpen($sXML,"",-1) = -1 Then
        ConsoleWrite("can′t open file!" & @CRLF)
        Exit
    EndIf
    $tmpvalue = _XMLSelectNodes($path)
    $template = $tmpvalue[1]
    for $i = 2 to UBound($tmpvalue) - 1
        $template =$template&"|"&$tmpvalue[$i]
    Next
EndFunc

Func _Getxmlvalue($xmlfilename,$path)
    $tmpvalue = ""
    $sXML = $xmlfilename    
    If _XMLFileOpen($sXML,"",-1) = -1 Then
        ConsoleWrite("can′t open file!" & @CRLF)
        Exit
    EndIf
    $tmpvalue = _XMLGetValue($path)
    Return $tmpvalue[1]
EndFunc



; Count the number of times substring occurs in string
Func _StringCount($string, $substring)
   Local $i, $count = 0
   For $i = 1 to StringLen($string)
      If StringMid($string, $i, StringLen($substring)) = $substring Then $count = $count + 1
   Next
   Return $count
EndFunc


Func _getreportnum($IE)
    $reportnum = ""
    _IELoadWait($IE)
    ; get bug report number
    $reporttext = $IE.document.body.innertext
    $reporttextlist = StringSplit($reporttext, @LF)
    for $i in $reporttextlist
        if StringInStr($i, "ReportID:") > 0 then
            $reportnum = _Getnumber($i)
            return $reportnum
        EndIf   
    Next
EndFunc

Func _SetThemes($Style)
    If @OSType = "WIN32_WINDOWS" Or $Style=-1 Then Return SetError(1,0,-1)
    Local $ret = DllCall("uxtheme.dll", "dword", "GetThemeAppProperties")
    If Not IsArray($ret) Then Return SetError(1,0,-1)
    DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "dword", $Style)
    Return $ret[0]
EndFunc

this is xml:

<?xml version="1.0" encoding="UTF-8" ?> 
- <wing>
- <report>
- <example1>
  </example1>
- <example2>
+ <bugreport>
  <title>test title 2</title> 
  <sql>TEST $$SQL## 2$$title## 2 $$teastaset## asdfa</sql> 
  </bugreport>
  </example2>
  </report>
- <readme>
- <PM>
- <aaaaa>
  <header>[Owner Name] : aaaa bbbb [Owner Phone] : 111-222-3333 [Owner Emergency Phone] : 222-222-4444 [Owner Email] : aaaa.bbbb@test.com</header> 
  </aaaaa>
  </PM>
  <page>$$header## DATE ============================================== [Date of Release] DATE : $$releasedate## RELEASE NUMBER ============================================== [Release # on J-Drive] $$releasenum## RELEASE NAME ============================================== [Brief description of this release] $$memoname## MEMO NUMBER ============================================== $$memonum## PATCH TYPE ============================================== [Description of this type of release PF-Page Full, PP-Page Partial, CF-Client Full, CP-Client Partial, DBP-Database Patch] PP CONTENTS ============================================== [List all the contents of this release includes all packages and other files] Refer to $$releasenum##_fileList.txt for contents BACKUP INSTRUCTIONS ============================================== [Provide backup recommendation and instructions including command and directory names] 1. Backup will be finished automatically after "psoinstaller.sh" run. 2. Backup files' path is: /backup/psobackup/$$releasenum## ROLLBACK INSTRUCTIONS ============================================== [Provide rollback recommendation and instructions including command and directory names] 1. Go to backup directory: cd /backup/psobackup/$$releasenum## 2. Run roll back script: ./psorollback.sh INSTALLATION INSTRUCTION ============================================== 1. Mount to "/backup"(Standard Implementation directory) 2. Copy the whole "$$releasenum##/" to "/backup" 3. Go to directory "/backup/$$releasenum##/install-rollback/", Change attributes for the following two scripts : cd /backup/$$releasenum##/install-rollback chmod +x psoinstaller.sh chmod +x psorollback.sh 4. Run "psoinstaller.sh" to start installation: ./psoinstaller.sh Please input your site name when it prompts "Please enter site name:" $$histroydependency## Please input "y" when it prompts "Configuration instructions for the release $$releasenum## implemented?"(Please make sure config file is correct before input "y") 5. To create multi-language sites or other sites, you need to run: ./psoinstaller.sh -cs and input site name when it prompts "Please enter site name:".(1 site name every time) 6. To delete existing site name: cd /backup/psobackup/$$releasenum## ./psorollback.sh [existing site name] Input "y" when it prompts "Are you sure you want to remove the soft links for the [site name]?"(1 site name every time) 7. Redeploy the $$train## application after installation finishes. CONFIGURATION INSTRUCTION ============================================== N/A SPECIAL CUSTOMER URLS ============================================== [Provide list of special customer URLs that this patch is to be deployed to] Please refer to RT SPECIAL SITE VERIFICATION PROCEDURE ============================================== [Provide special site verification procedure for this patch if can't use standard procedure] N/A SUPERSEDES ============================================== [Provide list of releases that this patch will supersede] N/A DEPENDENCIES ============================================== [Provide specific dependency information to other releases] $$readmedependenciesdata## MISCELLANEOUS ============================================== [Provide other important information about this release] Service down time: (minutes) No Server program restart? Y/N No Server machine reboot? Y/N No Customer impact? Low PACKAGE SOURCE ====================================================== [Provide information about what is included in this release] Packages included: page PRIMARY CONTACTS =========================================================== [Provide primary contact information for this release. Should include lead for each Component and Service] $$footer##</page> 
  </readme>
  </wing>

maybe @LF is notavailable in txt?

Edited by leevy
Link to comment
Share on other sites

leevy, there are so much things going wrong. Consider the following:

$a = "aaaa"
$b = "bbbb"
$c = "cccc"

$h = FileOpen("test.txt", 2)
FileWrite($h, $a) ; writes into file: aaaa    entire file content: aaaa
FileWrite($h, $b) ; writes into file: bbbb    entire file content: aaaabbbb
FileWrite($h, $c) ; writes into file: cccc    entire file content: aaaabbbbcccc
FileCLose($h)

A line ends in Windows with two characters after eachother: a CR and LN. In AutoIt you can get these characters together from a simple macro: @CRLF.

Now consider the following:

$a = "aaaa"
$b = "bbbb"
$c = "cccc"

$h = FileOpen("test.txt", 2)
FileWrite($h, $a & @CRLF) ; writes into file: aaaaCRLF    entire file content: aaaaCRLF
FileWrite($h, $b & @CRLF) ; writes into file: bbbbCRLF    entire file content: aaaaCRLFbbbbCRLF
FileWrite($h, $c & @CRLF) ; writes into file: ccccCRLF    entire file content: aaaaCRLFbbbbCRLFccccCRLF
FileCLose($h)

This will show the following file:

aaaa
bbbb
cccc

Mac operating system only uses LF to indicate a new line.

That answers your first post!

Now for your second post:

I used FileWriteLine, still show: "aaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccccccccccc"

FileWriteLine does the same as FileWrite, but it places CR and LF characters at the end of the string to write. This is how you use it:

$a = "aaaa"
$b = "bbbb"
$c = "cccc"

$h = FileOpen("test.txt", 2)
FileWriteLine($h, $a) ; writes into file: aaaaCRLF    entire file content: aaaaCRLF
FileWriteLine($h, $b) ; writes into file: bbbbCRLF    entire file content: aaaaCRLFbbbbCRLF
FileWriteLine($h, $c) ; writes into file: ccccCRLF    entire file content: aaaaCRLFbbbbCRLFccccCRLF
FileCLose($h)

And again, this is what it displays:

aaaa
bbbb
cccc

If you do it like this, however:

$a = "aaaa"
$b = "bbbb"
$c = "cccc"

$h = FileOpen("test.txt", 2)
FileWriteLine($h, $a & $b & $c) ; writes into file: aaaabbbbccccCRLF    entire file content: aaaabbbbccccCRLF
FileCLose($h)

Then it will write into the file aaaabbbbccccCRLF. Which is not what you wanted, presumably.

Now for your last post. In there is so much code that it is confusing to see what you actually wanted to do. If I don't understand your code, then there's a good chance the computer won't understand it either.. So try to explain what it is you're trying to do in a language not directly understandable by a computer and see if that helps.

Link to comment
Share on other sites

Thanks all, I understand how FileWrite and FileWriteLine work now.

I thought out an idea to solve this problem.

$readme = stringReplace($readme, @LF, @CRLF)
                    FileWrite($file, $readme)

It works well now.

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...