[英]PowerShell to move subitems of multiple folders into their own subfolders
I'm working on a project cleaning up a file server's folder redirection folders.我正在处理一个清理文件服务器的文件夹重定向文件夹的项目。 Would like to ask for some help with a PS script that would move the files in user folders into new "Documents" sub-folders, as we're facing manually performing this for a lot of profiles.
想寻求一些有关 PS 脚本的帮助,该脚本会将用户文件夹中的文件移动到新的“文档”子文件夹中,因为我们面临着为许多配置文件手动执行此操作。
A tree example currently looks like:当前的树示例如下所示:
―FolderRedirection
―――JContoso
――――――File.docx
――――――File.pptx
―――MDerby
――――――File.docx
――――――File.pptx
I'd like to be able to achieve:我希望能够实现:
―FolderRedirection
―――JContoso
――――――Documents
―――――――――File.docx
―――――――――File.pptx
―――MDerby
――――――Documents
―――――――――File.docx
―――――――――File.pptx
This should work, bear in mind for future questions you should provide at least a minimal attempt at solving the problem.这应该有效,请记住,对于未来的问题,您应该至少尝试解决问题。
The inline comments should help you with the code logic.内联注释应该可以帮助您处理代码逻辑。
# 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.