mv nested folder and files

Himanshuk 161 Reputation points
2021-02-24T19:02:43.037+00:00

I have folder structure like c:\temp\logs\
main\log.zip
main\second\log.zip
main2\log2.zip
fmain3\third\forth\log33.zip

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

gci c:\temp\logs -directory -rec -filter '.zip' | ? {mv $_fullname $dest}
OR
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:
C:\folderA\folderB\folderM\client1\f1.zips
C:\folderA\folderB\folderN\client1\f2.zips
C:\folderA\folderB\folderS\client2\f1.zips

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

C:\tmp\client1\f1.zips
C:\tmp\client2\f1.zips
C:\tmp\client3\f1.zips

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
    2021-02-24T20:02:46.097+00:00

    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\log.zip and fmain3\third\forth\log33.zip, 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
    2021-02-25T06:17:27.38+00:00

    Hi,

    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\log.zip", "main2\second\log2.zip"  
    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