Home > Enterprise >  Powershell Variable assigning issue
Powershell Variable assigning issue

Time:09-27

I am attemping to make the Destination from the Copy Item function be the $path and keep running into syntax error.

Function movefiles($dayhash){
    foreach ($h in $dayhash.GetEnumerator() )
    {
        $path = "$formsfolderDaily Checklists\$today_($h.Value)"
        Copy-Item $formsfolder$($h.Value) -Destination $formsfolder"Daily Checklists\"$today"_"$($h.Value)
        editDate($path)
    }

Desired outcome

Function movefiles($dayhash){
    foreach ($h in $dayhash.GetEnumerator() )
    {
        $path = $formsfolder   "Daily Checklists\"   $today   "_"   ($h.Value)
        Copy-Item $formsfolder$($h.Value) -Destination $path
        editDate($path)
    }

CodePudding user response:

$path = "$formsfolderDaily Checklists\$today_($h.Value)"

This makes me think $FormsFolder is a path variable with a trailing backslash -- BUT THAT'S JUST A GUESS -- and one of the reasons Join-Path is so useful.

It's also hard to know what is a literal and what is part of a variblbe name when you start constructing complex expansion strings. I would recommend using the -f (Format operator) which nicely separates the literal and variable portions of your string. My best guess for the above would be:

  • $path = '{0}Daily Checklists\{1}_{2}' -f $formsfolder, $today, $h.Value

Your template string is on the the left-hand side of the operator, with zero-indexed placeholders in the format {0}, {1}, etc. The placeholders correspond to the variables/expressions/function calls found in the list on the right-hand side of the operator.

CodePudding user response:

It sounds like you want to implement your solution using expandable (double-quoted) strings ("...").

To that end, you need to observe two fundamental rules:

  • In order to disambiguate variable names from subsequent characters, enclose their names in {...}, e.g. $[today} instead of just $today

    • Notably, _ is a legitimate character in a PowerShell variable name, so if a _ follows a variable reference {...} is needed too.
  • In order to embed expressions - such as $h.Value - inside "...", enclose them in $(...), the subexpression operator

    • You've done this in part in your question, but the first command is missing the $ before (.
  • For a complete overview of PowerShell's string-interpolation rules, see this answer.

Additionally:

  • You're using compound tokens composed of unquoted and quoted parts in order to form a single string argument, which is best avoided in PowerShell - see this answer.

  • Instead, use a single, "..."-enclosed string.

Therefore (the assumption is that the value of $formsfolder ends in \ (or /)):

Function movefiles($dayhash) {
  foreach ($h in $dayhash.GetEnumerator() ) {
    $path = "${formsfolder}Daily Checklists\$today_$($h.Value)"
    Copy-Item "${formsfolder}$($h.Value)" -Destination "${formsfolder}Daily Checklists\${today}_$($h.Value)"
    editDate $path
  }
  • Related