Source language: Translate to:

Search and Replace text in a file

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Search and Replace text in a file

Postby UltraVox » Wed Nov 13, 2013 1:55 pm

Hello everyone,

Not having to use advanced functions of NeoBook long enough, I can not remember how to Find and Replace a specific word which is a specific line in a file. This is a DirectX file (.X) which can be read as a .TXT (with Notepad for example).

At line 10 are the following :
Code: Select all
  "bottom.dds";


So two spaces, followed by "bottom.dds";
I need to change the text bottom.dss to something else, without affecting the rest, but I do not...

Does someone has an idea on how to proceed ? I'm lost with scripts Str...

A BIG thank you in advance :)

UltraVox
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dpayer » Wed Nov 13, 2013 2:30 pm

UltraVox wrote:Hello everyone,

Not having to use advanced functions of NeoBook long enough, I can not remember how to Find and Replace a specific word which is a specific line in a file. This is a DirectX file (.X) which can be read as a .TXT (with Notepad for example).

At line 10 are the following :
Code: Select all
  "bottom.dds";


So two spaces, followed by "bottom.dds";
I need to change the text bottom.dss to something else, without affecting the rest, but I do not...

Does someone has an idea on how to proceed ? I'm lost with scripts Str...

A BIG thank you in advance :)

UltraVox


Try this:
Code: Select all
FileWrite "[pubdir]DirectXfile" "10" "[newdata]"


where the file is in the pubdir and is named DirectXfile. The new content of that line would be placed in the variable: [newdata]

I didn't test this so you better see if it works for you.

David P.
David Payer
Des Moines, Iowa
USA
User avatar
dpayer
 
Posts: 1384
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: Search and Replace text in a file

Postby dec » Wed Nov 13, 2013 2:34 pm

Hello,

You can use "FileRead", "StrReplace" and "FileWrite" actions like this:

Code: Select all
.Read the file contents
FileRead "[PubDir]Sample.txt" "All" "[FileContents]"

.Replace all ocurrences of "bottom.dds" to "something.dss"
StrReplace "[FileContents]" "bottom.dds" "something.dss" "[FileContents]" ""

.Save the replaced contents into the file
FileWrite "[PubDir]Sample.txt" "All" "[FileContents]"
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Search and Replace text in a file

Postby UltraVox » Wed Nov 13, 2013 2:51 pm

@dec

Hum? It was that simple? lol
it works! You saved my life ;)

Thank you very much!

@dpayer

The FileWrite control was insufficient... But thank you anyway. :)

Thanks all!
UltraVox
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dec » Wed Nov 13, 2013 3:07 pm

Hello,

UltraVox wrote:@dec

Hum? It was that simple? lol
it works! You saved my life ;)


No problem. If you want more power take a look at the Regular Expression world. But probably this time "StrReplace" can be sufficient for the task. ;)
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Search and Replace text in a file

Postby UltraVox » Thu Nov 14, 2013 3:52 am

David,
Thank you for your plugin, I'll watch it!

Do you know how to display in the List Zone, all files selected by the user ?

First I open a dialog box (FileOpenBox) with Multiple choice, and I stored the selected file(s) in the variable [SelectedImages].

I tested the "StrParse" command, but I can not seem to get the list of selected file(s) for the show in a List Zone.

Once again, I'm sure it will be incredibly simple ? :mrgreen:
Thank you for your help!
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dec » Thu Nov 14, 2013 4:06 am

Hello UltraVox,

Try something like this:

Code: Select all
.Let the user to choose one or more files (any kind of files)
FileOpenBox "Choose some files" "All files|*.*" "[PubDir]" "[SelectedFiles]" "Multiple"

.Parse the FileOpenBox result variable to get all the selected file paths
StrParse "[SelectedFiles]" ";" "[FilePaths]" "[FilesCount]"

.Iterate over all selected files
Loop "1" "[FilesCount]" "[I]"

  .And finally add to the List Box the file paths one by one
  ListBoxAddItem "SelectedFilesListBox" "0" "[FilePaths[I]]"
EndLoop
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Search and Replace text in a file

Postby UltraVox » Thu Nov 14, 2013 4:25 am

Thank you David!
But there are some problems. I just want to display the names of files. Not the file path. And when I select 4 files, only 2 files appear in the list.
Do you have any idea?
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby UltraVox » Thu Nov 14, 2013 4:45 am

No I have 4 files !
But with the path... The same thing is it possible with only file names ?
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dec » Thu Nov 14, 2013 6:11 am

Hello,

Of course. Just use the "ExtractFileName" action:

Code: Select all
.Let the user to choose one or more files (any kind of files)
FileOpenBox "Choose some files" "All files|*.*" "[PubDir]" "[SelectedFiles]" "Multiple"

.Parse the FileOpenBox result function to get all the selected file paths
StrParse "[SelectedFiles]" ";" "[FilePaths]" "[FilesCount]"

.Iterate over all selected files
Loop "1" "[FilesCount]" "[I]"

  .Extract just the file name from the path
  ExtractFileName "[FilePaths[I]]" "[FileName]"

  .And finally add to the List Box the file names one by one
  ListBoxAddItem "SelectedFilesListBox" "0" "[FileName]"
EndLoop


Maybe you want to shown the file names without extensions. This can be does with:

Code: Select all
.Let the user to choose one or more files (any kind of files)
FileOpenBox "Choose some files" "All files|*.*" "[PubDir]" "[SelectedFiles]" "Multiple"

.Parse the FileOpenBox result function to get all the selected file paths
StrParse "[SelectedFiles]" ";" "[FilePaths]" "[FilesCount]"

.Iterate over all selected files
Loop "1" "[FilesCount]" "[I]"

  .Extract just the file name from the path
  ExtractFileName "[FilePaths[I]]" "[FileName]"

  .Remove the file extension from the name
  ChangeFileExt "[FileName]" "" "[FileName]"

  .And finally add to the List Box the file names one by one
  ListBoxAddItem "SelectedFilesListBox" "0" "[FileName]"
EndLoop
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Search and Replace text in a file

Postby UltraVox » Thu Nov 14, 2013 9:32 am

Excellent! Thank you very much !

I am looking for a plugin that can read (and open in NeoBook) the Image.dds files

You know one? Because it's great to convert but it's even better to read before converting.
I have not found anything yet...
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dec » Thu Nov 14, 2013 9:43 am

Hello,

If you refer to "Direct Draw Surface" files (.dds) I only can say that my npImgs plugin can convert from/to this format. This is not exactly what you wanted but maybe can help you in some manner.

UltraVox wrote:Excellent! Thank you very much !

I am looking for a plugin that can read (and open in NeoBook) the Image.dds files

You know one? Because it's great to convert but it's even better to read before converting.
I have not found anything yet...
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Search and Replace text in a file

Postby UltraVox » Thu Nov 14, 2013 9:55 am

Yes David, I use your plugin to convert, but I also need to display the .DDS image. The .DDS files are hyper used in the world of video games (SkyBox, Surface Mapping, etc ...) and I have no tools to display a DDS file, unless I convert. I asked this feature to Dave, there are years of it, but not back :cry:
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dec » Thu Nov 14, 2013 10:04 am

Hello,

I understand. Unfortunatelly I can't help at this time... Perhaps are you tried with the NeoDezign Image Works Plug-In?
.
Enhance your NeoBook applications!
.
58 plugins, 1131 actions and 233 samples
.
NeoPlugins website: www.neoplugins.com
.
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Re: Search and Replace text in a file

Postby UltraVox » Thu Nov 14, 2013 10:10 am

No more than your plugin. :(
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Next

Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 1 guest