Solved

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

Posted on 2014-12-26
5
357 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
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 68

Accepted Solution

by:
Qlemo earned 500 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now