{"id":54713,"date":"2008-12-31T11:44:00","date_gmt":"2008-12-31T11:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/12\/31\/hey-scripting-guy-how-can-i-create-file-names-based-on-their-time-stamp\/"},"modified":"2008-12-31T11:44:00","modified_gmt":"2008-12-31T11:44:00","slug":"hey-scripting-guy-how-can-i-create-file-names-based-on-their-time-stamp","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-file-names-based-on-their-time-stamp\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create File Names Based on Their Time Stamp?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/> <\/h2>\n<p>Hey, Scripting Guy! How can I create a file name from the date? I have this application that creates a log file, and every hour I would like to save that log file as a file whose name is generated from the current time stamp. Can this be done?<\/p>\n<p>&#8211; RH<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/> <\/p>\n<p>Hi RH,<\/p>\n<p>Ever since I read your e-mail message, I have had this <a href=\"http:\/\/en.wikipedia.org\/wiki\/Time_Is_on_My_Side\" target=\"_blank\">old Rolling Stones tune<\/a> stuck in my head (which is much better than having &#8220;I&#8217;ve got a brand new pair of roller skates\u2026&#8221;), and it is causing some serious <a href=\"http:\/\/news.bbc.co.uk\/2\/hi\/health\/4332771.stm\" target=\"_blank\">brain itch<\/a>. So I was back in Munich, Germany, a couple of years ago, and the Stones were on stage. It was a fine spring evening, ink-black sky, full moon, and not a cloud in the sky. The lights went off and two flame pots erupted. The crowd went wild and\u2026sorry.<\/p>\n<p>The main logic of today\u2019s script is contained in a function we have called <b>GetFileName<\/b>. Inside this function we use a couple of pretty cool methods to create the file name for you. Here\u2019s the sscript:<\/p>\n<pre class=\"codeSample\">Function GetFileName([ref]$fileName)\n{\n $invalidChars = [io.path]::GetInvalidFileNamechars() \n $date = Get-Date -format s\n $fileName.value = ($date.ToString() -replace \"[$invalidChars]\",\"-\") + \".txt\"\n}\n\n$fileName = $null\nGetFileName([ref]$fileName)\nnew-item -path c:\\fso -name $filename -itemtype file\n<\/pre>\n<p>The first thing we do is create a function named <b>GetFileName<\/b>. To do this we use the <b>function<\/b> keyword, and give it the name of the function. We are going to pass a variable to the function by reference. VBScript functions are talked about <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_sbp_cxzm.mspx?mfr=true\" target=\"_blank\">here<\/a>. When we pass a variable to a function by reference, it means we will be changing the value of the variable inside the function, and then handing that value back to the code that called the function. <\/p>\n<p>Another way to call a function is to pass a variable by value. When that happens, the value of the variable is what is important. Inside the function the value of the variable may change, but when the script is done, the value of the variable remains unchanged. This is seen here in this demo script:<\/p>\n<pre class=\"codeSample\">Function addOne($in)\n{\n $in = $in + 1\n 'In the function $in equals ' + $in \n}\n$in = 1\naddOne($in)\n'Out of the function $in equals ' + $in\n<\/pre>\n<p>Back to the <b>GetFileName<\/b> function\u2014when you want to pass a variable by reference, you need to use the <b>[ref]<\/b> type constraint to make the variable into a reference type. This is seen here. <\/p>\n<pre class=\"codeSample\">Function GetFileName([ref]$fileName)<\/pre>\n<p>Next we need to get a list of all the things you are not allowed to have in a file name. Dude! You mean you cannot remember all the things you are not allowed to have in a file name? (Uh, join the club.) I know some of the things, and I know some things that I do not like in file names (such as spaces), but that is about it. I generally stick with letters and numbers for my file names. But suppose you needed to know exactly what you could not have in a file name; how would you do that? Well it is easy. You can use the <b>GetInvalidFileNameChars<\/b> method from the <b>System.Io.Path<\/b> .NET Framework class. If you would like to see a list, copy this code into the Windows PowerShell command window, as seen here:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of a listing of invalid file name characters\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1231\/hsg_ff3_01.jpg\" width=\"500\" height=\"319\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>In our script, we store the invalid file name characters into an array named <b>$invalidChars<\/b>. This is shown here:<\/p>\n<pre class=\"codeSample\">$invalidChars = [io.path]::GetInvalidFileNamechars()<\/pre>\n<p>Now we want to get the current date and time. We use a .NET Framework standard format string. These are documented on <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/az4se3k1.aspx\" target=\"_blank\">MSDN<\/a>. Using the standard format strings is very easy. You use the <b>\u2013format<\/b> parameter, and then include the format in which you want the date to be displayed. An example of the use of some of these format strings is seen here:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of different date formats\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1231\/hsg_ff3_02.jpg\" width=\"500\" height=\"380\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>In our script, we want to store the date in the sortable fashion. A sortable date places the year, month, and day first, separated by the letter <b>T<\/b>, and then followed by the hour, minute, and second. This is seen here: <\/p>\n<pre class=\"codeSample\">$date = Get-Date -format s<\/pre>\n<p>Next we need to get rid of the invalid characters that are not permitted in a file name. To do this, we use the invalid characters we stored in the <b>$invalidChars<\/b> and feed it as a regular expression pattern to the <b>replace<\/b> operator. The second parameter of the <b>replace<\/b> operator is what we are going to replace it with. We use the dash (&#8220;\u2013&#8221;) for all the invalid characters. We also append <b>.txt<\/b> to the end of our file name, and assign it to the value of the <b>$filename<\/b> reference variable. This is seen here:<\/p>\n<pre class=\"codeSample\">$fileName.value = ($date.ToString() -replace \"[$invalidChars]\",\"-\") + \".txt\"<\/pre>\n<p>Now we are outside of the function. To use a variable as a reference, it must first be present. In Windows PowerShell we do not need to use the <b>DIM<\/b> command to declare a variable. In fact if we want to create a variable, we just assign a value to it. This is seen here: <\/p>\n<pre class=\"codeSample\">$fileName = $null<\/pre>\n<p>We now want to call the <b>GetFileName<\/b> function. To do this, we pass the <b>$fileName<\/b> variable to it. Note that the <b>[ref]<\/b> type is required. This is seen here: <\/p>\n<pre class=\"codeSample\">GetFileName([ref]$fileName)<\/pre>\n<p>Finally we use the <b>new-item<\/b> cmdlet to create a new file. We specify the path to the path parameter, and the name we created in the <b>$filename<\/b> variable. To create a file, we specify the item type as <b>file<\/b>. This is seen here:<\/p>\n<pre class=\"codeSample\">new-item -path c:\\fso -name $filename -itemtype file<\/pre>\n<p>When the script is run, it creates an empty file in the <b>fso<\/b> folder. I ran the script several times (I am easily entertained). The results are seen here:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of the results of running the script multiple times\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1231\/hsg_ff3_03.jpg\" width=\"500\" height=\"434\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>Well, RH, I hope you found this article useful. We have explored creating file names, date types, format strings, and all kinds of other cool stuff. Happy New Year! May 2009 be one of peace for you and for the planet.<\/p>\n<p><font class=\"Apple-style-span\" size=\"3\" face=\"Verdana\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" size=\"3\" face=\"Verdana\"><b><\/b><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a file name from the date? I have this application that creates a log file, and every hour I would like to save that log file as a file whose name is generated from the current time stamp. Can this be done? &#8211; RH Hi RH, Ever since [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,38,3,4,12,21,45],"class_list":["post-54713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a file name from the date? I have this application that creates a log file, and every hour I would like to save that log file as a file whose name is generated from the current time stamp. Can this be done? &#8211; RH Hi RH, Ever since [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=54713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}