Solved

Moving Files using Powershell

Posted on 2011-03-17
8
1,540 Views
Last Modified: 2012-05-11
Hi.

I have two folders src and dest. Inside src there are files with names xxx1, xxx2 .. etc.

Now I want to move these files from src to dest using powershell. The only catch is that when I move them, I want to change the name of the file so that after they are moved, under the dest folder the name of the file is yyyxxx1 instead of xxx1.

I am using the following command to move the file

move-item -path src -destination dest

Can anyone help me do what I want to get done?

Thanks
0
Comment
Question by:Nivlesh
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 35161985
Try a Copy-Item/Remove-Item.

Move-item doesn't always work liked you intend it to.

HTH,

Dale Harris
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35162670
Hi,

Try the following code, it moves the files and then renames the files

regards
Prashanth
$src="c:\test1\*.*"
$dest="d:\test1"

move-item -path $src -destination $dest
 
 Get-ChildItem -path $dest |
 Foreach-Object { 
 	  $_.name
      $newnamestr = $_.name.replace($_.name,"yyy"+$_.name)
	  $newnamestr
  	  Rename-Item -Path $_.fullname -newname ($newnamestr) 
}

Open in new window

0
 
LVL 8

Author Comment

by:Nivlesh
ID: 35178002
the move-item -path $src -destination $dest doesnt work since it comes up with the following error

Move-Item : Source and destination path must have identical roots. Move will not work across volume
s.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 12

Expert Comment

by:prashanthd
ID: 35178458
What will be the values for $src and $dest?

move-item has some limitations.
0
 
LVL 16

Accepted Solution

by:
Dale Harris earned 250 total points
ID: 35181869
Exactly why you must use "Copy-Item" then "Remove-Item" from my first post.

Please try that.  You will not get the error.

HTH,

Dale Harris
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35185998
Try the following code.

Copies the files to destination, renames the files and delete files from source

regards
Prashanth
$src="c:\test1\*.*"
$dest="d:\test1"

Copy-Item -path $src -destination $dest
 
 Get-ChildItem -path $dest |
 Foreach-Object { 
 	  $_.name
      $newnamestr = $_.name.replace($_.name,"yyy"+$_.name)
	  $newnamestr
  	  Rename-Item -Path $_.fullname -newname ($newnamestr) 
}

Remove-Item $src

Open in new window

0
 
LVL 12

Assisted Solution

by:prashanthd
prashanthd earned 250 total points
ID: 35241157
have you tried the above code?
0
 
LVL 8

Author Closing Comment

by:Nivlesh
ID: 35265942
Hi Guys.

Thank you for all your help. I tried the copy and rename and it worked.

Regards
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

806 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