TimeStamp on file name using PowerShell

前端 未结 5 693
甜味超标
甜味超标 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:44

    Here's some PowerShell code that should work. You can combine most of this into fewer lines, but I wanted to keep it clear and readable.

    [string]$filePath = "C:\tempFile.zip";
    
    [string]$directory = [System.IO.Path]::GetDirectoryName($filePath);
    [string]$strippedFileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath);
    [string]$extension = [System.IO.Path]::GetExtension($filePath);
    [string]$newFileName = $strippedFileName + [DateTime]::Now.ToString("yyyyMMdd-HHmmss") + $extension;
    [string]$newFilePath = [System.IO.Path]::Combine($directory, $newFileName);
    
    Move-Item -LiteralPath $filePath -Destination $newFilePath;
    

提交回复
热议问题