Powershell 重命名目录中的文件
Powershell rename the files from a directory
$ht = @{}
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\LAA"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\LBB"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\LCC"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie" }
$ht.Add($o.from, $o)
foreach($server in $ht.keys ) {
copy-item $ht.Item($server).from -destination $ht.Item($server).to -Recurse ;
}
# rename
##$destination = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie"
#foreach($fisier in $destination) {
# Get-ChildItem $fisier |
# Rename-Item -NewName {$_.Basename + '_' + $_.CreationTime.Year + $_.CreationTime.Month + $_.CreationTime.Day + '_'+ $_.CreationTime.Hour + $_.CreationTime.Minute + $_.Extension }
#}
定义的 ht 中的复制部分正在运行,现在在我复制文件夹和每个文件的内容后,我想将日期和时间添加到该文件夹中的每个文件,但不更改文件夹的名称目的地。最后一部分仍然让我头疼,因为当我在尝试重命名时从源复制文件夹及其内容时,它也会重命名文件夹,而我只想要文件夹的内容。例如,如果我有包含 50 个项目的文件夹 LAA,我只想添加这 50 个项目,重命名如下,创建日期和时间。
您可以像这样在同一个循环中使用新名称复制文件:
foreach($server in $ht.keys ) {
# for clarity..
$source = $ht.Item($server).from
$destination = $ht.Item($server).to
Get-ChildItem -Path $source -File -Recurse | ForEach-Object {
# create the target path for the file
$targetPath = Join-Path -Path $destination -ChildPath $_.FullName.Substring($source.Length)
# test if this path exists already and if not, create it
if (!(Test-Path $target -PathType Container)) {
$null = New-Item -ItemType Directory -Path $targetPath
}
# create the new file name
$newName = '{0}_{1:yyyyMMdd_HHmm}{2}' -f $_.BaseName, $_.CreationTime, $_.Extension
$_ | Copy-Item -Destination (Join-Path -Path $targetPath -ChildPath $newName)
}
}
希望对您有所帮助
$ht = @{}
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\LAA"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\LBB"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie" }
$ht.Add($o.from, $o)
$o = new-object PSObject -property @{
from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\LCC"
to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie" }
$ht.Add($o.from, $o)
foreach($server in $ht.keys ) {
copy-item $ht.Item($server).from -destination $ht.Item($server).to -Recurse ;
}
# rename
##$destination = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie"
#foreach($fisier in $destination) {
# Get-ChildItem $fisier |
# Rename-Item -NewName {$_.Basename + '_' + $_.CreationTime.Year + $_.CreationTime.Month + $_.CreationTime.Day + '_'+ $_.CreationTime.Hour + $_.CreationTime.Minute + $_.Extension }
#}
定义的 ht 中的复制部分正在运行,现在在我复制文件夹和每个文件的内容后,我想将日期和时间添加到该文件夹中的每个文件,但不更改文件夹的名称目的地。最后一部分仍然让我头疼,因为当我在尝试重命名时从源复制文件夹及其内容时,它也会重命名文件夹,而我只想要文件夹的内容。例如,如果我有包含 50 个项目的文件夹 LAA,我只想添加这 50 个项目,重命名如下,创建日期和时间。
您可以像这样在同一个循环中使用新名称复制文件:
foreach($server in $ht.keys ) {
# for clarity..
$source = $ht.Item($server).from
$destination = $ht.Item($server).to
Get-ChildItem -Path $source -File -Recurse | ForEach-Object {
# create the target path for the file
$targetPath = Join-Path -Path $destination -ChildPath $_.FullName.Substring($source.Length)
# test if this path exists already and if not, create it
if (!(Test-Path $target -PathType Container)) {
$null = New-Item -ItemType Directory -Path $targetPath
}
# create the new file name
$newName = '{0}_{1:yyyyMMdd_HHmm}{2}' -f $_.BaseName, $_.CreationTime, $_.Extension
$_ | Copy-Item -Destination (Join-Path -Path $targetPath -ChildPath $newName)
}
}
希望对您有所帮助