簡體   English   中英

PowerShell 將多個文件夾的子項移動到它們自己的子文件夾中

[英]PowerShell to move subitems of multiple folders into their own subfolders

我正在處理一個清理文件服務器的文件夾重定向文件夾的項目。 想尋求一些有關 PS 腳本的幫助,該腳本會將用戶文件夾中的文件移動到新的“文檔”子文件夾中,因為我們面臨着為許多配置文件手動執行此操作。

當前的樹示例如下所示:

―FolderRedirection
―――JContoso
――――――File.docx
――――――File.pptx
―――MDerby
――――――File.docx
――――――File.pptx

我希望能夠實現:

―FolderRedirection
―――JContoso
――――――Documents
―――――――――File.docx
―――――――――File.pptx
―――MDerby
――――――Documents
―――――――――File.docx
―――――――――File.pptx

這應該有效,請記住,對於未來的問題,您應該至少嘗試解決問題。

內聯注釋應該可以幫助您處理代碼邏輯。

# get all folders in `FolderRedirection`
foreach($dir in Get-ChildItem .\FolderRedirection -Directory) {
    # create a new `Documents` folder in each sub folder
    $dest = $dir | Join-Path -ChildPath Documents
    $dest = New-Item $dest -ItemType Directory -Force
    # get all files in each folder and move them to the new folder
    $dir | Get-ChildItem -File | Move-Item -Destination $dest
}

暫無
暫無

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

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