จะให้ฉันเพิ่มการสร้างวันที่แฟ้มทั้งหมดในโฟลเดอร์และโฟลเดอร์ย่อยใน PowerShell?

0

คำถาม

ฉันมีเล็กน้อสคริปต์ที่จะเรียบร้อยแล้คัดลอกแฟ้มทั้งหมดออกจากโฟลเดอร์และโฟลเดอร์ย่อยและต่อไปยังท้ายเวลาที่สร้างแต่งแฟ้มในโฟลเดอร์ย่อยยังไม่มีการสร้างเวลา appended ให้ชื่อพวกเขา

จะให้ฉันเพิ่มการสร้างวันที่แฟ้มทั้งหมดในโฟลเดอร์และโฟลเดอร์ย่อย?

ของปัจจุบันสคริปต์:

$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

ในขณะที่คุณสามารถสร้างของตัวเอง recursive วิธีการจะคัดลอกแฟ้มและเปลี่ยนชื่อพวกเขาอย่างที่คุณไปมันคงจะง่ายกว่าที่จะใช้ Copy-Item recursively และเปลี่ยนชื่อแฟ้มและโฟลเดอร์หลังจากนั้น:

$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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่