0

I've many files with VB script with many variables and I would like to replace all variable with their value by macro.

For example:

label1.text = "Fan"
label2.text = "Close"
label3.text = "Open"
ledfan.tag = label1.text
led2.tag = label2.text
led3.tag = label3.text

Into:

ledfan = "Fan"
led2 = "Close"
led3 = "Open"

I've tried to do that with macros, but I have the problem that the command Ctrl+C is not recorded into the macro. I also tried with regular expressions, but cannot seem to find the right formula.

Thank you ! :)

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • You might want to split your files in two, and have a look at [this post](http://stackoverflow.com/questions/24071114/rename-part-of-file-name-based-on-exact-match-in-contents-of-another-file) – Qeole Jun 14 '14 at 14:45

2 Answers2

0

I'm not using text editor Notepad++ as I'm using text editor UltraEdit.

An UltraEdit macro solution for this task would be:

InsertMode
ColumnModeOff
HexOff
Top
PerlReOn
Loop 0
Find MatchCase RegExp "^[ \t]*(\w+\.text)[ \t]*=[ \t]*("[^\r\n]+)\r?\n([\s\S]+?)\.tag([ \t]*=[ \t]*)\1"
Replace All "\3\4\2"
IfNotFound
ExitLoop
EndIf
EndLoop

I explain the Perl regular expression Replace All executed on the entire file content in a loop until nothing could be replaced anymore. This Perl regular expression Replace All should work also in Notepad++.

^ ... start every search at beginning of a line.

[ \t]* ... 0 or more spaces or tabs can be at beginning of a line.

\w+ ... find a string with 1 or more word characters which are letters, digits and the character underscore.

\.text ... next the fixed string .text must be in the line.

The opening and closing parenthesis around \w+\.text mark/capture the string found by this expression for backreferencing. This part of the found string is backreferenced in search string itself with \1.

[ \t]*=[ \t]* ... next there can be 0 or more spaces or tabs before an equal sign must be found and again 0 or more spaces or tabs are found.

("[^\r\n]+) ... finds a string starting with a double quote and ending at end of the line which is also captured for backreferencing. This expression matches the text value and also a comment if present. This part of the found string is backreferenced in replace string with \2.

\r?\n ... next a carriage return if present and a line feed is matched.

The expression up to \n matches therefore the text definition line.

([\s\S]+?) ... a non greedy expression matching anything up to the position on which the next part of the expression returns a positive result. This expression matches everything from beginning of line below the current line with a text value definition to end of name of the matching tag value variable. For example on first run for label1.text this expression matches everything from label2.text to ledfan. This block is also captured for backreferencing in replace string with \3.

\.tag ... the fixed string .tag must be found next.

([ \t]*=[ \t]*) ... next there can be 0 or more spaces or tabs before an equal sign must be found and again 0 or more spaces or tabs are found. This part of the found string is captured for backreferencing with \4 in the replace string.

\1 ... the name of the text value variable must be finally found, too.


I must add that this macro respectively Perl regular expression Replace All executed in a loop until nothing could be replaced anymore works only if every text value is assigned only once to a variable. If a text value like label2.text would be assigned twice to a variable, this solution would not work.

An UltraEdit macro solution working also with multiple assignments of a text value to a variable would be:

InsertMode
ColumnModeOff
HexOff
Top
UltraEditReOn
Clipboard 9
Loop 0
Find MatchCase RegExp "%[^t ]++[0-9A-Za-z_]+.text[^t ]++=[^t ]++""
IfNotFound
ExitLoop
EndIf
Key LEFT ARROW
Find MatchCase RegExp ""*$"
Copy
Key HOME
Find MatchCase RegExp "[0-9A-Za-z_]+.text"
Find MatchCase RegExp ".tag^([^t ]++=[^t ]++^)^s"
Replace All "^1^c"
DeleteLine
EndLoop
ClearClipboard
Clipboard 0

But this macro uses content of user clipboard 9 (^c) and the currently selected text (^s) in an UltraEdit regular expression Replace All which cannot be done with the other regular expression engines (Unix and Perl) supported by UltraEdit.

Another advantage of this solution is the deletion of lines defining a text value not assigned to any other variable in the code below. So this macro performs also a cleanup of unused text values.

I don't know how to use this method in a Notepad++ macro as I'm not using Notepad++.


The first solution working only with only 1 assignment of a text variable to another variable using a Perl regular expression Replace All could be also executed with Replace in Files command to run it on all files of a directory (tree), of course also in a loop until nothing could be replaced anymore in any file.


The UltraEdit macros for the next task requested with a comment to change

txtOssigeno.temp = GetStringResource(167)
txtFotoresistenza.temp = GetStringResource(174)
Me.txtOssigeno.Caption = Prot00026
Me.txtFotoresistenza.Caption =Prot00006

into

GetStringResource(167) = Prot00026
GetStringResource(174) = Prot00006

are with Perl regular expression engine:

InsertMode
ColumnModeOff
HexOff
Top
PerlReOn
Loop 0
Find MatchCase RegExp "^[ \t]*(\w+)\.temp[ \t]*=[ \t]*([\w()]+)[^\r\n]*?\r?\n([\s\S]+?)Me\.\1\.Caption[ \t]*=[ \t]*"
Replace All "\3\2 = "
IfNotFound
ExitLoop
EndIf
EndLoop

and with UltraEdit regular expression engine:

InsertMode
ColumnModeOff
HexOff
Top
UltraEditReOn
Clipboard 9
Loop 0
Find MatchCase RegExp "%[^t ]++[0-9A-Za-z_]+.temp[^t ]++="
IfNotFound
ExitLoop
EndIf
Find MatchCase RegExp "[0-9A-Za-z_()]+"
Copy
Key HOME
Find MatchCase RegExp "[0-9A-Za-z_]+"
Find MatchCase RegExp "Me.^s.Caption[^t ]++=[^t ]++"
Replace All "^c = "
DeleteLine
EndLoop
ClearClipboard
Clipboard 0

It is up to you which one of the 2 macros you want to use.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Perfect... Now I want to change te variabile example: – favalcatraz Jun 15 '14 at 13:11
  • Perfect... Now I want to change te variabile example: ` txtOssigeno.temp = GetStringResource(167) txtFotoresistenza.temp = GetStringResource(174) Me.txtOssigeno.Caption = Prot00026 Me.txtFotoresistenza.Caption =Prot00006 ` into: ` GetStringResource(167) = Prot00026 GetStringResource(174) = Prot00006 ` I try Find `^[ \t]*(\w+)\.temp[ \t]*=[ \t]*(GetStringResource[^\r\n]+)\r?\n([\s\S]+?)\Me.\1.Caption` and Replce \2 but not work What's wrong? – favalcatraz Jun 15 '14 at 13:31
  • Your regular expression works also for the find part. The mistake is in the replace string as you would need with your expression `\3\2` to keep the lines between the line to remove and the line to update. I updated my answer also with 2 new macros for this new task. – Mofi Jun 15 '14 at 18:38
0

In Notepad++, you can do it for one variable at a time. If you run the replacement repeatedly, you can do all the lines. Maybe you can do that with a macro.

Here is a regex that will work for one variable (one pair of lines):

Search: (?sm)^(\w+)\.[^=]*(=.*?[\r\n]+)(.*)^(\w+)\.tag[^=]*=\s*\1[^\r\n]*

Replace: \3\4\2

zx81
  • 41,100
  • 9
  • 89
  • 105