| 1. |
Solve : 2 part question about date in a batch file? |
|
Answer» Part 1: Part 2: I doubt if this is achievable using only batch scripting, VBS might be better for this part, perhaps one of the VBS gurus will drop in. Meantime here's another version of batching to create the filename in the format you requested using the 24-hour clock (aka Military Time). The underlined command lines should be removed when you are finished testing. Your date format is assumed to be 'day mm/dd/yyyy' and the time format as 'hh:mm:ss.ms AM (or PM)', When the hour is less than 10 it's assumed it is shown as one digit. Code: [Select]Script removed. Edit: See amended script below....As a suggestion you could try extracting it with the Set command Code: [Select]@echo off set nummonth=%Date:~4,2% set day=%Date:~7,2% set year=%Date:~12,2% set hour=%Time:~2% set min=%Time:~3,2% if %hour% LSS 10 set hour=0%hour% for /f "tokens=%nummonth%" %%a in ('echo JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC') do ( set wordmonth=%%a ) Then for the ZIP paramater you can just put: Code: [Select]%day%%wordmonth%%year%_%hour%%min%.ZIP For part two, you would have to use a 3rd party program or perhaps Cscript. I'll get back with the Cscript instructions. Hope this helps ,Nick(macdad-)Thanks T.C. and macdad. T.C.'s method worked well. Macdad, your method left some extra spaces and inserted a : into the file name, so that was invalid. Thanks to everyone for answering.Quote from: JohnBergt on November 25, 2009, 05:08:29 PM T.C.'s method worked well. You SPOKE to soon John, there is a glaring error in my script. In the command line Code: [Select]for /f "tokens=%date:~4,2%" %%1 in ("%alphamonth%") do ( set month=%%1 ) if the month is 08 or 09 (Aug or Sept) tokens will FAIL probably due to 08 and 09 being invalid octal numbers. Amended script posted below, sorry about the error. Code: [Select]@echo off cls setlocal enabledelayedexpansion set date=fri 09.26.2009 :: -------------------- set alphamonth=JAN FEB MAR APL MAY JUN JUL AUG SEP OCT NOV DEC if %date:~4,2% lss 10 (set monthnbr=%date:~5,1% ) else ( set monthnbr=%date:~4,2% ) for /f "tokens=%monthnbr%" %%1 in ("%alphamonth%") do ( set month=%%1 ) set today=%date:~7,2%%month%%date:~-2% set time=1:12:13.14 am :: ------------------- for /f "tokens=1-4 delims=: " %%1 in ("%time%") do ( set hour=%%1 set min=%%2 if /i %%4 equ pm if %%1 lss 12 set /a Hour +=12 ) if %hour% lss 10 set hour=0%hour% set time=%hour%%min% echo.&echo. echo Today=%today% Time=%time% echo.&echo. echo Filename=%today%_%time%.zip Script edited to allow for correct display of times between 12.00 (noon) and 1.00 pm date manipulation has been discussed countless times. Don't use batch for date manipulations. try learning and using vbscript if you are a native guy, or get a real programming language, Perl/Python are the ones you need.Quote from: gh0std0g74 on November 26, 2009, 02:53:27 AM date manipulation has been discussed countless times. Don't use batch for date manipulations. try learning and using vbscript if you are a native guy, or get a real programming language, Perl/Python are the ones you need. I believe this vbscript should do the job.Quote from: gh0std0g74 on November 26, 2009, 02:53:27 AM Don't use batch for date manipulations. try learning and using vbscript Sorry, I cannot agree with that. While it is a good idea to learn VBScript etc., when a member asks for a solution to a problem using batch scripting he/she should be assisted to achieve that solution in the scripting language of his/her choice not told to go off and learn another language with which he/she may have no experience. Certainly batch scripting is very limited in some areas, and date manipulation is one of them, but if the request is for assistance in a batch script that assistance should be forthcoming where it is possible. Then, and only then, should the suggestion be made to expand into VBS etc...Quote from: T.C. on November 26, 2009, 12:04:51 PM Batch Works. Code: [Select]@echo off setlocal enabledelayedexpansion date /t > sdate.txt set /p sdate=<sdate.txt echo sdate=!sdate! set alphamonth=JAN FEB MAR APL MAY JUN JUL AUG SEP OCT NOV DEC set tokens=%sdate:~4,2% echo tokens=!tokens! for /f "tokens=%date:~4,2%" %%a in ("%alphamonth%") do ( set month=%%a echo month=!month! ) set today=%date:~7,2%%month%%date:~-2% time /t > stime.txt set /p stime=<stime.txt echo stime=!stime! for /f "tokens=1-4 delims=: " %%a in ("%stime%") do ( set hour=%%a set min=%%b set pmm=%%c echo hour=!hour! echo min=!min! echo PM=!pmm! if !pmm! equ PM set /a hour=!hour! + 12 echo hour = !hour! ) if %hour% lss 10 set hour=0%hour% set time=%hour%%min% echo.&echo. echo Today=%today% Time=%time% echo.&echo. echo Filename=%today%_%time%.zip Output: C:\>fdate.bat sdate=Wed 11/25/2009 tokens=11 month=NOV stime=11:40 PM hour=11 min=40 PM=PM hour = 23 Today=25NOV09 Time=2340 Filename=25NOV09_2340.zip Output for 11/26/2009: C:\test>joan.bat sdate=Thu 11/26/2009 tokens=11 month=NOV stime=04:15 PM hour=04 min=15 PM=PM hour = 16 Today=26NOV09 Time=1615 Filename=26NOV09_1615.zip C:\test> Quote from: T.C. on November 26, 2009, 12:04:51 PM Sorry, I cannot agree with that.you do not agree because i said "Don't use batch for date manipulation"? or you don't agree that i asked OP to learn and use something else that is better at doing this job? Quote While it is a good idea to learn VBScript etc., when a member asks for a solution to a problem using batch scripting he/she should be assisted to achieve that solution in the scripting language of his/her choice not told to go off and learn another language with which he/she may have no experience.you must understand that this is a forum, and we are allowed to suggest BETTER and EASIER alternatives to SOLVE the problem. What really matters is OP's choice of solution, not yours or mine. No experience in learning something he did not know?? If OP has experience in batch, he wouldn't have asked here already. so what's your point? Quote Certainly batch scripting is very limited in some areas, and date manipulation is one of them, but if the request is for assistance in a batch script that assistance should be forthcoming where it is possible. Then, and only then, should the suggestion be made to expand into VBS etc...as far as i already know, you have provided a batch (that may not work in every machine due to individual date settings). I/macdad have provided suggestion using vbscript and all that's left is up to OP. A challenge for you, try producing a batch to convert local to UTC for OP then, if you really meant what you say.Quote from: GD A challenge for you, try producing a batch to convert local to UTC for OP then, if you really meant what you say. GD - You obviously did not read my first response to JohnBergt, did I not state Quote Quote from: JohnBergtPart 2: One of the VBS gurus did drop in but unfortunately didn't leave a solution. Quote from: GD as far as i already know, you have provided a batch (that may not work in every machine due to individual date settings). Quite correct, it will not work for date settings other than the ones I stipulated in my first response: Quote Your date format is assumed to be 'day mm/dd/yyyy' and the time format as 'hh:mm:ss.ms AM (or PM)', When the hour is less than 10 it's assumed it is shown as one digit. How can I make that clearer? I have no wish to become involved in a drawn out argument about the merits of Batch Scripting vs VBS, I also believe VBS is superior in many ways than Batch but still believe that if a solution to a problem is asked for in batch scripting and that solution can be arrived at then it should be given. Kind regards to all. T.C. Quote from: T.C. on November 26, 2009, 08:24:33 PM GD - You obviously did not read my first response to JohnBergt, did I not statedon't worry, i did read your post before posting my last response. Quote but still believe that if a solution to a problem is asked for in batch scripting and that solution can be arrived at then it should be given.so is there a clean and understandable batch(cmd.exe) solution given yet to take into account, different regional date settings on different machines, and the ability to convert to UTC time easily?? Your reasoning that batch solution should be given is flawed. most of the time, the ones posting questions here either did not know there are better alternatives, or they just have the mentality that says batch does everything. What's wrong with posting better alternatives that do the job in half the time it takes to do it in batch? you are beginning to sound that the other "hated" person around here. I suggest you focus more on providing solution to the OP and stop meddling with trivial things like this. Quote from: JohnBergt on November 25, 2009, 09:04:59 AM
http://www.timeanddate.com/worldclock/converted.html?month=11&day=29&year=2009&hour=0&min=0&sec=0&p1=0&p2=184 The World Clock – Time Zone Converter – results At the specified time, local time in Oklahoma City was 6 hours behind UTC LocationLocal timeTime zone UTCSunday, November 29, 2009 at 00:00:00 Oklahoma City (U.S.A. - Oklahoma)Saturday, November 28, 2009 at 6:00:00 PMUTC-6 hours CST It appears Batch is the best solution for the first part of your question. See Post # 9 UTC is a minor problemthen , Miss genius, can you show a complete batch solution to do both part 1 and 2? |
|