Parameters in the File Name

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury, Rain

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3276
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Parameters in the File Name

Post by JRL » Fri Sep 27, 2019 8:31 pm

I have a script that runs daily and every now and then I need to alter how far back it looks within a group of files. I could just hard code the value and alter the script when I need to but in this case the file is compiled so I'd need to recompile each time I made a change. Could also use the standard Macro Scheduler parameter rules and have a batch file or another script or a shortcut that contains the line:
Days Ago.exe /Date_Start=2019-09-27 /Days_Back=3

Then I have to remember where the batch file, script or shortcut resides.

Its probably been done before but it just occurred to me today, a file name could contain specially formatted text that can easily be parsed and parameterized. (<-- Is that a word?) Simply rename the file to alter the parameter values. Works as a script or an EXE.

This could be done many ways but following is an example of how I used the file name for parameter storage today.

Code: Select all

//Name the script file:
//Days Ago (Date_Start=2019-09-27) (Days_Back=3).scp
//Run the saved script.
//You should see the date "9/24/2019"
//
//Rename the file to:
//Days Ago (Date_Start=2019-09-27) (Days_Back=7).scp
//Run the renamed script.
//You should see the date "9/20/2019"
//
//
//Use Marcus' "Most Used Regex"
//Let>pattern=(?<=TOKEN1).*?(?=TOKEN2)
//To parse the "Script_Name" variable and acquire the parameters
//buried within parentheses in the file name
//
Let>pattern=(?<=\().*?(?=\))
Regex>pattern,Script_Name,0,ParmArr,ParmNum,0
//
//The parameters name and value are each separated by an equal sign
//Use Separate> to parse them.
Separate>ParmArr_1,=,Parm_1
Separate>ParmArr_2,=,Parm_2
//
//Assign each variable name its associated value
Let>%Parm_1_1%=%Parm_1_2%
Let>%Parm_2_1%=%Parm_2_2%
//
//Can't use "/" in a file name so used "-" as a date part separator
//The date has to be rebuilt from hyphens to slashes 
Separate>Date_Start,-,Date_Part
Let>New_Date=%Date_Part_2%/%Date_Part_3%/%Date_Part_1%
//
//Subtract "Days_Back" parameter assigned value from the
//"Date_Start" parameter assigned value
Sub>New_Date,%Days_Back%
//
//Display new date
MDL>New_Date


Here is a short subroutine that will parse all parameters used in a filename using the (var=val) method. You, the programmer still have to know how to deal with the created variables and their values just as with any program using parameters. This sample uses the same filename and provides the same result as above.

Code: Select all

GoSub>ParseTitleParms
//
Separate>Date_Start,-,Date_Part
Let>New_Date=%Date_Part_2%/%Date_Part_3%/%Date_Part_1%
//
Sub>New_Date,%Days_Back%
//
MDL>New_Date
//
//
//Sub to parse all script title parameters
SRT>ParseTitleParms
Let>pattern=(?<=\().*?(?=\))
Regex>pattern,Script_Name,0,ParmArr,ParmNum,0
Let>Parmbuild=0
Let>parmRun=
Let>Pct=%
Repeat>ParmBuild
  Add>ParmBuild,1
  Let>ParmRun=%ParmRun%%crlf%Let>%pct%ParmArr_%Parmbuild%%pct%
Until>ParmBuild=ParmNum
IncludeFromVar>parmRun
END>ParseTitleParms

Here is a sample that uses parameters in the file name to control the size and location of a dialog. Name the file whatever you'd like but make sure it has these parameters in the name. You can alter the parameter values to suit your taste.

(DW=400)(DH=100)(DX=50)(DY=200)

Eg: Parms_define_Dialog_(DW=400)(DH=100)(DX=50)(DY=200).scp

Code: Select all

GoSub>ParseTitleParms
//
LabelToVar>DialogVar1,Dia1
IncludeFromVar>Dia1
//
Let>WIN_USEHANDLE=1
MoveWindow>Dialog1.handle,DX,DY
Let>WIN_USEHANDLE=0
//
Show>dialog1,
//
//
/*
DialogVar1:
Dialog>Dialog1
object Dialog1: TForm
  Caption = 'Defined by Parameters'
  ClientHeight = %DH%
  ClientWidth = %DW%
  object Label1: TLabel
    Left = 40
    Top = 24
    Caption = 'Dialog Size = %DW% x %DH%'
  end
  object Label2: TLabel
    Left = 40
    Top = 56
    Caption = 'Dialog Location = %DX%,%DY%'
  end
end
EndDialog>Dialog1
*/
//
//
//Sub to parse all script title parameters
SRT>ParseTitleParms
Let>pattern=(?<=\().*?(?=\))
Regex>pattern,Script_Name,0,ParmArr,ParmNum,0
Let>Parmbuild=0
Let>parmRun=
Let>Pct=%
Repeat>ParmBuild
  Add>ParmBuild,1
  Let>ParmRun=%ParmRun%%crlf%Let>%pct%ParmArr_%Parmbuild%%pct%
Until>ParmBuild=ParmNum
IncludeFromVar>parmRun
END>ParseTitleParms

Post Reply