November 11, 200718 yr Using the Command Prompt (CMD.EXE) in Win XP Home SP2. Date format is Day mm/dd/yyyyI want to be able to display the name of the current month using a batch file. The relevant part of the file is:@echo offclsset datemonth=%date:~4,2%if %datemonth%==11 set datename=Novecho %datemonth% %datename%But do I need 12 'if' statements to handle each of the 12 months or is there a better way with a loop or some kind of array?Thanks
November 12, 200718 yr Author Thank you for your speedy response.Abject apologies for posting in the wrong forum.I cannot get your coding to run, the first line gives me the error "Invalid number. Number constants are either decimal (17) hexadecimal (0x11) or octal (021)."At my knowledge level I can't figure out what the problem is or, obviously, how to correct it. However I have been able to slightly modify your second line in order to get the file to run. Regrettably I function on the KISS principle. Edited November 12, 200718 yr by Yzöwl code removed
November 12, 200718 yr Author With regard to the error message I found this on another site.You're using "set /a" which will mean any time you try to set a value of "08", or "09" then an error will be thrown, such as this:Invalid number. Numeric constants are either decimal (17),hexadecimal (0x11), or octal (021).This is because "set /a" will treat any number with a leading 0 as octal, which it will attempt to store as decimal. Obviously "08" and "09" are not value numbers in octal, hence the above error.Hope it's some help.
November 12, 200718 yr I don't know why you are getting an error message.You stated that you are getting the output mm/dd/yyyy. The code parses your date to give you the first two tokens, using your stated / as a delimiter that means it would set %? as mm i.e. 11 which as you know doesn't have a leading 0.However, as you will note by my code, to prevent an error of the type you are getting I have already built in a fix. When your run the code in March, the %? variable would be 03. In order to remove any leading 0, I prefix a 1 thus making it 103 then subtract 100 making it read 3! SET/A would therefore never see a figure with a leading 0.
November 12, 200718 yr Author Thank you again, things are a bit clearer now.I stated that the output I'm getting is Day mm/dd/yyyy so the 12th November would show as "Mon 11/12/2007" without the double quotes. Could it be that %? is being set to "Mon 11" ? Edited November 12, 200718 yr by RatPack
November 12, 200718 yr Yes it would make a difference! I didn't see 'Day' in your original post. This would probably work better in that case.day_mm2mmm.cmd
November 15, 200718 yr set datemonth=%date:~4,2%I did not know that you could extract part of an environment variable with :~x,y. This will be really useful. Also, I did not know about %date% nor %time%, either.For example, I noticed that echo %logonserver:~2% returns the server name, without the two backslashes.Very interesting!-John
November 15, 200718 yr set datemonth=%date:~4,2%I noticed that echo %logonserver:~2% returns the server name, without the two backslashes.Also bear in mind that so does this!echo %logonserver:\=%
November 15, 200718 yr Nice tip.Is there a web page that discusses these type of features in depth?Thanks,-John
November 16, 200718 yr Nice tip.Is there a web page that discusses these type of features in depth?Thanks,-JohnThe "base" references are usually:Rob van der Woude site:http://www.robvanderwoude.com/The part about variable expansion/substitution is on the pages related to SET and FOR, check also the SETLOCAL one. http://www.robvanderwoude.com/variableexpansion.htmlhttp://www.robvanderwoude.com/ntset.htmlhttp://www.robvanderwoude.com/local.htmlthe SS64 help for windows commands:http://www.ss64.com/nt/jaclaz
Create an account or sign in to comment