Moving, renaming & copying folder trees

Tagged: , ,

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 6 months ago.

  • Author
    Posts
  • #16004
    Profile photo of Gary Pietila
    Gary Pietila
    Participant

    I have a scenario where we want to roll back certain folders on a drive that contain a common text file while preserving the folders & sub folders that contain those test files by moving them to a different location & prepending OLD_ to the foldernames down the folder structure.

    We have mounted a earlier .vhd backup of the drive which we want use to replace the folder structure we just moved in the previous paragraph.

    All files under this folder tree need to get moved in the first part & copied in the second part.

    Any ideas on how I would do this in PowerShell?

  • #16006
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You have a bunch of basic steps there: Get-ChildItem to identify the folders that you want to target (probably by looking for this text file you mentioned), and some combination of Move-Item, Rename-Item and/or Copy-Item to do the rest. The exact details will depend on your requirements, but if I understand you correctly, it sounds like this would be the basic algorithm:

    – Search the drive for this text file.
    – In each location that it's found, move the text file's parent folder and all its child files/folders to a new location.
    – After moving the folder tree, rename all of the folders in that tree to prepend an OLD_ name.
    – After moving the folder tree, copy in the version of it from your backup VHC.

    There are only a couple of "gotchas" to watch out for there, that I can think of off the top of my head:

    – If there are multiple copies of the text file in the same folder tree, you'd want to ignore the deeper ones, since that folder's already getting moved and renamed anyway. Trying to move the child folder later might cause errors, or might screw up your data by overwriting something in the wrong order.

    – When renaming folders, there's a slight chance that you might run into naming conflicts. Say, for example, that a folder has subfolders named "Dir1" and "OLD_Dir1" already; if you tried to rename Dir1 to OLD_Dir1 first, you'd get an error, but doing them in the opposite order would be fine. If there's a chance of that happening, you'd want the code to account for it in some way.

You must be logged in to reply to this topic.