最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows scripting - Bulk move the entire subdirectory containing a file to the root folder while preserving its structure - Stac

programmeradmin1浏览0评论

I am looking to bulk move the entire subdirectory containing a file to the root folder. Meaning if there are empty subdirectories in between, they will be deleted but everything beyond the level where the first file was seen will be preserved. Please note that the filenames and folder names may contain non-ascii characters and the first subdirectory from the root that contains a file may at least be 3 levels down

From:

Root | Sub 1 > Sub 2 > Sub 3 > A. files and subfolders > Sub > Sub
Root | Sub 4 > Sub 5 > B. file, E. file
Root | Sub 6 > Sub 7 > Sub 8 > Sub 9 > C. file and subfolders > Sub > Sub > Sub > Sub > D. file

To:

Root | Sub 3 > A. file and subfolders > Sub > Sub
Root | Sub 5 > B. file, E. file
Root | Sub 9 > C. file and subfolders > Sub > Sub > Sub > Sub > D. file

I am looking to bulk move the entire subdirectory containing a file to the root folder. Meaning if there are empty subdirectories in between, they will be deleted but everything beyond the level where the first file was seen will be preserved. Please note that the filenames and folder names may contain non-ascii characters and the first subdirectory from the root that contains a file may at least be 3 levels down

From:

Root | Sub 1 > Sub 2 > Sub 3 > A. files and subfolders > Sub > Sub
Root | Sub 4 > Sub 5 > B. file, E. file
Root | Sub 6 > Sub 7 > Sub 8 > Sub 9 > C. file and subfolders > Sub > Sub > Sub > Sub > D. file

To:

Root | Sub 3 > A. file and subfolders > Sub > Sub
Root | Sub 5 > B. file, E. file
Root | Sub 9 > C. file and subfolders > Sub > Sub > Sub > Sub > D. file
Share Improve this question edited Apr 1 at 10:11 Compo 38.8k5 gold badges31 silver badges45 bronze badges asked Apr 1 at 1:11 vavvav 436 bronze badges 3
  • 1 Stackoverflow <stackoverflow> is not a free script/code writing service. Edit relevant section(s) of what you have tried into your question along with appropriate representative data (use cut/paste) & say what your actual & expected results are. We can try to help with specific problems. You should also read How do I ask a good question? <stackoverflow/help/how-to-ask>. Requests for code to be written are off-topic & are likely to be closed. – Magoo Commented Apr 1 at 8:09
  • @Magoo I'm not sure I'm allowed to post AI generated or modified script. I did try generating scripts w/ ChatGPT but it got me nowhere & so I deleted them. Posted here after that & Daniel kindly responded but it wasn't working and I fed it to ChatGPT, got it to work (using ChatGPT since I can't read/write Powershell) to a point but I wanted it to be chatty (log & console), look pretty w/ color coded error messages and be able to handle different types of collision, however, it kept reverting its changes & then I got sidetracked by cooking dinner so I didn't have time to respond to Daniel. – vav Commented Apr 1 at 10:54
  • I believe the ban on ChatGPT is a ban on mechanically "solving" posted problems using such AI tools and publishing the results - an exercise in cut-and-paste instead of actually generating a solution that can be massaged to suit other scenarios. They are typically characterised by a vague explanation of how they work, if any and are likely to be untested. The objective is to foster understanding of HOW to solve the problem and not having a follow-up question of the same thing in green. I don't talk Powersmell, so if that solution suits you, so be it. – Magoo Commented Apr 2 at 0:27
Add a comment  | 

1 Answer 1

Reset to default 0

Consider creating a new root folder rather than modifying directly and if satisfied you can delete the old root and rename the new root

function Copy-FirstSubdirectoriesWithFilesToNewRoot {
    param(
        [string]$SourceRootDirectory,
        [string]$DestinationRootDirectory
    )

    function GetFirstFolderThatHasFile {
        param(
            $Path
        )

        if (Get-ChildItem $Path -File) {
            # if files exist return the folders path
            $Path
        }
        else {
            # if no files then recursively check any subfolders
            Get-ChildItem $Path -Directory | ForEach-Object { GetFirstFolderThatHasFile $_ }
        }
    }

    GetFirstFolderThatHasFile $SourceRootDirectory | Copy-Item -Recurse -Destination $DestinationRootDirectory
}


Copy-FirstSubdirectoriesWithFilesToNewRoot $oldRootPath $newRootPath

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论