Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1593
  • Last Modified:

Moving Files using Powershell

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
Nivlesh
Asked:
Nivlesh
  • 4
  • 2
  • 2
2 Solutions
 
Dale HarrisProfessional Services EngineerCommented:
Try a Copy-Item/Remove-Item.

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

HTH,

Dale Harris
0
 
prashanthdCommented:
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
 
NivleshAuthor Commented:
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
prashanthdCommented:
What will be the values for $src and $dest?

move-item has some limitations.
0
 
Dale HarrisProfessional Services EngineerCommented:
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
 
prashanthdCommented:
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
 
prashanthdCommented:
have you tried the above code?
0
 
NivleshAuthor Commented:
Hi Guys.

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

Regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now