Results 1 to 8 of 8

Thread: file and replace qt :)

  1. #1

    file and replace qt :)

    hello.

    I am working out a new project. will include all parts at the end in my gui with a lot of functions !. perhaps this first step is fussy or antique, but I go this way to make it. If anyone knows better way, say it. I am open to make it better. there might be better way to use replacing words by open files !

    my script includes textfile to load. but how I can change text file by open any script file if I don`t know what script I am using with ? *laugh* other things you see when I show the script.

    I suppose better use file_seek file_put or other things, then replace words. the script works, but not for replacing these two words I wished: "ZUCCHERO" into "zucherox" (only an example). see you.

    my script:

    [code=thinbasic] uses "file","console", "ui"

    dim lastString as string value "last one"
    dim newString as string value "new songs and lyrics"
    dim fileBuffer as string value ""
    dim lydFile as string value "lyd_test_song.txt"
    dim myResult as dword
    dim nfile as string
    dim sfilter, p as string
    dim numero_line, z as long
    dim nfile_putin as string value file_load(nfile)
    dim nfile_putout as string value "output_lydiaSongs.txt"
    dim my_output_text as string
    dim nfile_in_lines() as string
    dim counter as long

    printl

    '' 1 ) open file and replace words of song text file ------------------

    sfilter = "thinbasic files (*.tbasic, *.tbasicc)|*.tbasic;*.tbasicc|"
    sfilter += "all files (*.*)|*.*"
    nfile = dialog_openfile(0, "open a new file",dir_getcurrent , sfilter, "tbasic", %ofn_filemustexist or %ofn_hidereadonly or %ofn_enablesizing)
    numero_line = parsecount(nfile_putin, $crlf)
    numero_line = parse(nfile_putin, nfile_in_lines, $crlf)

    for counter = 1 to numero_line
    for z = 1 to len(nfile_in_lines(counter))
    mid$(nfile_in_lines(counter),z,1) = ucase$(mid$(nfile_in_lines(counter),z,1))
    next
    p = nfile_in_lines(counter)
    p = replace$( app_sourcepath + "lyd_test_song.txt" + p, "ZUCCHERO_", "zucherox_")
    my_output_text += p + $crlf
    next

    msgbox 0, "intermediate qt: all ok with script ?"

    file_save(app_sourcepath + nfile_putout , my_output_text)

    '' 2 ) load file and save it as copy with whole song text of zucchero -----------------

    printl
    printl "-- first work of replacing word done, I hope so !.."
    printl
    printl "-- next task.. save file, push any key "

    waitkey

    printl

    printl "hello, save my file and song text of zucchero"

    fileBuffer = file_load(lydFile)
    if lydFile = "" then msgbox 0, "good!: file was found and will save now.." end if

    fileBuffer = Replace$( fileBuffer, lastString, newString)
    if filebuffer = "" then msgbox 0, "error: no file was select and found..", %MB_ICONINFORMATION, "file error !" : stop

    myResult = file_save(app_sourcepath+"LydSavedSongCopy.txt", fileBuffer)

    printl

    if myResult = 0 then msgbox 0, "I have saved your file, it's all ok"
    else msgbox 0, "oh no, file save error!"
    printl
    printl "file was saved with success!"

    waitkey[/code]

    weapons out, please beat me!

    songtext of zucchero I include as attachement.

    good evening. lydia
    Attached Files Attached Files
    hasta la vida

  2. #2
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: file and replace qt :)

    I am not sure what your doing but this might be it

    [code=thinbasic]
    uses "file","UI"

    dim nfile as string
    dim nfileName as string
    dim sfilter as string

    sfilter = "Text files (*.txt)| *.txt|"
    sfilter += "all files (*.*)|*.*"
    nfileName = dialog_openfile(0, "open a new file",dir_getcurrent , sfilter, "Txt", %ofn_filemustexist or %ofn_hidereadonly or %ofn_enablesizing)

    IF nFileName = "" THEN MSGBOX 0, "No File Selected" : STOP ' If no file selected then exit
    nFileName = REMAIN$ ( -1,nFileName, "\" ) ' This will remove the directories
    ' and leave just the filename and extension
    nFile = File_LOAD(APP_SOURCEPATH+nFileName) ' Load the File
    IF nFile = "" THEN MSGBOX 0, "File empty" : STOP ' if the file is empty message and exit

    nFile = replace$( Ucase$(nFile), "ZUCCHERO", "zucherox") ' This will convert the text to uppercase and replace
    ' the required string
    file_save(app_sourcepath+"OutPut"+nFileName, MCASE$(nFile)) ' Save the text to a file and convert to mixed case.

    msgbox 0, "Finished"

    [/code]
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  3. #3

    Re: file and replace qt :)

    thank you michael for help! without ucase$ and mcase$ my script cannot replace my song words!

    update of my script and works very fine with replacing and changing words !

    [code=thinbasic] ''-- LYDS FILE SCRIPT OPEN AND SAVE AS COPY, 09-2009-11

    uses "file","console", "ui"

    dim lastString as string value "last one"
    dim newString as string value "new songs and lyrics"
    dim fileBuffer as string value ""
    dim lydFile as string value "lyd_test_song.txt"
    dim myResult as dword
    dim nfile as string
    dim nFileName as string

    dim sfilter, p as string
    dim numero_line, z as long
    dim nfile_putin as string value file_load(nfile)
    dim nfile_putout as string value "output_lydiaSongs.txt"
    dim my_output_text as string
    dim nfile_in_lines() as string
    dim counter as long

    printl

    msgbox 0, "TB example shows: OPEN, SAVE file as COPY, REPLACE words", %MB_Iconinformation, "lydia file & replace script "

    ''-------------------------------------------------------------------
    '' 1 ) open file and replace words of song text file
    ''-------------------------------------------------------------------

    sfilter = "thinbasic files (*.tbasic, *.tbasicc)|*.tbasic;*.tbasicc|"
    sfilter += "all files (*.*)|*.*"
    nfileName = dialog_openfile(0, "open a new file",dir_getcurrent , sfilter, "tbasic|txt", %ofn_filemustexist or %ofn_hidereadonly or %ofn_enablesizing)
    numero_line = parsecount(nfile_putin, $crlf)
    numero_line = parse(nfile_putin, nfile_in_lines, $crlf)

    if nFileName = "" then msgbox 0, "sorry, no file selected" : stop ' If no file selected then exit
    nFileName = remain$ ( -1,nFileName, "\" ) ' This will remove the directories
    ' and leave just the filename and extension
    nFile = file_load(app_sourcepath+nFileName) ' Load the File
    if nFile = "" then messagebox 0, "this file is empty" : stop ' if the file is empty message and exit

    nFile = replace$( Ucase$(nFile), "ZUCCHERO", "zucherox") ' This will convert the text to uppercase and replace
    ' the required string
    file_save(app_sourcepath+"OutPut"+nFileName, mcase$(nFile)) ' Save the text to a file and convert to mixed case.
    msgbox 0, "=> a) super: found your script, well done.."
    sleep 250
    msgbox 0, "=> b) great: finished first part and replaced words "

    file_save(app_sourcepath + nfile_putout , my_output_text)

    ''-------------------------------------------------------------------
    '' 2 ) replace words + file save as copy with whole song text of zucchero
    ''-------------------------------------------------------------------

    printl
    printl "-- first work of replacing words done, I hope so !.."
    printl
    printl "-- next task.. save your file as copy, push any key ! "
    waitkey
    printl

    printl "-- now save my file and song text of zucchero --"

    fileBuffer = file_load(lydFile)
    if lydFile = "" then msgbox 0, "good!: file was found and will save now.." end if

    fileBuffer = replace$( fileBuffer, lastString, newString)
    if filebuffer = "" then msgbox 0, "error: no file was select and found..", %MB_ICONINFORMATION, "file error !" : stop

    myResult = file_save(app_sourcepath+"LydSavedSongCopy.txt", fileBuffer)

    printl

    if myResult = 0 then msgbox 0, "I have saved your file as copy, it's all ok!", "", "your friendly robot talks to you"
    else msgbox 0, "oh no, file save error!"
    printl
    printl "file was saved with success!"

    waitkey[/code]

    I use for open "sfilter" for all scripts (*.tbasic) and I can also open all files. very good! you have choosed open *.txt files. right way too. you have forgotten to use "ui" in your header. script doesn`t work without "ui"

    learned new things. thanks. wish nice day for you.

    lydia
    Attached Files Attached Files
    hasta la vida

  4. #4

    Re: file and replace qt :)

    hi.

    is there any thinbasic way to load a file by "name" ? have done silly things, but found no correct thinbasic way with file or os module.

    how can I load for example: c:\windows\wordpad.exe ? thought there was at board a specific example for it.
    any idea martin ?

    best regards, lydia
    hasta la vida

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: file and replace qt :)

    Lydia,

    do not forget when you do this:
    [code=thinbasic]
    nFile = file_load(app_sourcepath+nFileName) ' Load the File
    [/code]

    ...then you combine script path + file path + file name. nFileName should be enough.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6

    Re: file and replace qt :)

    petr.
    yes, I know. but I can use browse_folder, file_open, but not this way to start wordpad.exe:

    "file_load(app_sourcepath+"c:\windows\notepad.exe"+nFileName2)"

    c:\windows\wordpad.exe and start it.

    missing: "file_find" command

    tried my task with os: OS_ShellExecute!

    example:

    [code=thinbasic] uses "file", "ui", "os"

    dim nFileName, nFileName2, lydiaFile, directory as string
    dim sfilter, k as string

    '' testing for finding a file -------------------------------------
    msgbox 0, "one: wait a moment"

    lydiaFile = file_load(app_sourcepath+"c:\windows\notepad.exe"+nFileName2)

    msgbox 0, "two: intermediate qt for founding errors! "

    OS_ShellExecute("open", "notepad.exe", "c:\windows\notepad.exe", directory, 1)

    nFileName2 = Dialog_BrowseForFolder(0, "Please select your directory", "C:\windows", %FALSE)

    lydiafile=file_exists("c:\windows\notepad.exe")
    msgbox 0, "found notepad exe? " + lydiaFile
    msgbox 0, "found windows folder! " + nFileName2
    msgbox 0, "my result: 1"

    ''---------------------------------------------[/code]

    that's only a test code.
    thanks.

    lydia
    hasta la vida

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: file and replace qt :)

    Hi Lydia,

    From help file for FILE_LOAD:
    Load a complete file into a string.
    So you give path to file and you get its contents. You cannot use this command for launching programs.

    Also - not all users have Windows on drive C: (I don't have), to get windows directory I can recommend use of:
    OS_GetWindowsDir
    So your script becomes:
    [code=thinbasic]
    Uses "file", "ui", "os"

    Dim nFileName, nFileName2, lydiaFile As String
    Dim exists As Long

    MsgBox 0, "Loading notepad.exe"

    lydiaFile = FILE_LOAD(OS_GETWINDOWSDIR+"notepad.exe")

    MsgBox 0, "Notepad exe is "+Format$(Len(lydiaFile), "#,")+" bytes big"

    MsgBox 0, "Opening script file in notepad.exe"

    OS_SHELLEXECUTE("open", "notepad.exe", APP_SourceName, "", 1)

    nFileName2 = DIALOG_BROWSEFORFOLDER(0, "Please select your directory", OS_GETWINDOWSDIR, %FALSE)

    exists = FILE_EXISTS(OS_GETWINDOWSDIR+"notepad.exe")

    MsgBox 0, "found notepad exe? " + IIf$(exists = %TRUE, "Yes", "No")
    MsgBox 0, "found windows folder! " + nFileName2
    [/code]
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #8

    Re: file and replace qt :)

    hello.

    [code=thinbasic]
    uses "file", "ui", "os"
    dim hwnd as dword

    dim nFileName, nFileName2, lydiaFile, directory as string
    dim sfilter, k, nFileExists,retval as string

    '' testing for finding a file -------------------------------------
    msgbox 0, "loading files and notepad.exe: wait a moment"

    lydiaFile = FILE_LOAD(OS_GETWINDOWSDIR+"notepad.exe")

    lydiaFile = file_load(OS_GETWINDOWSDIR+"notepad.exe")
    MsgBox 0, "Notepad exe is "+Format$(Len(lydiaFile), "#,")+" bytes big"
    MsgBox 0, "Opening now your script file in notepad.exe"
    OS_SHELLEXECUTE("open", "notepad.exe", APP_SourceName, "", 1)

    nFileName2 = DIALOG_BROWSEFORFOLDER(0, "Please select your directory", OS_GETWINDOWSDIR, %FALSE)

    nFileExists = FILE_EXISTS(OS_GETWINDOWSDIR+"notepad.exe")

    MsgBox 0, "found notepad exe? " + IIf$(nFileExists = %TRUE, "Yes", "No")
    MsgBox 0, "found windows folder! " + nFileName2

    lydiafile = file_exists("c:\windows\notepad.exe")
    'msgbox 0, "found notepad exe? " + lydiaFile
    'msgbox 0, "found windows folder! " + nFileName2
    'msgbox 0, "my result: 1"

    OS_ShellAbout(hWnd, "mums win xp cookies", "3", "4", 10)
    'OS_Shell(OS_GETWINDOWSDIR+"c:\windows\notepad.exe",2,%OS_SHELL_SYNC)
    OS_Shell(OS_GETWINDOWSDIR+"notepad.exe",2,%OS_SHELL_SYNC)

    '' silly ? ---------------------------------
    RetVal = OS_SHELLEXECUTE( "open new horror cabinette", "notepad.exe",APP_SourceName,"C:\windows\notepad.exe ", 1)
    msgbox 0, "value returns: " + retval

    ''---------------------------------------------
    [/code]

    this is my long solution!

    the shortest way I like very hot

    [code=thinbasic]'' lydias shortest way

    uses "os"

    OS_SHELLEXECUTE("open", "notepad.exe", APP_SourceName, "", 1)
    OS_Shell(OS_GETWINDOWSDIR+"notepad.exe",2,%OS_SHELL_SYNC)[/code]

    "OS_GetWindowsDir" is a great help. thank you petr !

    I have looked for a possibility to start a *.exe file (for example wordpad.exe or thinair.exe with one line, so I have done it. I love it

    best regards, Lydia
    Attached Images Attached Images
    Attached Files Attached Files
    hasta la vida

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •