Difference between revisions of "ReNamer:Pascal Script:Reuse variable"
(Replaced the use of TStringsArray with TWideStringArray.) |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
(One intermediate revision by the same user not shown) | |||
Line 16: | Line 16: | ||
The ReNamer PascalScript functions to write and read text files are: | The ReNamer PascalScript functions to write and read text files are: | ||
− | < | + | <syntaxhighlight lang="pascal"> |
FileWriteContent( "FileName" , "Content" ); | FileWriteContent( "FileName" , "Content" ); | ||
FileAppendContent( "FileName" , "Content" ); | FileAppendContent( "FileName" , "Content" ); | ||
Line 23: | Line 23: | ||
FileCountLines("FileName"); | FileCountLines("FileName"); | ||
FileReadLine( "FileName" , LineNum ); | FileReadLine( "FileName" , LineNum ); | ||
− | </ | + | </syntaxhighlight> |
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> | ||
Example: | Example: | ||
− | < | + | <syntaxhighlight lang="pascal"> |
FileWriteContent( "FileName.txt" , "Content" ); | FileWriteContent( "FileName.txt" , "Content" ); | ||
FileWriteContent( ".\FileName.txt" , "Content" ); | FileWriteContent( ".\FileName.txt" , "Content" ); | ||
− | </ | + | </syntaxhighlight> |
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> | ||
Example: | Example: | ||
− | < | + | <syntaxhighlight lang="pascal"> |
FileWriteContent( WideExtractFilePath(GetApplicationPath) + "FileName.txt" , "Content" ); | FileWriteContent( WideExtractFilePath(GetApplicationPath) + "FileName.txt" , "Content" ); | ||
− | </ | + | </syntaxhighlight> |
You can use your private temp (temporary) folder in your profile<br> | You can use your private temp (temporary) folder in your profile<br> | ||
by using the ReNamer PascalScript function "WideGetTempPath"<br> | by using the ReNamer PascalScript function "WideGetTempPath"<br> | ||
Example: | Example: | ||
− | < | + | <syntaxhighlight lang="pascal"> |
FileWriteContent( WideGetTempPath + "FileName.txt" , "Content" ); | FileWriteContent( WideGetTempPath + "FileName.txt" , "Content" ); | ||
− | </ | + | </syntaxhighlight> |
Also you can access all system environment variables by using "WideGetEnvironmentVar"<br> | Also you can access all system environment variables by using "WideGetEnvironmentVar"<br> | ||
Example: | Example: | ||
− | < | + | <syntaxhighlight lang="pascal"> |
UserName := WideGetEnvironmentVar('USERNAME'); | UserName := WideGetEnvironmentVar('USERNAME'); | ||
− | </ | + | </syntaxhighlight> |
=== Script examples === | === Script examples === | ||
Line 57: | Line 57: | ||
First rule in rule list:<br> | First rule in rule list:<br> | ||
(GET stored variable 'LastFolder') | (GET stored variable 'LastFolder') | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
LastFold:string; | LastFold:string; | ||
Line 69: | Line 69: | ||
ShowMessage(LastFold); | ShowMessage(LastFold); | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
Other rules here in between<br> | Other rules here in between<br> | ||
Line 78: | Line 78: | ||
Last rule in rule list:<br> | Last rule in rule list:<br> | ||
(SET variable 'LastFolder' to stored for later reuse) | (SET variable 'LastFolder' to stored for later reuse) | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
ParentFold:string; | ParentFold:string; | ||
Line 92: | Line 92: | ||
ExecuteProgram('notepad ' + WideGetTempPath + 'den4b_LastFolder.txt',false); | ExecuteProgram('notepad ' + WideGetTempPath + 'den4b_LastFolder.txt',false); | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
====Store more then one value into a common file==== | ====Store more then one value into a common file==== | ||
Line 126: | Line 126: | ||
- 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> | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
vars,v:string; | vars,v:string; | ||
Line 158: | Line 158: | ||
end; | end; | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
== Registry Approach == | == Registry Approach == | ||
Line 166: | Line 166: | ||
Not recommended ! | Not recommended ! | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
ECAreturn:string; | ECAreturn:string; | ||
Line 189: | Line 189: | ||
//>---more code needed here to extract the value "Test value 1"---< | //>---more code needed here to extract the value "Test value 1"---< | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
== Clipboard Approach == | == Clipboard Approach == | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
vLastExtension, vCurrentExtension: WideString; | vLastExtension, vCurrentExtension: WideString; | ||
Line 212: | Line 212: | ||
SetClipboardText(vCurrentExtension); | SetClipboardText(vCurrentExtension); | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
[[Category:ReNamer]] | [[Category:ReNamer]] | ||
[[Category:Pascal Script]] | [[Category:Pascal Script]] |
Latest revision as of 15:03, 8 February 2017
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:TWideStringArray;
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 TestName1', 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
vLastExtension, vCurrentExtension: WideString;
begin
// At start, load stored data //Load/Read/Get:
vLastExtension := GetClipboardText;
// Add here some error handling and cleanup code for vLastExtension
//if Length(vLastExtension) > 4 then Exit;
//
// Add your code here and use content of var vLastExtension
vCurrentExtension := WideExtractFileExt(FileName);
if vCurrentExtension <> vLastExtension then
.....
//
//
//
// At the end store the wanted data //Store/Write/Set:
SetClipboardText(vCurrentExtension);
end.