簡體   English   中英

復制文件夾(包括內容)並粘貼到帶有日期的存檔文件夾中

[英]Copy Folder (contents included) and Paste in Archive Folder with Date

我是 PowerShell 的新手,有一項任務是將文件夾及其內容復制到存檔文件夾。 復制的文件夾也必須用日期重命名。

我一直在處理一些測試文件夾,但在弄清楚如何復制文件夾 + 其內容時遇到了麻煩,更不用說重命名復制的文件夾了。

我可以移動文件夾

Copy-Item -Path C:\Test -Destination C:\Archive

但是內容沒有附帶。 我也不確定如何申請最好的申請

$Date = (Get-Date -UFormat "%m%d%Y")
(Get-Date -UFormat "%m%d%Y")

為了用日期重命名復制的文件夾。

您可以首先使用名稱中的日期創建備份文件夾,然后遞歸復制源中的所有內容。

$source = 'C:\Temp'
$backupDestination = 'C:\Backups'

# Will Create: 2019-02-20T15.43.05    
$backupDate = (get-date -Format s) -replace ':', '.'

# sourceName will be set to "Temp"
$sourceName = (Get-Item $source).BaseName

# Combine the destination with the source name and the backup date
# to create C:\Backup\Temp-2019-02-20T15.43.05
$backupDirectory = Join-Path $backupDestination "$sourceName-$backupDate"

# Create the backup destination directory
New-Item -ItemType Directory -Path $backupDirectory

# Copy everything recursively
Copy-Item $source\** $backupDirectory\ -Recurse

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM