Sign in to follow this  
Followers 0
blejd

Open Text file, modify expression, save.

7 posts in this topic

Hello, it's my first post here, so i would like to greet you all!

I tried to search forums for such a thing but couldn't find anything that suit me.

I Need to open a file with autoit, modify a text (ill provide a pattern) and swap this text with other then save.

For example, i have "text.txt" located in c: drive, i would like to open it, modify line "hello" to "hallo" and save.

Any Hints how could i achieve it?

Share this post


Link to post
Share on other sites



FileOpen

FileReadLine

FileWriteLine

FileClose

All in help file.

Also look at Loops.

Thanks for it, will it work on server system? I can't modify anything directly on disk (save directly to) So first i have to copy file on desktop, modify, save and copy back to folder on c:)

Share this post


Link to post
Share on other sites

Well i can't edit i suppose, here's another problem. How to open .js file in text editor, then search for string and replace it with another?

Share this post


Link to post
Share on other sites

To copy a file on your desktop folder, just use FileCopy, and FileCopy again to copy back.

Why do you want to open the .js file in a text editor ? You don't need to open it in a external tool, you can directly modify it with your script, using the functions that JohnOne suggest.

Share this post


Link to post
Share on other sites

Something like this?

#include <GuiListView.au3>
#include <File.au3>
#include <ListViewConstants.au3>
#include <GuiEdit.au3>
#include <GUIConstants.au3>

Local $f_Open, $f_spltStr, $f_Read, $f_Index, _
        $f_Array, $a_item

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

$f_Open = FileOpenDialog("Open file", @WorkingDir, "Txt files (*.txt)|JS files (*.js)|All files (*.*)")

$f_spltStr = StringSplit($f_Open, "\")

$f_Index = $f_spltStr[0]

$f_Read = _FileReadToArray($f_Open, $f_Array, $FRTA_ENTIRESPLIT)

$hWnd = GUICreate("File ListView", 400, 600)
$h_Listview = GUICtrlCreateListView("List", 5, 5, 300, 500, $LVS_EDITLABELS)
$h_save_button = GUICtrlCreateButton("Save", 350, 25)
GUISetState()

For $f_Index = 0 To UBound($f_Array) - 1 Step 1
    $a_item = _GUICtrlListView_AddItem($h_Listview, $f_Array[$f_Index])
    If $a_item = -1 Then
        MsgBox(0, "", "error")
    EndIf
Next

_GUICtrlListView_SetColumnWidth($h_Listview, 0, $LVSCW_AUTOSIZE)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $h_save_button
            saveArray()
    EndSwitch
WEnd

Func saveArray()
    Local $f_s_Array[1000], $i
    Local $fileOpen = FileOpen($f_Open, 2)
    For $i = 0 To _GUICtrlListView_GetItemCount($h_ListView) Step 1
        $f_s_Array[$i] = _GUICtrlListView_GetItemText($h_Listview, $i)
    Next
    For $i = 0 To _GUICtrlListView_GetItemCount($h_Listview) Step 1
        FileWriteLine($fileOpen, $f_s_Array[$i])
    Next
    FileClose($fileOpen)
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $h_ListView
    If Not IsHWnd($h_ListView) Then $hWndListView = GUICtrlGetHandle($h_ListView)
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_BEGINLABELEDITA, $LVN_BEGINLABELEDITW ; Start of label editing for an item
                    $tInfo = DllStructCreate($tagNMLVDISPINFO, $lParam)
                    _DebugPrint("$LVN_BEGINLABELEDIT" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Mask:" & @TAB & DllStructGetData($tInfo, "Mask") & @CRLF & _
                            "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->State:" & @TAB & DllStructGetData($tInfo, "State") & @CRLF & _
                            "-->StateMask:" & @TAB & DllStructGetData($tInfo, "StateMask") & @CRLF & _
                            "-->Image:" & @TAB & DllStructGetData($tInfo, "Image") & @CRLF & _
                            "-->Param:" & @TAB & DllStructGetData($tInfo, "Param") & @CRLF & _
                            "-->Indent:" & @TAB & DllStructGetData($tInfo, "Indent") & @CRLF & _
                            "-->GroupID:" & @TAB & DllStructGetData($tInfo, "GroupID") & @CRLF & _
                            "-->Columns:" & @TAB & DllStructGetData($tInfo, "Columns") & @CRLF & _
                            "-->pColumns:" & @TAB & DllStructGetData($tInfo, "pColumns"))
                    Return False ; Allow the user to edit the label
                    ;Return True  ; Prevent the user from editing the label
                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    _DebugPrint("$LVN_COLUMNCLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
                    ; No return value
                Case $LVN_ENDLABELEDITA, $LVN_ENDLABELEDITW ; The end of label editing for an item
                    $tInfo = DllStructCreate($tagNMLVDISPINFO, $lParam)
                    Local $tBuffer = DllStructCreate("char Text[" & DllStructGetData($tInfo, "TextMax") & "]", DllStructGetData($tInfo, "Text"))
                    _DebugPrint("$LVN_ENDLABELEDIT" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Mask:" & @TAB & DllStructGetData($tInfo, "Mask") & @CRLF & _
                            "-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->State:" & @TAB & DllStructGetData($tInfo, "State") & @CRLF & _
                            "-->StateMask:" & @TAB & DllStructGetData($tInfo, "StateMask") & @CRLF & _
                            "-->Text:" & @TAB & DllStructGetData($tBuffer, "Text") & @CRLF & _
                            "-->TextMax:" & @TAB & DllStructGetData($tInfo, "TextMax") & @CRLF & _
                            "-->Image:" & @TAB & DllStructGetData($tInfo, "Image") & @CRLF & _
                            "-->Param:" & @TAB & DllStructGetData($tInfo, "Param") & @CRLF & _
                            "-->Indent:" & @TAB & DllStructGetData($tInfo, "Indent") & @CRLF & _
                            "-->GroupID:" & @TAB & DllStructGetData($tInfo, "GroupID") & @CRLF & _
                            "-->Columns:" & @TAB & DllStructGetData($tInfo, "Columns") & @CRLF & _
                            "-->pColumns:" & @TAB & DllStructGetData($tInfo, "pColumns"))
                    ; If Text is not empty, return True to set the item's label to the edited text, return false to reject it
                    ; If Text is empty the return value is ignored
                    If StringLen(DllStructGetData($tBuffer, "Text")) Then Return True
                Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
                Case $NM_RETURN ; The control has the input focus and that the user has pressed the ENTER key
                    _DebugPrint("$NM_RETURN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
                Case $NM_SETFOCUS ; The control has received the input focus
                    _DebugPrint("$NM_SETFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_Text , $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text  & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Well i did it in easy way, i needed to open .js file to modify 1 string.

$szFile = "c:tempx.js"
$szText = FileRead($szFile,FileGetSize($szFile))
$szText = StringReplace($szText, "a", "b")
FileDelete($szFile)
 
that's how it works.
 
and to modify it im just copying oryiginal one to temp folder, doing edits and copy it back with override, then delting temp.
 
Thanks for advices guys, close thread.

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