Page 1 of 10

Search and Replace text in a file

PostPosted: Wed Nov 13, 2013 1:55 pm
by UltraVox
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

Re: Search and Replace text in a file

PostPosted: Wed Nov 13, 2013 2:30 pm
by dpayer
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.

Re: Search and Replace text in a file

PostPosted: Wed Nov 13, 2013 2:34 pm
by dec
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]"

Re: Search and Replace text in a file

PostPosted: Wed Nov 13, 2013 2:51 pm
by UltraVox
@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

Re: Search and Replace text in a file

PostPosted: Wed Nov 13, 2013 3:07 pm
by dec
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. ;)

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 3:52 am
by UltraVox
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!

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 4:06 am
by dec
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

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 4:25 am
by UltraVox
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?

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 4:45 am
by UltraVox
No I have 4 files !
But with the path... The same thing is it possible with only file names ?

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 6:11 am
by dec
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

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 9:32 am
by UltraVox
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...

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 9:43 am
by dec
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...

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 9:55 am
by UltraVox
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:

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 10:04 am
by dec
Hello,

I understand. Unfortunatelly I can't help at this time... Perhaps are you tried with the NeoDezign Image Works Plug-In?

Re: Search and Replace text in a file

PostPosted: Thu Nov 14, 2013 10:10 am
by UltraVox
No more than your plugin. :(