PowerShell将多个文件夹的子标签移至自己的子文件夹中

发布于 2025-02-07 10:33:35 字数 444 浏览 2 评论 0原文

我正在研究一个项目,以清理文件服务器的文件夹重定向文件夹。想在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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2025-02-14 10:33:35

这应该有效,请记住以后的问题,您应该至少提供最小的尝试来解决该问题。

内联注释应帮助您使用代码逻辑。

# 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文