Create an ini file and read data, paths and variables from it for the compiled script.

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
toopnad
Junior Coder
Posts: 34
Joined: Mon Apr 12, 2021 10:10 am
Location: Russian Totalitarian Republic, Perm city

Create an ini file and read data, paths and variables from it for the compiled script.

Post by toopnad » Tue Dec 07, 2021 9:59 am

Please help a newbie where to start and how to do it (what to read, where to look)? How to set the values of variables in the ini file, how to read variables from the ini file. How to tell the script that the ini file is in the same folder as the compiled program itself? I need some detailed example.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1386
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Create an ini file and read data, paths and variables from it for the compiled script.

Post by Dorian (MJT support) » Tue Dec 07, 2021 10:41 am

Compile this :

Code: Select all

//Create ini file if it doesn't exist
IfNotFileExists>%script_dir%\myini.ini
  WriteLn>%script_dir%\myini.ini,,
Endif

//This writes to the ini file
EditIniFile>%script_dir%\myini.ini,TestSection,TestName,toopnad

//This reads the ini file
ReadIniFile>%script_dir%\myini.ini,TestSection,TestName,var1


mdl>%var1% was read from the ini file
%script_dir% will be wherever you place the exe.

More info (help file should always be first place to look):
EditIniFile
ReadIniFile

Regarding "what to read, where to look" - for future reference. Just in case you didn't know, there's a complete list of all commands here. So if you were to visit that link, press CTRL-F, and for example search for "XL" you'd see all the Excel commands. "ini" shows both the commands I used above. That list is a great way of discovering new commands and is well worth perusing when you have time to spare. Here's a categorized link for when what you're looking for is in an obvious category. And once a command is typed into your script editor, pressing F1 takes you to help for that command (context sensitive help).
Yes, we have a Custom Scripting Service. Message me or go here

toopnad
Junior Coder
Posts: 34
Joined: Mon Apr 12, 2021 10:10 am
Location: Russian Totalitarian Republic, Perm city

Re: Create an ini file and read data, paths and variables from it for the compiled script.

Post by toopnad » Tue Dec 07, 2021 10:56 am

Thank you for your help, it's just hard for me to understand the translation features. I know where to look at all the commands, but I could not understand how they (ini commands) are used, so many thanks for your given example code. I will try.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1386
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Create an ini file and read data, paths and variables from it for the compiled script.

Post by Dorian (MJT support) » Tue Dec 07, 2021 11:01 am

Always happy to help, and I can appreciate how hard it must be if English isn't your first language. The usage examples in the help files, luckily, are usually a great starting point and can be used for experimentation. That's exactly how I learned MS in the first place (been using Macro Scheduler since 1998).
Yes, we have a Custom Scripting Service. Message me or go here

toopnad
Junior Coder
Posts: 34
Joined: Mon Apr 12, 2021 10:10 am
Location: Russian Totalitarian Republic, Perm city

Re: Create an ini file and read data, paths and variables from it for the compiled script.

Post by toopnad » Tue Dec 07, 2021 11:21 am

Tell me more, if I want to add a VBA script to the ini file as a variable so that the user can edit it later, what should I do with a new line? is there any character for a line break, because I have to specify the entire script on one line?
I tried putting it in as a variable, but only the first line is read back.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1386
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Create an ini file and read data, paths and variables from it for the compiled script.

Post by Dorian (MJT support) » Tue Dec 07, 2021 11:59 am

I wouldn't use an in file for that. I'd include the vba in a label and use labeltovar. If it has to be in an external file for some reason then use writeln to write it to a text file and readfile to get it back out again. (usage examples for all these can be found in the Command Reference).
Yes, we have a Custom Scripting Service. Message me or go here

toopnad
Junior Coder
Posts: 34
Joined: Mon Apr 12, 2021 10:10 am
Location: Russian Totalitarian Republic, Perm city

Re: Create an ini file and read data, paths and variables from it for the compiled script.

Post by toopnad » Tue Dec 07, 2021 12:10 pm

This is what I used, but it may be necessary to correct the VBA script if the number of columns or the names of the tables change. I will try to use your advice. Thank you, you helped a lot. I was able to figure it out using the EditIniFile and ReadIniFile commands.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts