Combine path, file strings and literals for path

谁都会走 提交于 2019-12-02 16:18:25

问题


Trying to combine a path, filename, and add some text along with a variable for Out-File log.

I've tried many alternatives unsuccessfully and need assistance;

FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$OldVersion = C:\Temp\TestFile.txt
$OldPath = (Get-Item $OldVersion).DirectoryName
$OldBaseName = (Get-Item $OldVersion).BaseName
ErrFile = Join-Path $OldPath OldBaseName

Out-File -FilePath "$ErrFile_$FormattedDate Error.txt"
Out-File -FilePath "$($OldPath)$($OldBaseName)_$($FormattedDate)_Error.txt"

...just two examples.

I've tried many other combinations and driving me crazy.

Basically I want it to be.

C:\Temp\TestFile_2017-08-24 16:51:36_Error.txt

Update:

I've tried both

$filename = '{0}_{1:s}_Error{2}' -f $basename, (Get-Date), $extension

I get _2017-08-25T13:02:17_Error.txt but no basename (TestFile).

$newpath = "${dirname}\${basename}_${date}_Error${extension}"

I get

A drive with the name '_2017-08-25 13' does not exists.

Can you also explain or provide a resource of what '{0}_{1:s}_Error{2}' and/or '{0}_{1:yyyy-MM-dd HH:mm:ss}_Error{2}' does?


回答1:


Use the format operator (-f) for constructing the filename and Join-Path for building the path.

$oldpath   = 'C:\Temp\TestFile.txt'

$basename  = [IO.Path]::GetFileNameWithoutExtension($oldpath)
$extension = [IO.Path]::GetExtension($oldpath)

$filename  = '{0}_{1:yyyy-MM-dd HH:mm:ss}_Error{2}' -f $basename, (Get-Date), $extension

$newpath   = Join-Path ([IO.Path]::GetDirectoryName($oldpath)) $filename

Unless you must have the space in the date format you could simplify the format string by using the standard sortable format specifier (s) that will produce date strings like 2017-08-24T23:58:25 instead of 2017-08-24 23:58:25.

$filename  = '{0}_{1:s}_Error{2}' -f $basename, (Get-Date), $extension

If you want to construct the path as a string with inline variables you need to make sure that the underscores in your file name are kept separate from the variable name. Because underscores are valid name components for variable names $var_ is the variable var_, not the variable var followed by a literal underscore. Use curly braces to ensure that variables and literal underscores don't get mixed up.

$oldpath   = 'C:\Temp\TestFile.txt'

$date      = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
$dirname   = [IO.Path]::GetDirectoryName($oldpath)
$basename  = [IO.Path]::GetFileNameWithoutExtension($oldpath)
$extension = [IO.Path]::GetExtension($oldpath)

$newpath   = "${dirname}\${basename}_${date}_Error${extension}"

Addendum: Your file names should not contain colons. Colons in Windows paths either terminate a drive name or separate the file name from the name of an alternate data stream. Your date format string should rather be something like yyyy-MM-dd HH-mm-ss or yyyy-MM-dd_HH-mm-ss to avoid this pitfall.



来源:https://stackoverflow.com/questions/45871117/combine-path-file-strings-and-literals-for-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!