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
Solved

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

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

828 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