Source language: Translate to:

Search and Replace text in a file

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Re: Search and Replace text in a file

Postby Gaev » Mon Nov 18, 2013 11:38 am

UltraVox:
Well, I do not know if "dir" works because the DOS window launches and closes instantly...
... perhaps there are no (or too few) files in the folder ... because when I tried on my machine, I saw a fast scroll of the files (info).

Try and do something like ...
Code: Select all
Run "dir" "[PubDir]Data\Temp\ImgSb /s > C:\dirlist.txt" "Normal" "" ""
... which will send the output to the specified file ... which you can then review.
User avatar
Gaev
 
Posts: 3736
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Search and Replace text in a file

Postby UltraVox » Mon Nov 18, 2013 11:57 am

The file Dirlist.txt is created in C:\, but it is empty. :(
Yet, ImgSb directory contains top.jpg

If you can give me an example of code that works with :

Code: Select all
- use FileList to get the list of files in the folder
- do a Loop/EndLoop block to FileErase each file in the List
- then do the RemoveFolder


I'm interested because I do not want to keep bothering you with DOS...
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby CN_Iceman » Mon Nov 18, 2013 12:08 pm

To get file lists I usually use this command...

Code: Select all
SetVar "[Command_DOS]" "[#34][Search_Folder]*.pdf[#34] /AA /S /B > [#34][PubDir]result.txt[#34]"
Run "DIR" "[Command_DOS]" "Wait+Hidden" "" ""


As you can see, the example looks for PDF files in [Search_Folder] and stored in [PubDir]result.txt

I use the /AA, /S and /B modifiers to get a clean and simple list of filenames with no extra data.
/AA is for show files with the Archive attribute.
/S is for recursive subfolders.
/B is for simple format (no header and no summary).

Hope this helps.

Greetings.
Greetings/Saludos, Jose.
www.icemansoft.es
User avatar
CN_Iceman
 
Posts: 297
Joined: Tue Mar 01, 2011 11:04 am
Location: España

Re: Search and Replace text in a file

Postby Gaev » Mon Nov 18, 2013 12:22 pm

UltraVox:

I just tried ...
Code: Select all
AlertBox "DirList" "[PubDir]xxxx"
Run "dir" "[PubDir]xxxx /s /p" "Normal" "" ""
Run "dir" "[PubDir]xxxx /s > c:\dirlist.txt" "Normal" "" ""
... where xxxx is a valid folder ... and everything works as expected.

1) Insert the AlertBox command as I have ... to verify that the value is exactly what you expect

2) If your value contains spaces, as per suggestion of CN_Iceman, place double quotes([#34]) around the folder name
User avatar
Gaev
 
Posts: 3736
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Search and Replace text in a file

Postby CN_Iceman » Mon Nov 18, 2013 12:23 pm

UltraVox wrote:
Code: Select all
- use FileList to get the list of files in the folder
- do a Loop/EndLoop block to FileErase each file in the List
- then do the RemoveFolder



Try with this:

Code: Select all
FileList "F:\Temp\*.jpg" "Files" "[Found_Files]"
StrParse "[Found_Files]" "[#13]" "[File]" "[Total]"

Loop "1" "[Total]" "[y]"
     FileErase "F:\Temp\[File[y]]"
EndLoop

RemoveFolder "F:\Temp"


My example looks for jpg files in F:\Temp, after that, delete all the jpg files, and finally, delete de F:\Temp folder.
You must change the parameters in order to obtain your needs.

Greetings.
Last edited by CN_Iceman on Mon Nov 18, 2013 12:41 pm, edited 1 time in total.
Greetings/Saludos, Jose.
www.icemansoft.es
User avatar
CN_Iceman
 
Posts: 297
Joined: Tue Mar 01, 2011 11:04 am
Location: España

Re: Search and Replace text in a file

Postby UltraVox » Mon Nov 18, 2013 12:39 pm

You are all wonderful :mrgreen:
Thank you all !!! :wink:
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby CN_Iceman » Mon Nov 18, 2013 12:42 pm

A pleasure to help.

Greetings.
Greetings/Saludos, Jose.
www.icemansoft.es
User avatar
CN_Iceman
 
Posts: 297
Joined: Tue Mar 01, 2011 11:04 am
Location: España

Re: Search and Replace text in a file

Postby UltraVox » Fri Nov 29, 2013 1:16 am

Hello all,

I use IfEx command to determine whether the state of 6 buttons correspond to what I want to get. I need to know what files currently selected in the 6 buttons :

Code: Select all
GetObjectInfo "BoutonTop" "FileName" "[TopResult]"
GetObjectInfo "BoutonLeft" "FileName" "[LeftResult]"
GetObjectInfo "BoutonFront" "FileName" "[FrontResult]"
GetObjectInfo "BoutonRight" "FileName" "[RightResult]"
GetObjectInfo "BoutonBack" "FileName" "[BackResult]"
GetObjectInfo "BoutonDown" "FileName" "[DownResult]"


Then I want to determine whether these six variables contain a different file name from "SC180.png". And if the 6 variables are different "SC180.png" then I trigger a command.

Code: Select all
IfEx "[TopResult] <> SC180.png AND [LeftResult] <> SC180.png AND [FrontResult] <> SC180.png AND [RightResult] <> SC180.png AND [BackResult] <> SC180.png AND [DownResult] <> SC180.png"
    AlertBox "Action" "Action 1"
Else
    AlertBox "Action" "Action 2"
EndIf


Obviously it does not work. The result is always the same (Action 1), even when the contents of variables is different. I certainly misunderstood IfEx... Can you tell me where is the problem please ?

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

Re: Search and Replace text in a file

Postby HPW » Fri Nov 29, 2013 2:53 am

This works for me as expected:

Comment/Uncomment either of the setvar-blocks

Code: Select all
.SetVar "[TopResult]"   "SC181.png"
.SetVar "[LeftResult]"  "SC181.png"
.SetVar "[FrontResult]" "SC181.png"
.SetVar "[RightResult]" "SC181.png"
.SetVar "[BackResult]"  "SC181.png"
.SetVar "[DownResult]"  "SC181.png"

SetVar "[TopResult]"   "SC180.png"
SetVar "[LeftResult]"  "SC180.png"
SetVar "[FrontResult]" "SC180.png"
SetVar "[RightResult]" "SC180.png"
SetVar "[BackResult]"  "SC180.png"
SetVar "[DownResult]"  "SC180.png"

IfEx "[TopResult] <> SC180.png AND [LeftResult] <> SC180.png AND [FrontResult] <> SC180.png AND [RightResult] <> SC180.png AND [BackResult] <> SC180.png AND [DownResult] <> SC180.png"
    AlertBox "Action" "Action 1"
Else
    AlertBox "Action" "Action 2"
EndIf


This triggers action 2 because all are different!
??
Hans-Peter
User avatar
HPW
 
Posts: 2520
Joined: Fri Apr 01, 2005 11:24 pm
Location: Germany

Re: Search and Replace text in a file

Postby UltraVox » Fri Nov 29, 2013 3:03 am

The problem is solved !
I thought :
Code: Select all
GetObjectInfo "BoutonTop" "FileName" "[TopResult]"

"FileName" was indeed a file name, but in reality it is the full path + filename.
Sorry for the inconvenience...

Thank you HPW!
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby UltraVox » Mon Dec 02, 2013 1:24 am

Hello all,

Here is the script that adds the user-selected files (images only) in a list.

Code: Select all
If "[Groupe_AddType]" "=" "File(s)"
. Open a window for selecting multiple files
  FileOpenBox "Select one or more images files" "[InputMaskConvert]" "[MyDocuments]" "[SelectedFiles]" "Multiple"
. Separate number
  StrParse "[SelectedFiles]" ";" "[FilePaths]" "[FilesCount]"
. Calculate the number of items present in the list
  ListBoxSize "ZoneImagesOriginales" "[ListNbr]"
. Addition on the list items + the number of selected files
  Math "[ListNbr]+[FilesCount]" "-1" "[TotalNbrList]"
  Loop "1" "[FilesCount]" "[I]"
. Extract selected file names
  ExtractFileName "[FilePaths[I]]" "[FileName]"
. Add them to the list
  ListBoxAddItem "ZoneImagesOriginales" "0" "[FilePaths[I]]"
  EndLoop
EndIf


The result is a list of selected files: path + filename.

Now, I try to avoid duplicates. If one of the selected files is already in the list, then it is useless to add it.
Do you know how proceed, please ?

Thanks for help.
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby mishem » Mon Dec 02, 2013 3:51 am

Code: Select all
If "[Groupe_AddType]" "=" "File(s)"
. Open a window for selecting multiple files
FileOpenBox "Select one or more images files" "[InputMaskConvert]" "[MyDocuments]" "[SelectedFiles]" "Multiple"
. Separate number
StrParse "[SelectedFiles]" ";" "[FilePaths]" "[FilesCount]"
. Calculate the number of items present in the list
ListBoxSize "ZoneImagesOriginales" "[ListNbr]"
. Addition on the list items + the number of selected files
Math "[ListNbr]+[FilesCount]" "-1" "[TotalNbrList]"
Loop "1" "[FilesCount]" "[I]"
. Extract selected file names
ExtractFileName "[FilePaths[I]]" "[FileName]"
. Add them to the list
ListBoxFindItem "ZoneImagesOriginales" "[FilePaths[I]]" "[Result]"
If "[Result]" "=" "0"
   ListBoxAddItem "ZoneImagesOriginales" "0" "[FilePaths[I]]"
EndIf
EndLoop
EndIf
mishem
 
Posts: 575
Joined: Mon Oct 08, 2012 1:51 pm

Re: Search and Replace text in a file

Postby UltraVox » Mon Dec 02, 2013 4:19 am

it works thank you :)
There is a problem with the counter file, but it is easy to solve this problem.

Thank you again ;)
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby UltraVox » Wed Dec 04, 2013 1:44 am

Hello all,

I use the following command :

Code: Select all
FolderBox "Select your image directory :" "[SelectedDir]"


But when I click CANCEL, the variable still has a defaut directory
How to make it so that no directory is selected when opening FolderBox ?

I tried :
Code: Select all
SetVar "[SelectedDir]" ""

and / or

Code: Select all
If "[SelectedDir]" "=" ""
Else
EndIf

But it does not change.

Thanks for help.
UltraVox
 
Posts: 419
Joined: Sat Jul 28, 2007 11:14 am
Location: France

Re: Search and Replace text in a file

Postby dec » Wed Dec 04, 2013 1:54 am

Hello,

Try it with a "blank publication". The command works here like expected: when the dialog is cancelled (with scape key or cancel button) the variable is cleared, in other words, do not contain any information, just like we can expected. So initiate a "blank publication", put a button in the page and prepare just this code to be executed on the "Button Click" actions:

Code: Select all
FolderBox "Choose a directory" "[Selected]"


If you can see the expected behaviour... something is "wrong" in your publication code, but not in the FolderBox action. On the other hand, if you need more than "FileBox" you can take a look at the npFolderDialog action from my npUtil plugin. ;)
.
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

PreviousNext

Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 1 guest