할 수 있는 방법에 추가 작성일로부터의 모든 파일 폴더의 하위 폴더에 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ý
..................................................................................................................