Come posso aggiungere la data di creazione di tutti i file in una cartella e le sottocartelle in PowerShell?

0

Domanda

Ho un piccolo script che consente di copiare tutti i file da cartelle e sottocartelle e aggiungere l'ora di creazione, ma i file in sottocartelle non hanno il tempo di creazione aggiunto i loro nomi.

Come posso aggiungere la data di creazione di tutti i file in una cartella e le sottocartelle?

Il mio script corrente è:

$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

Migliore risposta

0

Sei stato davvero vicino, ma il -Recurse interruttore sono Get-ChildItem e all'interno del ciclo, è necessario assicurarsi che la destinazione sottocartella percorsi esistenti.

Provare

$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

Mentre si potrebbe creare il proprio metodo ricorsivo per copiare i file e rinominarlo come si va, sarebbe più facile da usare Copy-Item in modo ricorsivo e rinominare i file e le cartelle in seguito:

$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

In altre lingue

Questa pagina è in altre lingue

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