Solved

Moving Files using Powershell

Posted on 2011-03-17
8
1,537 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
 
LVL 12

Expert Comment

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

move-item has some limitations.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

910 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

22 Experts available now in Live!

Get 1:1 Help Now