I don't need or care about the hundredths of a second. So in the above line, the data was taken on the 109th day of 2010, at 7:57:15am. You can easily extend this solution to virtually any number of leading zeros. The first element is ignored, elements 2-5 are yyyy, dddd (Julian, leading zeros redacted), hhmm (military time, leading zeros redacted), ss.ss. A more general solution is to add a string of n zeros with your desired length, and then use a substring operation to keep the n right most digits. If %%N lss 10 (set HOSTNAMETOUSE=prefix-0%%N) else set HOSTNAMETOUSE=prefix-%%Nīut that is not convenient if your number width is more than 2 digits. Since you never need more than a single 0, you could use an IF statement. You must use delayed expansion to get the value upon execution time. So the expanded value is constant for all iterations. I’m hoping someone finds this useful, so I know I gave back.Your posted code cannot possibly work because %HOSTNAMETOUSE% is expanded when the statement is parsed, and the entire parenthesized block is parsed in one pass, before the loop is executed. Set yesterdaysstamp=%month%/%TODAYminus1%/%year%,%hourCivilian%:%minute%:%second%%AMPM%Įventquery /l application /v /fo list /fi “Datetime gt %yesterdaysstamp%” > d:\?tetimestamp%_last24hourslogs.txt ::::: then do something practical, like query for the last 24 hours of event logs ![]() ::::: demonstrate math abilities of “set /a” by getting yesterday’s number Set datetimestamp=%year%%month%?y%%hour%%minute%%second% ::::: identify Name of the day and provide short date for todayįor /f “tokens=1-2*” %%v in (‘date /t’) do ( If “%AMPM%” = “AM” set hourMilitary=%hourCivilian% ::::: identify hours in Civilian time, and identify AM or PMįor /f “tokens=1-2*” %%t in (‘time /t’) do ( Here are how I break down my variables (not all of them are used in a given script): I think it makes it harder visually and in script to parse. ![]() Thanks you for the article! Here’s my take… I like double digit hours, so the trim doesn’t work for me. I am still amazed this little trick works. The same method could be used for the current time I get a date stamped file, and no special vbscript, or command line program is needed. ![]() Which renames our C:\TempZip.Zip to C:\TempZip_20070215.zip Pkzip c:\ImportantFolder\*.* c:\TempZip.zip Lets say I zipped up a folder every night for archival purposes, and wanted a different filename for each day (Not sure if this pkzip syntax is correct, but that is not important for our discussion here) Like this:įor the month we start 7 from the right (Length of Year Length of Month 1 Slash)įor the day we start 10 from the right (Length of Year Length of Month Length Of Day 2 Slashes)īringing it all together. If we wanted to get the current year we could start 4 from the end, and 4 in length. I know this might be confusing at first, but you will see what I am talking about. And is the number of characters in the opposite direction of the starting point. If the number is negative it will start from the right. If is positive or zero the substring will start from the left. The :~ substring command works like this: To be safe we can pull the year, month and day starting from the right. Not sure if the length of the day changes. Lets take the ?te% variable and print it out That is when I realized you could use this to pull out parts of the current date (or time). What I didn’t know that was that you can use this character combination ‘:~’ to pull a substring out of an environment variable. The problem with that is the utility needs to be around if you want to send the batch file to someone. I found a way to take the ?te% environment variable, and turn it into a valid string for a filename – without any extra programs or scripts.įor the longest time I used a little utility I created to do this.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |