Jump to content

Template filling up using csv Config file


Go to solution Solved by water,

Recommended Posts

Hi Friends,

I'm new to autoit, so plz forgive me if this is a silly question

We have got a config file as below

IPAddress,ServerHostname,Env,Type
10.2.60.121,belgiumserver1,NP,WEB
10.2.61.121,belgiumserver2,NP,WEB
10.2.60.57,belgiumserver3,NP,WEB
10.2.60.82,belgiumserver4,DEV,WEB
10.2.60.98,belgiumserver5,DEV,WEB
10.2.60.99,belgiumserver6,DEV,WEB
10.2.61.101,belgiumserver7,DEV,WEB
10.2.90.82,belgiumserver8,PROD,WEB
10.2.58.82,belgiumserver9,PROD,WEB
10.2.90.83,belgiumserver10,PROD,WEB
10.2.58.83,denmark1,PROD,CORP
10.2.90.98,denmark2,PROD,CORP
10.2.90.99,denmark3,PROD,CORP
10.2.90.120,denmark4,PROD,CORP
10.2.91.82,denmark5,PROD,CORP
10.2.59.82,denmark6,PROD,CORP
10.2.91.98,denmark7,PROD,CORP
10.2.91.99,denmark8,PROD,CORP

We are trying to automate "Putty" session creation using autoit & Putty_Portable (

Template Sample..

....
WinTitle\DYNAMIC_PUTTY_TITLE\
LogType\0\
LogFileName\putty.log\
HostName\DYNAMIC_HOSTNAME_IP\
....

Is there a ready-made script for such csv "Config" file lookup and string replacement?

Cheers
 

Edited by getk
Link to post
Share on other sites

I use the following >script to read a CSV file into a 2D array. It should then be easy to create the output files.

Another idea would be to use PLINK (a command-line interface to the PuTTY back ends). You will find a few examples how to use PLINK on this forum.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

I use the following >script to read a CSV file into a 2D array. It should then be easy to create the output files.

Another idea would be to use PLINK (a command-line interface to the PuTTY back ends). You will find a few examples how to use PLINK on this forum.

Thanks water.  I will also use the above logic.

can we find/replace string in a file with multiple arrays at a time or have to do one string and then loop through the entire file multiple times?

Link to post
Share on other sites

Does the template change often so it needs to be saved in a file or is it possible to have the lines of the template "stored" in the script?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

Does the template change often so it needs to be saved in a file or is it possible to have the lines of the template "stored" in the script?

Template should remain the same.

But each template has around 200+ lines

Link to post
Share on other sites
  • Solution

The following script reads the config file Config.txt, replaces the data in Template.txt and writes a chagned template for every entry in the config file to Output.txt:

#include <File.au3>
Global $aConfig, $aTemplate, $aValues, $sOutput
; Read config file
_FileReadToArray(@ScriptDir & "\Config.txt", $aConfig)
; Read template file
_FileReadToArray(@ScriptDir & "\template.txt", $aTemplate)
; Open output file
$hOutput = FileOpen(@ScriptDir & "\Output.txt", 2)
For $i = 2 To $aConfig[0]
    $aValues = StringSplit($aConfig[$i], ",")
    For $j = 1 To $aTemplate[0]
        $sOutput = StringReplace($aTemplate[$j], "\DYNAMIC_PUTTY_TITLE\", "\" & $aValues[2] & "\") ; ServerHostName
        $sOutput = StringReplace($sOutput, "\DYNAMIC_HOSTNAME_IP\", "\" & $aValues[1] & "\") ; IPAddress
        ; more replacements to fill in here
        FileWriteLine($hOutput, $sOutput)
    Next
Next
FileClose($hOutput)

Is this what you need?

Config.txt

Template.txt

Output.txt

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

The following script reads the config file Config.txt, replaces the data in Template.txt and writes a chagned template for every entry in the config file to Output.txt:

@water, much appreciated for your quick reply. I will have a check and get back to you.. 

Link to post
Share on other sites

#include <File.au3>
Global $aConfig, $aTemplate, $aValues, $sOutput, $outDirectory, $tempVarHostName
; Read config file
_FileReadToArray(@ScriptDir & "\Config.txt", $aConfig)
; Read template file
_FileReadToArray(@ScriptDir & "\template.txt", $aTemplate)

; Open output file per item in Config File
$outDirectory = "C:\Rough\PuttyList"

For $i = 2 To $aConfig[0]     
    $aValues = StringSplit($aConfig[$i], ",")    
    $tempVarHostName = $aValues[2]
    $hOutput = FileOpen($outDirectory  & "\" & $tempVarHostName & ".txt", 2)
     For $j = 1 To $aTemplate[0]
         $sOutput = StringReplace($aTemplate[$j], "\DYNAMIC_PUTTY_TITLE\", "\" & $tempVarHostName & "\")  ; ServerHostName         
         $sOutput = StringReplace($sOutput, "\DYNAMIC_HOSTNAME_IP\", "\" & $aValues[1] & "\")  ; IPAddress         
         ; more replacements to fill in here         
         FileWriteLine($hOutput, $sOutput)     
     Next
    FileClose($hOutput)
 Next

@water,  I just modified your script slightly, so that each iterations create individual output files.

Again, your logic worked absolutely great and special thanks for your solution.  Great help

Link to post
Share on other sites

Glad this little script solved your "problem" :D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...