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
LVL 8
NivleshAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Dale HarrisConnect With a Mentor Professional 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
 
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
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
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
 
prashanthdCommented:
What will be the values for $src and $dest?

move-item has some limitations.
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
 
prashanthdConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.