-2

I am trying to get today's date in DD-MMM-YYYY format (for example, 12-JUN-2015) so that I can download a file from an FTP server that will be created daily.

i m using

@echo off
set month-num=%date:~3,2%
IF "%month-num:~0,1%"=="0" SET month-num=%month-num:~1%
FOR /f "tokens=%month-num%" %%a in ("jan feb mar apr may jun jul aug sep oct nov dec") do set mo-name=%%a
echo %mo-name%

it is giving syntax error

I would appreciate any advice, as I have no idea where to begin.

M.javid
  • 5,373
  • 3
  • 36
  • 52
Ganesh Konka
  • 31
  • 1
  • 1
  • Fine, nice to know it! – pavel Jun 15 '15 at 06:48
  • Which OS are you working on? Is it windows where you are gona have the batch files? –  Jun 15 '15 at 06:51
  • check this - http://stackoverflow.com/a/19799236/388389 – npocmaka Jun 15 '15 at 07:36
  • 1
    i m using @echo off set month-num=%date:~3,2% IF "%month-num:~0,1%"=="0" SET month-num=%month-num:~1% FOR /f "tokens=%month-num%" %%a in ("jan feb mar apr may jun jul aug sep oct nov dec") do set mo-name=%%a echo %mo-name% it is giving syntax error please help – Ganesh Konka Jun 15 '15 at 10:00
  • Please edit your question to include the code you are using; code is not properly displayed in comments. – SomethingDark Jun 15 '15 at 10:09
  • hi..there is no scrip related to 12-JUN-2015..if you please give me ... – Ganesh Konka Jun 15 '15 at 10:28
  • Stack Overflow is not a code writing service. Please edit your question to include the code you have tried and we will tell you why it isn't working. – SomethingDark Jun 15 '15 at 11:57
  • The down mods and votes to close are inappropriate for a post that's fixable. Obviously, OP is a rookie, and could use a bit of friendly guidance. He posted his code in a comment, which was a rookie mistake; yet instead of anyone helping him by editing his question and making it useful, several people continued the modding into oblivion. This is unproductive and contrary to the spirit of S.O. Have some patience with new users who have demonstrated a willingness to show their effort, please. – rojo Jun 15 '15 at 14:33
  • @GaneshKonka If my answer below was helpful, please consider marking it as accepted. [See this page](http://meta.stackexchange.com/questions/5234/) for an explanation of why this is important. I apologize for the hostile welcome you have received thus far. Nevertheless, welcome to Stack Overflow! – rojo Jun 15 '15 at 14:36
  • @rojo - there were five downvotes on this question before I completely rewrote it to prevent further downvotes. My first comment was so that he wouldn't make the same mistake in the future, and my second was a reaction to "please give me the script," again, to prevent future mistakes of this nature. – SomethingDark Jun 16 '15 at 08:39

2 Answers2

3

Ganesh, your use of a for /f loop to convert the numeric month to its abbreviation is pretty clever. I think part of your problem is that whoever originally wrote the code you're using wrote it for their locale, not yours. %date% looks different to the original author than it looks to you.

The format of %date% is unpredictable (well, without complicated registry queries and conversions anyway). To obtain datetime in a locale agnostic way, it's easier to use wmic os get localdatetime.

Try this:

@echo off
setlocal

for /f %%I in ('wmic os get localdatetime /format:list ^| find "="') do set "%%I"
set "YYYY=%localdatetime:~0,4%"
set /a "MM=1%localdatetime:~4,2% - 100"
set "DD=%localdatetime:~6,2%"
for /f "tokens=%MM%" %%I in ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") do set "month=%%I"

echo %DD%-%month%-%YYYY%
rojo
  • 22,626
  • 5
  • 47
  • 93
0

Normally reports will be asked for previous month. So to get previous month, here is a little modification:

@echo off
setlocal

for /f %%I in ('wmic os get localdatetime /format:list ^| find "="') do set "%%I"
set "YYYY=%localdatetime:~0,4%"
set /a "MM=1%localdatetime:~4,2% - 101"
set "DD=%localdatetime:~6,2%"
for /f "tokens=%MM%" %%I in ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") do set "month=%%I"

echo %month%-%YYYY%

Now passing this mmm-yyyy to your EXE will make it run for that month.

colidyre
  • 3,035
  • 11
  • 30
  • 43
  • This is confusing why post a solution for a different question. I missed that this is actually returning the PREVIOUS month. Look at Rojos solution for current month as per the question. – PST Jul 04 '20 at 13:09