RatPack Posted November 11, 2007 Share Posted November 11, 2007 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 Link to comment Share on other sites More sharing options...
Yzöwl Posted November 11, 2007 Share Posted November 11, 2007 This should help:mmtommm.cmd Link to comment Share on other sites More sharing options...
RatPack Posted November 12, 2007 Author Share Posted November 12, 2007 (edited) 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, 2007 by Yzöwl code removed Link to comment Share on other sites More sharing options...
RatPack Posted November 12, 2007 Author Share Posted November 12, 2007 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. Link to comment Share on other sites More sharing options...
Yzöwl Posted November 12, 2007 Share Posted November 12, 2007 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. Link to comment Share on other sites More sharing options...
RatPack Posted November 12, 2007 Author Share Posted November 12, 2007 (edited) 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, 2007 by RatPack Link to comment Share on other sites More sharing options...
Yzöwl Posted November 12, 2007 Share Posted November 12, 2007 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 Link to comment Share on other sites More sharing options...
RatPack Posted November 12, 2007 Author Share Posted November 12, 2007 Thank you, all is clear now.Your help is much appreciated. Link to comment Share on other sites More sharing options...
jftuga Posted November 15, 2007 Share Posted November 15, 2007 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 Link to comment Share on other sites More sharing options...
Yzöwl Posted November 15, 2007 Share Posted November 15, 2007 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:\=% Link to comment Share on other sites More sharing options...
jftuga Posted November 15, 2007 Share Posted November 15, 2007 Nice tip.Is there a web page that discusses these type of features in depth?Thanks,-John Link to comment Share on other sites More sharing options...
jaclaz Posted November 16, 2007 Share Posted November 16, 2007 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now