Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

OfficeMate Freezes on login or does not load after login credentials are input.
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 walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

706 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

19 Experts available now in Live!

Get 1:1 Help Now