Difference between revisions of "ReNamer:Pascal Script:Reuse variable"

From den4b Wiki
Jump to navigation Jump to search
m (corrected two pasted text to make more sense together)
(cleaned up a bit)
Line 1: Line 1:
== --- This page is work in process! --- ==
+
{{Cleanup|This page is work in process!<br/>
 
+
Currently i just collect here a few information and POC proof of concept code.<br/>
currently i just collect here a few information and POC proof of concept code.
+
I want to clean up this page later.<br/>
 
+
If you want to help please do so.<br/>
I want to clean up this page later.
+
-- [[User:Stefan]]}}
 
 
If you want to help please do so.
 
 
 
 
 
 
 
== Set/Get or Write/Read or Save/Load variable values ==
 
 
 
  
 
If you want to store a variable to reuse the value later you can read here a few tips.
 
If you want to store a variable to reuse the value later you can read here a few tips.
  
 +
For examples you can store the last used path, folder or file name or parts of them. Also you may want to store an index number, or perhaps a list of the original file names. There are several techniques to store variable values, feel free to choose the one that suits you.
  
For examples you can store the last used path, folder or file name or parts of them.<br>
+
== File Approach ==
Also you may want to store an index number, or perhaps a list of the original file names.
 
<br>
 
<br>
 
<br>
 
 
 
 
 
There are two main techniques to store variable values: use the registry or use a temporary file on disc.
 
 
 
 
 
 
 
== the file approach ==
 
  
 
First we take a look on the file approach.
 
First we take a look on the file approach.
Line 32: Line 15:
 
We use simple plain text files to store the variable.
 
We use simple plain text files to store the variable.
  
=== utilized function ===
+
=== Utilized Function ===
  
 
The ReNamer PascalScript functions to write and read text files are:
 
The ReNamer PascalScript functions to write and read text files are:
 +
 
<source>
 
<source>
 
FileWriteContent( "FileName" , "Content" );
 
FileWriteContent( "FileName" , "Content" );
Line 43: Line 27:
 
FileReadLine( "FileName" , LineNum );
 
FileReadLine( "FileName" , LineNum );
 
</source>
 
</source>
 
  
 
For "FileName" you can just use a name without a path to store the file into the ReNamer folder.<br>
 
For "FileName" you can just use a name without a path to store the file into the ReNamer folder.<br>
Line 51: Line 34:
 
FileWriteContent( ".\FileName.txt" , "Content" );
 
FileWriteContent( ".\FileName.txt" , "Content" );
 
</source>
 
</source>
 
 
  
 
To use an full path to your ReNamer folder use the ReNamer PascalScript function "GetApplicationPath"<br>
 
To use an full path to your ReNamer folder use the ReNamer PascalScript function "GetApplicationPath"<br>
Line 59: Line 40:
 
FileWriteContent( WideExtractFilePath(GetApplicationPath) + "FileName.txt" , "Content" );
 
FileWriteContent( WideExtractFilePath(GetApplicationPath) + "FileName.txt" , "Content" );
 
</source>
 
</source>
 
 
  
 
You can use your private temp (temporary) folder in your profile<br>
 
You can use your private temp (temporary) folder in your profile<br>
Line 68: Line 47:
 
FileWriteContent( WideGetTempPath + "FileName.txt" , "Content" );
 
FileWriteContent( WideGetTempPath + "FileName.txt" , "Content" );
 
</source>
 
</source>
 
  
 
Also you can access all system environment variables by using "WideGetEnvironmentVar"<br>
 
Also you can access all system environment variables by using "WideGetEnvironmentVar"<br>
Line 76: Line 54:
 
</source>
 
</source>
  
 
+
=== Script examples ===
 
 
=== script examples ===
 
  
 
==== Store one value to one dedicated file ====
 
==== Store one value to one dedicated file ====
Line 97: Line 73:
 
end.
 
end.
 
</source>
 
</source>
 
  
 
Other rules here in between<br>
 
Other rules here in between<br>
Line 103: Line 78:
 
-<br>
 
-<br>
 
-<br>
 
-<br>
 
  
 
Last rule in rule list:<br>
 
Last rule in rule list:<br>
Line 122: Line 96:
 
end.
 
end.
 
</source>
 
</source>
 
  
 
====Store more then one value into a common file====
 
====Store more then one value into a common file====
Line 128: Line 101:
 
Note this approach is to cumbersome for real use. <br>
 
Note this approach is to cumbersome for real use. <br>
 
Better use dedicated file for each var/value (see above).
 
Better use dedicated file for each var/value (see above).
 
 
  
 
We can also use  FileAppendContent();  to add "NAME=VALUE" pairs to an single file<br>
 
We can also use  FileAppendContent();  to add "NAME=VALUE" pairs to an single file<br>
 
and a routine to read the lines of that common vars.txt.
 
and a routine to read the lines of that common vars.txt.
 
  
 
Here just as proof of concept:
 
Here just as proof of concept:
Line 140: Line 110:
 
(add a line with "NAME=VALUE" pair)
 
(add a line with "NAME=VALUE" pair)
 
   FileAppendContent('den4b_vars.txt','LastFolder=' + ParentF + #13#10);
 
   FileAppendContent('den4b_vars.txt','LastFolder=' + ParentF + #13#10);
 
 
  
 
This 'den4b_vars.txt' could look now like:
 
This 'den4b_vars.txt' could look now like:
Line 147: Line 115:
 
  LastString=Vacation 2013
 
  LastString=Vacation 2013
 
  LastFolder=Translits
 
  LastFolder=Translits
 
 
 
  
 
Other rules here in between<br>
 
Other rules here in between<br>
Line 155: Line 120:
 
-<br>
 
-<br>
 
-<br>
 
-<br>
 
  
 
To get the wanted value i would use a code like this:
 
To get the wanted value i would use a code like this:
Line 165: Line 129:
 
- check if 'name' is a wanted var name, if yes get the value<br>
 
- check if 'name' is a wanted var name, if yes get the value<br>
  
var
+
<source>
  vars,v:string;
+
var
  Lines,CurrLine:TStringsArray;
+
  vars,v:string;
  i:integer;
+
  Lines,CurrLine:TStringsArray;
 +
  i:integer;
 
    
 
    
begin
+
begin
 
   if(GetCurrentFileIndex=1)then
 
   if(GetCurrentFileIndex=1)then
 
   begin
 
   begin
Line 195: Line 160:
 
     end;
 
     end;
 
   end;
 
   end;
end.
+
end.
 
+
</source>
 
 
 
 
 
 
 
 
 
 
  
== the registry approach ==
+
== Registry Approach ==
  
 
=== Not recommended ! ===
 
=== Not recommended ! ===
Line 209: Line 169:
 
Not recommended !
 
Not recommended !
  
 +
<source>
 
  var
 
  var
 
   ECAreturn:string;
 
   ECAreturn:string;
 
+
 
 
  begin
 
  begin
 
   // Add your code here
 
   // Add your code here
Line 231: Line 192:
 
     //>---more code needed here to extract the value "Test value 1"---<
 
     //>---more code needed here to extract the value "Test value 1"---<
 
end.
 
end.
 +
</source>
  
== the clipboard approach ==
+
== Clipboard Approach ==
=== no tested yet ===
 
SetClipboardText( "content" );
 
  
varClip := GetClipboardText;
+
<source>
 
+
var
 
+
  Data: WideString;
-# end
+
begin
 +
  SetClipboardText('Sample Data');
 +
  Data := GetClipboardText;
 +
end.
 +
</source>

Revision as of 15:58, 27 January 2013

{{{iparam}}} This article needs to be cleaned up!

This page is work in process!
Currently i just collect here a few information and POC proof of concept code.
I want to clean up this page later.
If you want to help please do so.
-- User:Stefan

If you want to store a variable to reuse the value later you can read here a few tips.

For examples you can store the last used path, folder or file name or parts of them. Also you may want to store an index number, or perhaps a list of the original file names. There are several techniques to store variable values, feel free to choose the one that suits you.

File Approach

First we take a look on the file approach.

We use simple plain text files to store the variable.

Utilized Function

The ReNamer PascalScript functions to write and read text files are:

FileWriteContent( "FileName" , "Content" );
FileAppendContent( "FileName" , "Content" );

FileReadContent("FileName");
FileCountLines("FileName");
FileReadLine( "FileName" , LineNum );

For "FileName" you can just use a name without a path to store the file into the ReNamer folder.
Example:

FileWriteContent( "FileName.txt" , "Content" );
FileWriteContent( ".\FileName.txt" , "Content" );

To use an full path to your ReNamer folder use the ReNamer PascalScript function "GetApplicationPath"
Example:

FileWriteContent( WideExtractFilePath(GetApplicationPath) + "FileName.txt" , "Content" );

You can use your private temp (temporary) folder in your profile
by using the ReNamer PascalScript function "WideGetTempPath"
Example:

FileWriteContent( WideGetTempPath + "FileName.txt" , "Content" );

Also you can access all system environment variables by using "WideGetEnvironmentVar"
Example:

UserName := WideGetEnvironmentVar('USERNAME');

Script examples

Store one value to one dedicated file

First rule in rule list:
(GET stored variable 'LastFolder')

var
  LastFold:string;

begin
 if(GetCurrentFileIndex=1)then
    if(WideFileExists(WideGetTempPath +'\den4b_LastFolder.txt')) then
        LastFold := FileReadContent(WideGetTempPath +'den4b_LastFolder.txt');

  //check if it works:
  ShowMessage(LastFold);
end.

Other rules here in between
-
-
-

Last rule in rule list:
(SET variable 'LastFolder' to stored for later reuse)

var
  ParentFold:string;

begin
 if(GetCurrentFileIndex=GetTotalNumberOfFiles)then
   begin
       ParentFold := CalculateMetaTag(FilePath, 'File_FolderName');
       FileWriteContent(WideGetTempPath + 'den4b_LastFolder.txt', ParentFold);
   end;
 
  //check if it works:
  ExecuteProgram('notepad ' + WideGetTempPath + 'den4b_LastFolder.txt',false);
end.

Store more then one value into a common file

Note this approach is to cumbersome for real use.
Better use dedicated file for each var/value (see above).

We can also use FileAppendContent(); to add "NAME=VALUE" pairs to an single file
and a routine to read the lines of that common vars.txt.

Here just as proof of concept:

Last rule in rule list:
(add a line with "NAME=VALUE" pair)

 FileAppendContent('den4b_vars.txt','LastFolder=' + ParentF + #13#10);

This 'den4b_vars.txt' could look now like:

LastIndex=10
LastString=Vacation 2013
LastFolder=Translits

Other rules here in between
-
-
-

To get the wanted value i would use a code like this:

First rule in rule list:
- read the vars.txt
- split into lines
- split line into 'name' and 'value'
- check if 'name' is a wanted var name, if yes get the value

var
  vars,v:string;
  Lines,CurrLine:TStringsArray;
  i:integer;
   
begin
  if(GetCurrentFileIndex=1)then
  begin
    if( WideFileExists('.\den4b_vars.txt') ) then
    begin
       vars := FileReadContent('.\den4b_vars.txt');
       ShowMessage('complete content:'+#10+vars);
       Lines:= WideSplitString(vars, #13#10);
       if( length(Lines) > 0 ) then
         begin
             for i:=0 to length(Lines)-1 do
             begin
                CurrLine := WideSplitString(Lines[i],'=');
                v := CurrLine[0];
                //ShowMessage(v);
                if ( WideCompareText( v, 'LastIndex' ) =0) then
                    ShowMessage( v + ' has value ' + CurrLine[1] );
                if ( WideCompareText( v, 'LastFolder' ) =0) then
                    ShowMessage( v + ' has value ' +  CurrLine[1] );
                if ( WideCompareText( v, 'LastString' ) =0) then
                    ShowMessage( v + ' has value ' +  CurrLine[1] );
             end;
         end;
    end;
  end;
end.

Registry Approach

Not recommended !

Not recommended !

 var
  ECAreturn:string;

 begin
   // Add your code here
   
   //Store/Write/Set:
    ExecuteProgram('reg add  HKCU\Software\den4b /v TestName1 /t REG_SZ /d "Test value 1" /f', true);
     
    //Load/Read/Get: 
    ExecConsoleApp('reg query  HKCU\Software\den4b /v Test6', ECAreturn);
    ShowMessage(ECAreturn);
    // Result:
    //  
    //  ! REG.EXE VERSION 3.0
    //  
    //  HKEY_CURRENT_USER\Software\den4b
    //      TestName1       REG_SZ  Test value 1
    //  
      
    //>---more code needed here to extract the value "Test value 1"---<
end.

Clipboard Approach

var
  Data: WideString;
begin
  SetClipboardText('Sample Data');
  Data := GetClipboardText;
end.