Link to home
Start Free TrialLog in
Avatar of Jim Metcalf
Jim MetcalfFlag for United States of America

asked on

Powershell change

I have a powershell script below.  it grabs the latest txt file in a directory and then moves it to another directory.  basic copy and paste but only retrieving the latest file.

In my script i am naming the file a new name when i paste it into the desired folder.   I want it to keep the same name that it originally copied.  can someone show me how to do this.

example.. the following scipt may grab a file named dog.txt   but it always places this file as recent.txt in the final location.  i would like it to keep the original name of dog.txt

$dst = "\\contoso1\share"  # change to your source directory
$src = "C:\Users\john.doe\Desktop"  # change to your destination directory
$latest = (Get-ChildItem -Path $src -Filter *.txt -File |
    sort -Property LastWriteTime -Descending | select -First 1).FullName

Copy-Item -Path $latest -Destination (Join-Path -Path $dst -ChildPath recent.txt) -Force
ASKER CERTIFIED SOLUTION
Avatar of Sean
Sean
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Metcalf

ASKER

Boom!
it worked.....
Thanks gentlemen