Using dynamic paths as parameters

This topic contains 3 replies, has 2 voices, and was last updated by  Curtis Smith 2 years ago.

  • Author
    Posts
  • #31957

    Neil Kronberg
    Participant

    Hello!

    I am attempting to write a semi-advanced function that will accept paths as parameters. However, I want one of the parameters to depend on an earlier parameter. Basically, it assumes the second item is a direct child item of the parent (a directory). But I'm not sure how to write the code. Here is what I have so far:

    [cmdletbinding()]
    
    Param (
        #Source file for parsing
        [string]$sourcedir = "E:\desired\source\directory",
    
        [string]$sourcefile = "$sourcedir\$sourcefile",
        
        [string]$archivedir = "$sourcedir\Archived",
    
        [string[]]$To = "receivingaddress@company.pri",
        
        [string]$From = "sendingaddress@company.pri"
    )
    

    The $sourcefile parameter is what I'm struggling with. Since I'm feeding it the $sourcedir explicitly with the full path, I want the full path of the $sourcefile to be determined by only feeding the parameter the $sourcefile file name. Does that make sense?

  • #31959

    Curtis Smith
    Participant

    You would not do this in your param section. The only reason to set your various parameters equal to something in the param section is if you want it to default to a specific value if the parameter is not specified.

    So you would typically do something like this where $archivedir would equal "Archived" unless someone specified -archivedir "archives" or such. The $archivedir would be whatever they set.

    [cmdletbinding()]
    
    Param (
        #Source file for parsing
        [string]$sourcedir,
        [string]$sourcefile,
        [string]$archivedir = "Archived",
        [string[]]$To,
        [string]$From
    )
    
    $sourcefilepath = "$sourcedir\$sourcefile"
    $archivedirpath = "$sourcedir\$archivedir"
    
  • #31961

    Neil Kronberg
    Participant

    Thank you Curtis. I guess maybe I was overthinking the param block. Basically in this case the same script needs to be set up as 2 separate scheduled tasks, with a common source directory and 2 different source files (it's a monitoring script). I thought maybe I could make the path dynamic, but it sounds as if it's not very practical to do it that way. To use Mr. Jones' nomenclature, to me this script is more of a "controller" than a "tool" since it has a very specific use case, so I did not mind setting default values for the first and third param. The idea originally was to have the second param accept a file name only (not a complete path), and derive a complete path from that name.

  • #31962

    Curtis Smith
    Participant

    Based on that description, it sounds as though the only thing you want to really be definable at the time of run is the name of the source file to process. As such, the only input parameter your script needs to accept is the source file. The rest of the variables can be part of the main code, The only reason to define a parameter is if you want to be able to change its value when you call the code.

    [cmdletbinding()]
    
    Param (
        [string]$sourcefile
    )
    
    $sourcedir = "E:\desired\source\directory"
    $sourcefilepath = "$sourcedir\$sourcefile"
    $archivedir = "$sourcedir\Archived"
    $To = "receivingaddress@company.pri"
    $From = "sendingaddress@company.pri"
    

You must be logged in to reply to this topic.