• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

Date change using Batch Files

I am running monthly queries and exporting the results as .csv files.  I am trying to create a batch file that changes the file name by adding the current month and year at the end of the file name and then moving the file to the appropriate folder.  (ex. NST_ALMachineryException.csv= NST_ALMachineryException_Feb08.csv).  
  • 2
1 Solution
Lee W, MVPTechnology and Business Process AdvisorCommented:
I would recommend changing your datecode a little for ease of coding and sorting...

In the batch file, use:

REN NST_ALMachineryException.csv NST_ALMachineryException_%date:~-4%-%date:~4,2%.csv

that will result in a file name of NST_ALMachineryException_2008-02.csv  (Year first, then Month)

If you insist on keeping it month first than year, then you will need to use:
REN NST_ALMachineryException.csv NST_ALMachineryException_%date:~4,2%-%date:~-4%.csv

If you insist on keeping the month name, then you will need to create a series of IF statements...

IF "%date:~4,2%"="01" set MMM=Jan
IF "%date:~4,2%"="02" set MMM=Feb
IF "%date:~4,2%"="03" set MMM=Mar
IF "%date:~4,2%"="04" set MMM=Apr
IF "%date:~4,2%"="05" set MMM=May
IF "%date:~4,2%"="06" set MMM=Jun
IF "%date:~4,2%"="07" set MMM=Jul
IF "%date:~4,2%"="08" set MMM=Aug
IF "%date:~4,2%"="09" set MMM=Sep
IF "%date:~4,2%"="10" set MMM=Oct
IF "%date:~4,2%"="11" set MMM=Nov
IF "%date:~4,2%"="12" set MMM=Dec
REN NST_ALMachineryException.csv NST_ALMachineryException_%MMM%-%date:~-2%.csv
MarkRJacksonAuthor Commented:
Thanks for the reply.  One thing that was missing is an extra "=" in the IF commands.
Lee W, MVPTechnology and Business Process AdvisorCommented:
Your right - I was typing in a mix of VB and batch... sorry about that.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now