none
Powershell move folder and subfolders

    Question

  • hi gurus,

    got this code from Technet: http://technet.microsoft.com/en-us/library/ee692739.aspx

    Move-Item C:\scripts\temp* C:\old

    The code above will move all the folders that has a file name beginning with temp and if the temp folder has a lot of sub folders will it be moved also?

    Thanks in advance :)


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Tuesday, October 22, 2013 3:54 AM

Answers

All replies

  • The code above will move all the folders that has a file name beginning with temp and if the temp folder has a lot of sub folders will it be moved also?

    Move-Item C:\scripts\temp* C:\old

    With the above command :

    Yes if the folder\filename name starts with temp.


    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.


    Tuesday, October 22, 2013 4:09 AM
  • Thanks Azam.

    Just one more question, is there a way to check whether the moving of files was successful or there were some errors?


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Tuesday, October 22, 2013 4:55 AM
  • Hi,

    Move-Item doesn't output anything by default, but you'll see any errors appear in the console.

    EDIT: Example:

    PS C:\Scripting\Testing\10-22-2013> Move-Item .\* .\test
    Move-Item : The process cannot access the file because it is being used by another process.
    At line:1 char:1
    + Move-Item .\* .\test
    + ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\Scripting\Testing\10-22-2013\test:DirectoryInfo) [Move-Item], IOException
        + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
     
    Move-Item : The process cannot access the file because it is being used by another process.
    At line:1 char:1
    + Move-Item .\* .\test
    + ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\Scripting\Testing\10-22-2013\all.csv:FileInfo) [Move-Item], IOException
        + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)


    Tuesday, October 22, 2013 4:58 AM
  • Thanks Mike, so basically there is no way to trap the error.

    Need to check whatever output on the console?


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Tuesday, October 22, 2013 5:19 AM
  • You can redirect the errors to a file if you'd like:

    http://technet.microsoft.com/en-us/library/hh847746.aspx

    Move-Item .\* .\test 2>> .\errs.txt

    You can also test if there were any errors by checking the value of $? once the move has completed.


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)

    • Marked as answer by cguan Tuesday, October 22, 2013 10:33 AM
    Tuesday, October 22, 2013 5:32 AM
  • Wow, Thanks Mike.

    That's a treat!!!

    Thank you very much..

    So it looks like a bash script with 2> hmmm... never cross my mind that Powershell does it  also.

    Is there a documentation about Ternary operators on Technet? 

     -:) 


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Tuesday, October 22, 2013 6:14 AM
  • You're very welcome, glad I could help out. Check out the link I posted above, it goes through the redirection operators that are available. Most of them require PS 3.0+, but they're helpful nonetheless.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)

    Tuesday, October 22, 2013 6:12 PM
  • You're very welcome, glad I could help out. Check out the link I posted above, it goes through the redirection operators that are available. Most of them require PS 3.0+, but they're helpful nonetheless.

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)

    So basically Powershell is living to its name, it is really a Powerful Shell.. Bravo!!! Powershell Engineers...

    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Wednesday, October 23, 2013 3:35 AM