Reputation: 473
This is driving me bananas. I am assuming I am missing something stupid, but I have tried about 10 different formats and nothing works.
This is the code I am running:
$today=$(Get-Date -Format o)
$destfile = "C:\SEA-FTP\toFTP\SEAInfo-$today.csv"
$srcfile = "C:\SEA-FTP\temp\QueryCSVTempMstr.csv"
Copy-Item $srcfile -Destination $destfile
This is the error message:
Copy-Item : The given path's format is not supported.
At C:\SEA-FTP\BCP_SQL_Script-v1.0.ps1:53 char:10
+ Copy-Item <<<< $srcfile -Destination $destfile
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
I know there are a million questions with this in Google search, but it looks like I am doing what they are doing. Any thoughts on the format?
Upvotes: 2
Views: 12155
Reputation: 865
Windows does not allow colons (:) in a filename.
Get-Date -Format o
Gives you the output: "2016-08-23T07:54:19.4530323+02:00" Which does include colons.
If you want to keep the name as it is, you could replace the colon ":" with a dash. In your example:
$Today = Get-Date -Format o
$SrcFileName = "C:\temp\SEAInfo-$($Today.Replace(":","-")).csv"
But if it was my files, I would change the timestamp format. The filename "SEAInfo-2016-08-23T08-33-36.3268865+02-00.csv" is just waaay to hard for my eyes to get a good understanding of when this file was created..
Depending on how detailed you need to have your timestamp, you could use:
$Today = Get-Date -Format "yyyyMMdd-HHMMss.ff"
$SrcFileName = "C:\temp\SEAInfo$Today.csv"
Or maybe just the date will be enough?
$Today = Get-Date -Format "yyyy-MM-dd"
$SrcFileName = "C:\temp\SEAInfo-$Today.csv"
My eyes would appreciate a filename like: "SEAInfo2016-08-23.csv"
You should try and see what you find easy to read. Here you will find alot of examples of ways to format a Date and Time: https://technet.microsoft.com/en-us/library/ee692801.aspx
Upvotes: 0
Reputation: 28963
You can't put colons in a filename on Windows:
Your date format is 2016-08-22T22:15:25.3693163+01:00
so it can't work as part of a filename.
Fix: there's no fix which keeps the name how as specified; you could -replace ':', '-'
, any character that makes sense to you.
Upvotes: 7