如何使用powershell重命名文件(例如插入字符)名称

时间:2015-04-01 07:31:11

标签: powershell

我有一个三星手机(注释4),以下列日期格式保存照片20150401_070344

即yyyymmdd_hhmmss

我有1000个这些文件,并希望将它们全部更改为以下格式:

2015-04-01 07.03.44 Wed

我怎样才能使用powershell呢?

此致

乔治

2 个答案:

答案 0 :(得分:0)

使用Get-ChildItem检索照片,对其进行迭代,解析日期并使用下面的脚本对其进行转换,最后使用Rename-Item cmdlet重命名该文件。

$photoDatetime = [datetime]::ParseExact("20150401_070344", "yyyyMMdd_hhmmss", $null)
$formatedPhotoDateTime = "{0:yyyy-mm-dd hh.mm.ss ddd}" -f [datetime]$photoDatetime

答案 1 :(得分:0)

我喜欢Jisaak的方法,但他的代码实际上并没有从当前文件中解析日期和时间。

<强>操作实例

以下是你应该如何做这个GG,

首先,请确保我们正在查看包含我们文件的目录

Get-ChildItem 'C:\users\Stephen\Dropbox\Camera Uploads' | select -first 20
>    Directory: C:\users\Stephen\Dropbox\Camera Uploads


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       12/17/2014   9:25 AM        1151985 2014-11-18 13.53.56.jpg
-a----        12/9/2014  11:35 AM         149247 2014-12-09 16.35.58.png
-a----        12/9/2014   9:20 PM        3046709 2014-12-09 21.20.23.jpg
-a----       12/10/2014   6:33 PM        2496437 2014-12-10 18.33.13.jpg
-a----       12/10/2014   6:33 PM        2889022 2014-12-10 18.33.15.jpg
-a----       12/10/2014   6:33 PM        2298856 2014-12-10 18.33.18.jpg
-a----       12/10/2014   6:33 PM        2290490 2014-12-10 18.33.25.jpg
-a----       12/10/2014   6:33 PM        3079367 2014-12-10 18.33.34.jpg
...

好的,为了实现Jissak的方法,我们将其传输到ForEach对象循环中,并对其代码进行一些调整,如下所示,以查看每个文件的新名称:

Get-ChildItem 'C:\users\Stephen\Dropbox\Camera Uploads' |select -first 20 |
  ForEach-Object {
   $photoDatetime = [datetime]::ParseExact($_.BaseName, "yyyyMMdd_HHmmss", $null)
   $formatedPhotoDateTime = "{0:yyyy-mm-dd hh.mm.ss ddd}" -f [datetime]$photoDatetime
   Write-Output "New file name would be $formatedPhotoDateTime"
}
>New filename would be 2014-53-18 01.53.56 Tue
New filename would be 2014-35-09 04.35.58 Tue
New filename would be 2014-20-09 09.20.23 Tue
New filename would be 2014-33-10 06.33.13 Wed
New filename would be 2014-33-10 06.33.15 Wed
New filename would be 2014-33-10 06.33.18 Wed
New filename would be 2014-33-10 06.33.25 Wed
New filename would be 2014-33-10 06.33.34 Wed
New filename would be 2014-56-10 06.56.02 Wed
...

如果您喜欢新文件名,现在可以重命名每个文件。只需将最后一行从Write-Output更改为此

Rename-Item -Path $_.FullName -NewName "$formatedPhotoDateTime$($_.Extension)" -WhatIf
>What if: Performing the operation "Rename File" on target "Item: C:\users\Stephen\Dropbox\Camera Uploads\2014-11-18 13.53.56
.jpg Destination: C:\users\Stephen\Dropbox\Camera Uploads\2014-53-18 01.53.56 Tue.jpg".
What if: Performing the operation "Rename File" on target "Item: C:\users\Stephen\Dropbox\Camera Uploads\2014-12-09 16.35.58
.png Destination: C:\users\Stephen\Dropbox\Camera Uploads\2014-35-09 04.35.58 Tue.png".

如果您对新名称感到满意,只需删除-Whatif并从管道中删除Select -First 20并最后一次运行它。

已完成的脚本

Get-ChildItem 'C:\users\Stephen\Dropbox\Camera Uploads' | ForEach-Object {
  $photoDatetime = [datetime]::ParseExact($_.BaseName, "yyyyMMdd_HHmmss", $null)
  $formatedPhotoDateTime = "{0:yyyy-mm-dd hh.mm.ss ddd}" -f [datetime]$photoDatetime
  Rename-Item -Path $_.FullName -NewName "$formatedPhotoDateTime$($_.Extension)" -PassThru
}