mv nested folder and files

Himanshuk 161 Reputation points

I have folder structure like c:\temp\logs\

I want to move main\ , main2\second\ to other location say at $dest= e:\back\logs

gci c:\temp\logs -directory -rec -filter '.zip' | ? {mv $_fullname $dest}
gci c:\temp\logs -rec -filter '
.zip' | ? {mv $_fullname $dest}

both the conditions the folder main folders + nested folders with zips are not as it moved to dest

also while rollback from $dest the all folders and files after zip is not going back as it is to source
what I can modify in gci condition

My question reframe like
I have a directory structure that looks like this:

I want to move the content of the client1,2,3 folders in C:\tmp\ to get this
from folder structure without disturb as it is move


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,434 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points

    If you only want to move two files why go to all the trouble? Just use two separate Move-Item cmdlets.

    Why are you using the -Recurse switch. If you do that you're going to move main\second\ and fmain3\third\forth\, too. And by specifying the -Directory switch you're not going to find any files at all (or directories, unless they end with ".zip").

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 32,321 Reputation points Microsoft Vendor


    As you use the -Recurse and -Filter parameters, not only the two files but all zip files will be moved to $dest. If you want to keep the folder structure you can create the folder before copying the file

    $src= "c:\temp\logs\"  
    $dest= "e:\back\logs\"  
    $files = "main\", "main2\second\"  
    foreach($file in $files){  
        Get-Item -Path (Join-Path -Path $src -ChildPath $file) | ForEach-Object{      
           $path = Join-Path -Path $dest -ChildPath $file.Substring(0,$file.Length - $_.Name.Length)  
           New-Item -Path $path -ItemType Directory  
           Move-Item -Path $_.fullname -Destination $path   

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments