How to Create Filename with Date Time in Windows Batch Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys if I ask you you have to create a file name with the date time hour minutes seconds and milliseconds a file name with all the entire format and this will be practically used in many times when you have to create some log files so I'm going to teach you how you can create such files giving them the date time format name in an easy way I'm going to teach you in this video so let's get started okay so let's see the output your final output would appear something like this there are three different files and it has got the year then the month and the date are minute seconds in milliseconds so this is how the entire name has been given okay so how we will get this output yes we'll get to know first thing what you do you open your command prompt let me clear the screen open the command prompt and you need to be aware of two different commands one is the date command and the second thing you need to be aware is the time command okay so the date command gives you the date and look at the format how it is coming on your computer on your computer it could be slightly different than mine so for me it is Friday then there is a space and then there is there is a month then I have a date then year or it could be the same way and then if you look at the time there's the r the second one is the second one is your minute and then second and then millisecond so this is how it is coming so first have a look so once you have this data what I have done I have created a simple table for demonstration purpose you can draw it on your page or simply count it so the entire output that we have got on the top the first letter of the output will be there in the position number zero so if you write the position number zero it will fetch the f similar way I have given the position number to all the characters including the space if the space has a position which is 3 here okay so this positions will be using to provide that create that entire name same thing I have done it for the time let's move on to the bass script and let's get started and see how we can fetch those positions and write the output what we need Okay so let's get started so the first thing I will write is equal off and then I'm going to create a variable let's create a simple variable set a a b c d whatever you like so I'll take a and here what I need I will simply say date in percentage so if I say date percentage what is the output I would see if I write a CO percentage a to see the value and I will pause it here so what is the output you are going to get you'll simply get the date right so let me run this so you see what we have got we have got simply the date isn't it we have got that what if I only won the year because what is the output we want we want the first output to be here in the month so first we need the year out of that entire day now to get the year all I will do is put a colon Mark and then use a tilde symbol now the year where exactly the year is starting the year is starting from the position number 10 and how many characters does it have one two three four four characters so ten and four so all I will do I'll first mention the position number and number of characters I need is starting from 10 I need four four so what would be the output now if you save it and run it look at the output so the output is 2022. okay just to give it a little bit of a better color so that we can view it properly while I'm teaching I will simply put a color code color let's say e is for yellow okay all right so we got it now the same thing we will copied okay we'll just copy because we don't want any space for the file name I'll simply paste this after is what we need we need the month right here if you see I need the month after the year so where the month is starting from the month is starting from position number four and number of characters are two so I will simply say four and number of characters are 2 for the month then I need date so I'll simply again copy paste okay and then date is starting from position number seven it is pretty simple right I am simply writing the position numbers 7 and then number of characters I'm writing two pretty simple okay so did we get year month and date yes we have fetched all that we need if you need even the day day you can also fetch it but generally not required so let me save it and let's run this code and see are we getting the design output or not see 2022 1209 we are getting the desired date output now let's do the same thing for the time pretty simple let me close it let's do the same thing for the time now for the time let's write again let's create another variable set you can write a simple line code also okay but again for learning purpose this is how we can do it uh B and then uh for the time right so I will say time okay for the time also we'll do the same same process so for the time what is the first thing that you need the first thing that you need is the r which is starting from the zero position number zero and how many character it has two characters well comma two and percentage so I got it the r same thing let's repeat copy paste R after that I need the minute right so I'll get the position number three it starts from the question number three I will write three done quite simple quite easy and then we need six starting six and the third one that we need is millisecond which is starting from seven eight nine okay it's starting from nine in number of characters is 2. same thing we have repeated so let's see how this is coming okay Echo and then I will say percentage B percentage simple save it and let's quickly run this okay now let's see this how the output has come the first one is for the date year uh year month and date this is a month and the date then the second one is the this is the r and then this one is the minute seconds sorry seconds and then milliseconds got it so we got exact two things now what is the next thing we need we need to combine both of it and create a file name okay so how to do that let's see it so the very the next thing that we need to ask is where they would like to create that particular file okay so for that what I will do we'll use a set command Escape I'll use a prompt I need to prompt the user and let me store that value in the C enter the directory path enter directory path okay so here the C will carry the value of the directory now all we have to do we have to combine it so for that I will simply say make there or make directory and then I have to combine all of it so first thing I will use percentage C percentage and then I need the date I will simply say percentage a percentage then I need the time then I will say percentage C oh sorry it is in B right percentage B percentage so with that did we get the output no we have to put this backslash isn't it after the derivative you have to provide a backslash then this becomes the name so this this is how we have created the file name no doubt but there is a still a problem what is the problem sometimes your R will have a single digit now it is 12 o'clock so it will say 1 to the two digits but at 8 AM or 9 am it will have only single digit so a single digit mean the first letter there might be a space and that will create two two files when you run the same script so for that we have to trim the space okay to trim the space all you have to do write a colon mark and then give it a space because we have to trim the space give it a single space okay single space and equals to this is the final script okay so let's save it and see if there's any error we will try to correct so let's run the script so what we have got we got here this we have seen let's enter the directory path so I would like to create this entire file in this particular directory Ctrl V and hit on enter okay the moment I have hit on enter what would happen if you see is one more file got created at the bottom do you see there were three files one more file got created did we get the output yes we have got the output okay so based on your uh timing of your computer you can decide how that format should look like okay if you want the year first you want the month first or DD first based on that you can do it if you put the DD sometimes the DD will start with a single digit one two so in that you have to implement the same concept of trimming that space if there is a space coming okay through experiment you will get to know so this is the final script and you could see the output has been created for us there's another file got created with the date time format so thank you guys for watching let let's move on to our next screen stay tuned and continue to like our videos and once you have done with any topic please type completed thank you guys let's move on to our next topic bye
Info
Channel: Automate with Rakesh
Views: 8,188
Rating: undefined out of 5
Keywords: batch file, batch scripting, batch file command, batch file tutorial, create log file with date and time in batch script, batch script to create folder with date and time, batch script rename file with date and time, append date to filename windows batch, batch file date format dd/mm/yyyy, batch script date format, batch date format yyyymmddhhmmss, append date and time to filename windows
Id: 1GUCJKmJuAo
Channel Id: undefined
Length: 9min 25sec (565 seconds)
Published: Fri Dec 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.