【发布时间】:2021-02-11 19:12:05
【问题描述】:
我正在尝试将时间戳附加到文件名,然后将该文件移动到另一个目录中。 这是一个代码示例:
$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
}
当我执行此操作时,"$fileNameWithoutExtension_$timestamp.csv" 似乎完全忽略了$fileNameWithoutExtension 变量的内容,并且仅将时间戳包含在文件名中。我也已经对此进行了调试并检查了$fileNameWithoutExtension 变量的内容,它确实包含正确的文件名,但没有扩展名。为什么会这样?如何正确创建文件名?
【问题讨论】:
标签: string powershell concatenation concat string-concatenation