TimeStamp on file name using PowerShell

前端 未结 5 692
甜味超标
甜味超标 2020-12-07 13:18

I have a path in a string,

\"C:\\temp\\mybackup.zip\"

I would like insert a timestamp in that script, for example,

\"C:\\temp\\myb

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 13:32

    You can insert arbitrary PowerShell script code in a double-quoted string by using a subexpression, for example, $() like so:

    "C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
    

    And if you are getting the path from somewhere else - already as a string:

    $dirName  = [io.path]::GetDirectoryName($path)
    $filename = [io.path]::GetFileNameWithoutExtension($path)
    $ext      = [io.path]::GetExtension($path)
    $newPath  = "$dirName\$filename $(get-date -f yyyy-MM-dd)$ext"
    

    And if the path happens to be coming from the output of Get-ChildItem:

    Get-ChildItem *.zip | Foreach {
      "$($_.DirectoryName)\$($_.BaseName) $(get-date -f yyyy-MM-dd)$($_.extension)"}
    

提交回复
热议问题