PowerShell - Concatenating two strings with underscore inbetween not working?
Asked Answered
P

1

5

I am trying to append a timestamp to the name of a file and then move that file into another directory. Here is a code example:

$sourceFiles= Get-ChildItem $sourcePath\* -Include *.csv

ForEach($sourceFile in $sourceFiles)
{
    $fileNameWithoutExtension = $sourceFile.BaseName
    $timestamp = $(Get-Date -f yyyy-MM-dd_HH-mm_ss)
    $processedFile = Join-Path -Path $processedPath -ChildPath "$fileNameWithoutExtension_$timestamp.csv"   
    Move-Item -Path $sourceFile -Destination $processedFile
}

When I execute this it seems like "$fileNameWithoutExtension_$timestamp.csv" completely ignores the content of the $fileNameWithoutExtension variable and only includes the timestamp into the file name. I also debugged this already and checked the content of the $fileNameWithoutExtension variable and it does indeed contain the correct filename without the extension. Why is that and how can I create the file name correctly?

Permeance answered 29/10, 2020 at 10:42 Comment(0)
V
6

This is because the underscore is a valid character in a variable name (look here), so PowerShell basically concatenates $fileNameWithoutExtension_ + $timestamp + .csv. The first variable (name incl. underscore) doesn't exist, so it's interpreted as null / empty.

Try one of these solutions (of course, there are more):

# curly-bracket-notation for variables
"${fileNameWithoutExtension}_${timestamp}.csv"

# sub-expression operator
"$($fileNameWithoutExtension)_$($timestamp).csv"

# escape character
"$fileNameWithoutExtension`_$timestamp.csv"

# format operator
"{0}_{1}.csv" -f $fileNameWithoutExtension, $timestamp

# string concatenation
($fileNameWithoutExtension + "_" + $timestamp + ".csv")
Viceroy answered 29/10, 2020 at 10:43 Comment(2)
Thanks for the prompt answer! I tried your first solution but there seems to be an error in it because it includes the brackets in the file name. However, the second one works just fine!Permeance
Option 1 "curly-bracket-notation" worked for me. Thanks! :)Voiced

© 2022 - 2024 — McMap. All rights reserved.