To move the sub directories not the files inside it

Welcome Forums General PowerShell Q&A To move the sub directories not the files inside it

Viewing 2 reply threads
  • Author
    Posts
    • #218094
      Participant
      Topics: 4
      Replies: 2
      Points: 33
      Rank: Member

      $source = “G:\Program Files\Backup”
      $dest = “G:\Program Files\Backup\newfolder” + “.cab”

      The command am executing is
      <# C:
      Get-ChildItem $source | Move-Item -Destination $dest -Force

      #>

      consider Backup folder consists

      subfolder1- file1,file2.

      subfolder2-file1

      after executing the command the newfolder has

      file1,

      file2,

      subfolder2

      but what i want in newfolder

      subfolder1

      subfolder2.

       

      I dont hav any idea why it moves the subfolder 1 files not the folder.

      please guide me !

      Thanks in advance!

    • #218100
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Please, when you post code or sample data or error messages or console output format it as code using the code tags “PRE”. Thanks in advance.

      Does the destination folder already exist?

      Edit: And BTW: You may have a recursion error. Your destination path is in your source path. 😉

    • #218331
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Hello mohankumars69,

      Are you intending to “move” the subfolders but not the files within them? I’ll have to assume no but your subject is confusing. I have a few recommendations for you. First, you should clarify exactly what you are trying to do. Move means no longer existing on the source. If you move all the files/folders out of G:\Program Files\Backup, afterwards you want this folder empty? Clarifying this, if for nothing else, would help you express your clear intentions to others when seeking assistance. Second, until you are more familiar with powershells quirks, I’d break the tasks up. From what I gather you need to first make a list of items you want to move. If you want to move an entire folder with all its contents, you don’t need to recurse. Build the list like so.

      
      $source = Get-ChildItem -Path C:\temp\New\
      
      

      It’s always a great idea just to check that it has exactly what you expect. Especially before a move command.

      
      $source
      
      Directory: C:\temp\New
      
      Mode LastWriteTime Length Name
      ---- ------------- ------ ----
      d----- 4/13/2020 8:28 PM New
      d----- 4/13/2020 8:28 PM New1
      -a---- 4/7/2020 2:40 PM 1176 Content.txt
      
      

      Now that I know it has the list of folders/files I want to move, we can continue. Next thing we want to make sure of is that the destination exists. Otherwise you’ll likely end up with a file with that same name and a nasty, confusing error message. If you want to ensure it’s there before the move, you can code for it.

      
      if(-not(test-path c:\temp\newnew)){new-item -path c:\temp -Type Directory -Name newnew | out-null}
      
      

      Finally, you can continue with the move.

      
      $source | move-item -Destination c:\temp\newnew
      
      

      Now this doesn’t take into consideration of files/folders being in use or many other points that are farther outside the scope of your question than I’ve already gone. Many argue that it would be better to copy the files and once confirmed they are there, complete and not corrupt, then delete from the source.

      Don’t forget that you have the entire help system of Powershell right at your fingertips. A lot of times it is faster than even googling. The following commands you should get sick of typing them so much.

      
      help move-item -online
      
      help move-item -full
      
      help move-item -examples
      
      

      The forums, in my opinion, should be the last resort. I hope this helps.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.