Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 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