stringpowershellconcatenationstring-concatenation

PowerShell - Concatenating two strings with underscore inbetween not working?


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?


Solution

  • 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")