?
Solved

Moving Files using Powershell

Posted on 2011-03-17
8
Medium Priority
?
1,570 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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