Home > Net >  Duplicating Files in same folder
Duplicating Files in same folder

Time:05-10

Hello Powershell Noob here, I am trying to write a script that will take paths from my clipboard and just duplicate them right in their parent folder.

Lets say my clipboard looks like this:

"C:\Users\User1\New Folder\Image A.jpg"
"C:\Users\User1\New Folder\Image B.jpg"
"C:\Users\User1\New Folder\Image C.jpg"

I know its just a matter of Copy-Item but I am stuck on correctly appending "(1)" on the file name each file name. I've tried:

$objectList = Get-Clipboard
Foreach($Object in $ObjectList){
$i
$NewName = Join-Path -Path $Object -ChildPath ((Split-Path $Object -Leaf)   $i)
Copy-Item -Path $Object -Destination $NewName}

Sometimes I get a result like:

"C:\Users\User1\New Folder\Image A.jpg1"

but I mostly just end up getting red errors, I've read the docs and trawled stack overflow and I've gotton even more confused.

Can anyone help me understand how to do this? Thanks!

CodePudding user response:

Something like this should work:

$objectList = Get-Clipboard
Foreach ($Object in $ObjectList) {
    $File = Get-Item -Path $Object
    $NewName = Join-Path -Path $File.Directory -ChildPath ($File.BaseName   '1'   $File.Extension)
    Copy-Item -Path $File.FullName -Destination $NewName
}
  • Related