Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, June 26, 2012 6:56 PM
I want to create a script that will allow me top copy a file addign a date/time stamp to the file name, and delete the original one.
Can someone help me. I've seen a lot of samples but its not very clear to me.
Paul Arbogast
All replies (8)
Tuesday, June 26, 2012 8:09 PM âś…Answered
You can't have colons in your file name.
Grant Ward, a.k.a. Bigteddy
Tuesday, June 26, 2012 7:01 PM | 2 votes
Can't hand ya over everything now can I ;-)
Here's how to name the file with a date in it
$filename = "whatever_you_want" + (Get-Date -Format "mm-dd-yyyy")
Tuesday, June 26, 2012 7:14 PM
To copy a file and delete the original is called moving the object. Use Move-Item.
Grant Ward, a.k.a. Bigteddy
Tuesday, June 26, 2012 8:02 PM
Thanks D-doug, that bit of script helps
but for some reason its not letting me actually use the variable to change the name of the file.
This is what I have
$filename = "Iamthelog_" + (get-date -Format "MM-dd-yyyy_hh:mm:ss")
Copy-Item c:\testing1\testinglvl2\testinglvl3\Iamthelog.log c:\testing1\testinglvl2\testinglvl3\archive\
Rename-Item -path c:\testing1\testinglvl2\testinglvl3\archive\Iamthelog.log -NewName $filename
The Rename_Item commandlet works if I do not use $filename, but instead usse somethinglike Iamthelog.bak
I end up getting the following error
Rename-Item : Cannot rename because the target specified represents a path or device name.
At line:3 char:12
- Rename-Item <<<< -path c:\testing1\testinglvl2\testinglvl3\archive\Iamthelog.log -NewName $filename
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand
Paul Arbogast
Tuesday, June 26, 2012 8:18 PM
Ah yes, that would do it Bigteddy much thanks
Paul Arbogast
Sunday, January 21, 2018 6:36 AM
i tried to remove the colons still getting the error, can you share what you changed in the script
Friday, March 16, 2018 3:38 PM
To insert date & time stamp in file name, you can use something like,
$FileName = "FileName" + (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
Friday, March 16, 2018 4:35 PM
You know you can do this to get a timestamp whenever you want it?
(get-item filename).lastwritetime
Wednesday, July 12, 2017 11:06:10 AM