This topic contains 10 replies, has 4 voices, and was last updated by  Jeff Drumm 4 months, 3 weeks ago.

use of os.fs.glob

  • Hi,

    API os.fs.glob- is used to match a filename . But I need to know this API can access the files in which directory location. For example, I have the file “Nationality_12345777.csv” in the location C:\Program Files\iNTERFACEWARE\Iguana\iguana_training.

    How do I use this API to check for this filename under this specified folder.

    When I use this API as per in example — for FileName, FileInfo in os.fs.glob(‘Nationality/*.csv’) do
    trace(FileName)
    trace(FileInfo)
    — do some processing here
    end

    my result is “Nationality/* does not exist”.

    Hi Supriya,

    When calling the API you need to specify the full file path to your CSV files:

    local Dir = [[C:\Program Files\iNTERFACEWARE\Iguana\iguana_training\]]

    Name, FileInfo in os.fs.glob(Dir..'*.csv')

    Let me know if that works.

    Kind Regards,

    Paul

    Hi folks. Just started using this as well. My issue is, I need the actual Filename returned, without the path.

    local Dir = “\\\\MH-DEV\\ClientFiles\\Import\\5531\\”
    local Filename = “log_export_*”
    local Archive = “Archive”

    — Iterate through the default directory
    — to make a list of the files/dirs we find
    local List = {}
    for Name, FileInfo in os.fs.glob(Dir..Filename) do

    I get the name as
    ‘\\\\MH-DEV\\ClientFiles\\Import\\5531\\log_export_20190124_144315.txt

    I need to save the filename in the database, and move it to the Archive folder when done. If there is not a way to grab that filename, will I need to remove the initial path from the string, and add in the archive path? That seems like a lot more work than it should be.

    Thanks
    Steve

    Steve Ela
    Macro Helix / McKesson
    Software Developer / Integration Lead / Scrum Master

    (looks like the HTML tag functionality is now broken with the recent forum software upgrade)

    Grabbing the filename from the returned path is a fairly straightforward string operation using the split method:

    local fImportPath = “\\\\MH-DEV\\ClientFiles\\Import\\5531\\log_export_20190124_144315.txt”
    local fImportList = fImportPath:split(‘\\’)
    local fImportName = fImportList[#fImportList]

    The above splits the string fImportPath on the backslash character into the array fImportList. The base filename is the last element, which is the same as the array count (returned by #fImportList).

    You can now create the archive path and simply append the filename to it:

    local fArchiveDir = “\\\\MH-DEV\\ClientFiles\\Archive\\5531\\”
    local fArchivePath = fArchiveDir .. fImportName

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Thanks Jeff. Was hoping the built in function would return the filename, but no luck. Will try your suggestion

    Thanks!

    Steve Ela
    Macro Helix / McKesson
    Software Developer / Integration Lead / Scrum Master

    If you think you’ll be doing a lot of this, just create a utility library and include a function in it like the one below:

    function string.getBaseName(path)
       local pathDelim = '\\'
       if os.getenv('OS') ~= 'Windows_NT' then
          pathDelim = '/'
       end
       local pathList = path:split(pathDelim)
       return pathList[#pathList]
    end

    ‘require’ the library and you’ll then be able to invoke it just like any other string function:

    require 'mhutils'
    ...
    
       local baseName = path:getBaseName()

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Excellent. I am still learning LUA and structured programming in general. Much appreciate the tips!

    Steve Ela
    Macro Helix / McKesson
    Software Developer / Integration Lead / Scrum Master

    Yes this did worked. I can now read the file name under the folder/iguana_training

    My next step would be, say i have multiple files under the directory like nationality,race,religion,designation. I extracted the file name (Nationality) using :sub API like below.

    local Dir = [[C:\Program Files\iNTERFACEWARE\Iguana\iguana_training\]]
    for FileDir, FileInfo in os.fs.glob(Dir..’*.csv’) do
    trace(FileDir)
    FileName=FileDir:sub(55)
    if FileName:sub(1,11)==’Designation’ then
    <do some task>
    end

    Here I want to read only the designation file, hence i included in If loop and then do some action. But when I run this channel, all the files were read and no task is performed which is mentioned inside the loop. Though the annotations looks good and works as expected.

    What is it that you’re trying to do with the files … what does <do some task> represent?

    If you’re using io.open() to open a file to perform some operation on the file contents, you’ll need to supply the full path to the file, not just the filename. Same with the os.* functions such as os.remove() and os.rename().

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    I have multiple files under \iguana_training folder like “designation”,”nationality”,”race”,”religion”
    This is my piece of code

    local Dir = [[C:\Program Files\iNTERFACEWARE\Iguana\iguana_training\]]
    for FileDir, FileInfo in os.fs.glob(Dir..’*.csv’) do –look for the files under the specified —
    –directory
    trace(FileDir)
    FileName=FileDir:sub(55) –extract the file name alone
    if FileName:sub(1,11)==’Designation’ then –check for designation file
    trace(FileName)

    local CsvMsg = csv.parseCsv(Data) –parse the csv file and map to a dummy —
    –template
    trace(CsvMsg)
    local XMLTemplate=xml.parse{data=temp}
    local Result = mapdata(XMLTemplate,CsvMsg)
    trace(Result)
    URL=’https://localhost:6544/response ‘ –post the response to a webservice
    UNAME=”
    PWD=”
    local response, status, headers =
    net.http.post{
    url=WFOURL,
    body = Result ,
    live=true,
    auth={username=UNAME,password=PWD},
    headers={[‘Content-Type’]=’text/xml’},
    timeout=300}

    1. I can see the annotations working as expected(capture.jpg) ie it looked for the designation file and done the mapping and if condition failed for other files
    2.When i run this channel, all the files are read when i checked the iguana logs(capture2.jpg).
    3. the csv file is not parsed and no mapping was done. Just a log that message successfully processed by translator

    So i am not sure which part i am missing.

    Attachments:
    You must be logged in to view attached files.

    Well, I’m going to take a guess here that this channel has a “From File” source, and the files it’s configured to pick up are the ones you’re attempting to check for with os.fs.glob().

    If that’s the case, the reason you’re having issues is that the channel’s “From File” process picks the files up, reads them into the queue, then deletes or moves them (depending on what you’ve configured for the channel) before the filter (or “To Translator” endpoint) logic executes and os.fs.glob() therefore can’t find the file at runtime. Everything seems to work OK when you’re working in the translator editor because the “From File” functionality isn’t active … the Data variable is populated from the Sample Message facility.

    If you need to have access to the name of the file when processing it, you’ll need to use a “From Translator” source for your channel and write the code to locate, read, parse, translate, enqueue (or, as in your case, invoke a web service), and dispose of (or archive) the files yourself rather than rely on the “From File” configuration.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

You must be logged in to reply to this topic.