我怎么能追加的创建日期的所有文件夹中的文件和文件夹在PowerShell?

0

的问题

我有一个小小的脚本,可以成功地复制所有文件夹和子文件夹和追加的创建时间,但是本文件在文件夹没有创建所附时间,以他们的名字。

我怎么能追加的创建日期的所有文件夹中的文件和文件夹?

我前脚本是:

$path = "C:\test1"
$destination = "C:\test2"

Get-ChildItem -path $path | ForEach-Object{
        $newname = $_.CreationTime.toString("yyyy-MM-dd") + $_.BaseName +$_.Extension
        (Copy-Item -Recurse -Path $_.FullName -Destination ( Join-Path  $destination $newname)) 
}

powershell
2021-11-23 21:01:36
2

最好的答案

0

你真的很接近,但是 -Recurse 关应该已经上 Get-ChildItem 和内的循环需要确保目标的子文件夹路存在。

尝试

$source      = "C:\test1"
$destination = "C:\test2"

Get-ChildItem -Path $source -File -Recurse | ForEach-Object {
    # create the new target folderpath for the copy
    $targetPath = Join-Path -Path $destination -ChildPath $_.DirectoryName.Substring($source.Length)
    # make sure the target path exists, if not create it
    $null = New-Item -ItemType Directory -Path $targetPath -Force
    # create a new filename with CreationDate prefixed
    $newName = '{0:yyy-MM-dd}{1}{2}' -f $_.CreationTime, $_.BaseName, $_.Extension
    # copy the file
    $_ | Copy-Item -Destination (Join-Path -Path $targetPath -ChildPath $newname) -Force
}
2021-11-24 12:41:27
0

同时您可以创建自己的递归的方法复制文件并重新命名为你走了,它会更容易使用 Copy-Item 递归和重新命名的文件和文件夹之后:

$Source = "src"
$Destination = "dst"

Copy-Item -Recurse $Source $Destination

foreach ($Item in (Get-ChildItem -Recurse -File $Destination)) {
    Rename-Item $Item ($Item.Name + "-" + $Item.CreationTime.toString("yyyy-MM-dd"))
}
2021-11-23 22:41:19

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................