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?