Code Snippets for Pathes and filenames
Posted: 14. Dec 2018, 15:54
These are a few small code snippets you need when dealing with paths.
It's about paths and filenames.
1. If you only want to go one level up from any given path, you can do this:
In the clipboard is now the path, a directory level higher than where the script is currently.
2. Automatically use Path of the executable in "EXE-Mode" or use "?path" of the Script in the Editor?
If you want to run the script as EXE you need $ev4$ instead of "?path".
This could be automatically distinguished by using the command IEX\(If EXE Mode):
But you do not have to go this extra-mile.
Because $ev4$ produces exactly the same result as "?path" in editor mode.
So if you use $ev4$ instead of ?path, you'll always get the right result.
3. Sometimes you need a file name that you already have, but with a different extension.
4. If you want to wait in the course of a process until a certain file appears, a command WFF\ (Wait For File) could be used.
Let's take a compilation, because a file is created. Only when it is there should another process continue to use the file, for example compress or attach a certificate.
With the MPR you just write "WFF" (Wait for File)
And if you want to wait until it has been safely deleted or is no longer there, just write:
5. In the course of such batch scripts you have to change here and there also the current directory, this is how it works:
6. And finally you want to call a commandline tool that gets a parameter.
7. Local variables
Now if you have a collection of such batch scripts, and one calls another (with JNF\)
At least there is the danger that a variable of a called sub-script overwrites a variable of the calling script.
To avoid this you can use script local variables. This works with VVS\ and VVR\.
To do this, call the calling script as usual:
For example, the called sub script would look like this:
8. Splitting file paths with the MPR.
Basically, you can easily break a path with GFT\ (Get File Tokens).
It's about paths and filenames.
1. If you only want to go one level up from any given path, you can do this:
Code: Select all
'Any path, where the script itself is located
VAR\$$PSA=?pfad
' The output variable now contains
' c:\program files (x86)\matrix42\package robot\remfiles\projects
'Switch one level up
BLB\$$PSA
VAR\$$PSB=$v1$
' Copy To Clipboard
CTC\$$PSB
'Text in the clipboard is now
' c:\program files (x86)\matrix42\package robot\remfiles
2. Automatically use Path of the executable in "EXE-Mode" or use "?path" of the Script in the Editor?
If you want to run the script as EXE you need $ev4$ instead of "?path".
This could be automatically distinguished by using the command IEX\(If EXE Mode):
Code: Select all
'If the script is running as EXE
IEX\
VAR\$$PAT=$ev4$
ELSE
'If the script is running in the editor
VAR\PAT=?pfad
EEX\
BLB\$$PAT
VAR\$$PSA=$v1$
CTC\$v1$
Because $ev4$ produces exactly the same result as "?path" in editor mode.
So if you use $ev4$ instead of ?path, you'll always get the right result.
3. Sometimes you need a file name that you already have, but with a different extension.
Code: Select all
'Again we take a concrete file (with or without path)
VAR\$$FIC=f:\files\tiefer\Meinfile.pas
'Now we determine everything before the last'. "
BLC\$$FIC|.
VAR\$$FIX=$v1$
'And so we can now determine the other files of the same name, another extension
' f:\files\tiefer\Meinfile.log
VAR\$$FIN=$$FIX.log
' f:\files\tiefer\Meinfile.inc
VAR\$$FIS=$$FIX.inc
MBX\$$FIS
Let's take a compilation, because a file is created. Only when it is there should another process continue to use the file, for example compress or attach a certificate.
With the MPR you just write "WFF" (Wait for File)
Code: Select all
WFF\$$FIN
Code: Select all
' Delete file
DEL\$$FIN
'And for safety's sake wait until the file is really gone from all buffers
WFF\$$FIN|!
Code: Select all
VAR\$$PAT=c:\myfolder\
CHD\$$PAT
'Small break of 150 ms
TDL\150
Code: Select all
VAR\$$COM=c:\bin\mytool.exe
VAR\$$FIC=My parameters /v /x
EXE\$$COM|"$$FIC"
Now if you have a collection of such batch scripts, and one calls another (with JNF\)
At least there is the danger that a variable of a called sub-script overwrites a variable of the calling script.
To avoid this you can use script local variables. This works with VVS\ and VVR\.
To do this, call the calling script as usual:
Code: Select all
' The Script: "MySubScript.rem" in the same folder is called, two parameters are passed to the script.
VAR\$$TXT=Hallo Franz
JNF\MySubScript.rem|Parameter1|Parameter2
' Output is "Hallo Franz"
MBX\$$TXT
@
Code: Select all
' MySubScript.rem
VVS\
VAR\$$TXT=Hallo Peter
'The variables are all saved, but the parameters are all there.
VAR\$$NAM=$$_01
VAR\$$NAN=$$_02
MBX\ Der erste Parameter ist: $$NAM
MBX\ Der zweite Parameter ist: $$NAN
' Output is "Hallo Peter"
MBX\$$TXT
VVR\
' The @ ends a subprogram that was called using "JNF\".
@
8. Splitting file paths with the MPR.
Basically, you can easily break a path with GFT\ (Get File Tokens).
With "GFT\" you can split a file path into the component parts
- directory (without backslash at the end),
- filename (without extension) and
- extension (if there is one)
The tokens are stored in program internal variables:
$v2$ contains the directory without a final backslash,
$v3$ contains the file name without extension,
$v4$ contains the extension,
$v5$ contains the root directory with final backslash and
$v6$ contains the pure directory name without a final backslash.
If this is a network path, the directory name is used instead of the directory name and $v6$ contains the server name supplied with final backslash.
If the name of an (existing) directory is transferred in the data part, $v3$ and $v4$ remain empty.
If you leave the data part blank, the contents of $v1$ will be used as the file path. For example "IEF\" leaves File pathes in $v1$ (If Exists File/Folder).
Example:
DVV\1+
GFT\?remexe
@
Output:
$v2$=e:\wr6-code
$v3$=remote
$v4$=exe
$v5$=e:\
$v6$=wr6-code