PDA

View Full Version : file and replace qt :)



lydia_sp
11-09-2009, 18:14
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:


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

weapons out, please beat me! :blind:

songtext of zucchero I include as attachement.

good evening. lydia

Michael Clease
11-09-2009, 22:21
I am not sure what your doing but this might be it :unsure:



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"

lydia_sp
12-09-2009, 09:24
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 !


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

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

lydia_sp
14-09-2009, 13:45
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

Petr Schreiber
14-09-2009, 14:06
Lydia,

do not forget when you do this:


nFile = file_load(app_sourcepath+nFileName) ' Load the File


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


Petr

lydia_sp
14-09-2009, 18:02
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:


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"

''---------------------------------------------

that's only a test code.
thanks.

lydia

Petr Schreiber
14-09-2009, 18:19
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:


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

lydia_sp
15-09-2009, 18:28
hello.



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

''---------------------------------------------


this is my long solution!

the shortest way I like very hot :)


'' lydias shortest way

uses "os"

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

"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