Jump to content

Batch loop?


Recommended Posts

Using the Command Prompt (CMD.EXE) in Win XP Home SP2. Date format is Day mm/dd/yyyy

I want to be able to display the name of the current month using a batch file. The relevant part of the file is:

@echo off

cls

set datemonth=%date:~4,2%

if %datemonth%==11 set datename=Nov

echo %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


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 by Yzöwl
code removed
Link to comment
Share on other sites

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

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

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 by RatPack
Link to comment
Share on other sites

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

Nice tip.

Is there a web page that discusses these type of features in depth?

Thanks,

-John

The "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.html

http://www.robvanderwoude.com/ntset.html

http://www.robvanderwoude.com/local.html

the SS64 help for windows commands:

http://www.ss64.com/nt/

jaclaz

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...