?
Solved

How to do powershell move all the subcontent of folder to dst in recursive mode

Posted on 2014-12-26
5
Medium Priority
?
530 Views
Last Modified: 2014-12-27
Hello, World :)

I looked through the help for: copy-item, move-item, get-childitem before writing this question.
Example:

x:\tmp\
x:\tmp\1.txt
x:\tmp\2.txt
x:\tmp\1\3.txt
x:\tmp\1\4.txt

I need to move all the content (except x:\tmp itself) to the new destination, so i'd get in result:

x:\dst\
x:\dst\1.txt
x:\dst\2.txt
x:\dst\1\3.txt
x:\dst\1\4.txt

The reason why i don't need to move "x:\tmp" itself - there are too many specific NTFS permissions of that folder so, as i need to do movement every night, I don't wanna recreate x:\temp each time and set the same permissions again.

also:
PS X:\> get-childitem -path x:\tmp\ -recurse -force | move-item -destination x:\dst -force

Get-ChildItem : Could not find a part of the path 'X:\tmp\1'.
At line:1 char:14
+ get-childitem <<<<  -path x:\tmp\ -recurse -force | move-item -destination x:\dst -force
    + CategoryInfo          : ReadError: (X:\tmp\1:String) [Get-ChildItem], DirectoryNotFoundException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

I still cant understand why this command generates errors, EVEN it works, BUT i don't wanna use this command: AS IT GENERATES ERRORS (yes, in this was i'm a perfectionist: everything works fine ONLY if it doesn't produce any errors).

Also, robocopy works fine:
robocopy $srcpath $dstpath /E /R:1 /W:10 /NP /MT:2 /ETA /TEE
But, as i'm using PSh instead of batch, and in my opinion everything that is possible for PSh must be used.

Any ideas?
0
Comment
Question by:Petr Poleshko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40519368
if you run this just get the items
get-childitem -path x:\tmp\ -recurse -force
does it generates the error?

I really don't see any syntax error on the command

permission issue could possible generate that directory not found execption
0
 
LVL 2

Author Comment

by:Petr Poleshko
ID: 40519405
does it generates the error?
No, no errors.
Also i run PSh under Administrative account as Administrator
0
 
LVL 2

Author Comment

by:Petr Poleshko
ID: 40519409
I looked deeply into help section for move-item and found whatif parameter:

PS X:\Backup_AllShares> get-childitem -path x:\tmp\ -recurse -force | move-item -destination x:\dst -force -whatif
What if: Performing operation "Move Directory" on Target "Item: X:\tmp\1 Destination: X:\dst\1".
What if: Performing operation "Move File" on Target "Item: X:\tmp\1.txt Destination: X:\dst\1.txt".
What if: Performing operation "Move File" on Target "Item: X:\tmp\2.txt Destination: X:\dst\2.txt".
What if: Performing operation "Move File" on Target "Item: X:\tmp\1\3.txt Destination: X:\dst\3.txt".
What if: Performing operation "Move File" on Target "Item: X:\tmp\1\4.txt Destination: X:\dst\4.txt".

The 1st thing the script tries to do - move folder x:\tmp\1 inspite that there two files inside that folder...
Maybe this causes an error i posted above?
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40519437
Remove the -recurse. Moving folders will also move anything inside them.
The RoboCopy does not move anything, BTW, and it is a single command, so you can't compare both approaches.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40519585
"and in my opinion everything that is possible for PSh must be used"

When there are optimised tools for doing a job, in my opinion USE THEM.

Just have an execute from the Powershell script that runs robocopy. It is FAR MORE efficient.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question