1. Posts/

Windows batch script to create directory auto file name DDMONYYYY with the help of javascript

···
Batch Script Windows XP

In the previous I had shown a windows batch script for creating directories with name of format DDMMYYYY. In this post I will show you how to create directories of format DDMONYYYY. I did a lot of research on web to figure this out. Here is the script, it looks a little bit complex.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
:: Auto directory date batch (DDMONYYYY format)
:: In Win32, use WSH/JS to create directory in format DDMONYYYY,
:: using temporary javascript and batch files.
:: JS file is used for for getting MON
:: @author Deepu Mohan Puthrote https://deepumohan.com
@echo off
:: Declare function fnMonthLookUp
echo function fnMonthLookUp () {} > tmpDate.js
:: Create array on fnMonthLookUp
echo fnMonthLookUp['1'] = 'JAN'; >> tmpDate.js
echo fnMonthLookUp['2'] = 'FEB'; >> tmpDate.js
echo fnMonthLookUp['3'] = 'MAR'; >> tmpDate.js
echo fnMonthLookUp['4'] = 'APR'; >> tmpDate.js
echo fnMonthLookUp['5'] = 'MAY'; >> tmpDate.js
echo fnMonthLookUp['6'] = 'JUN'; >> tmpDate.js
echo fnMonthLookUp['7'] = 'JUL'; >> tmpDate.js
echo fnMonthLookUp['8'] = 'AUG'; >> tmpDate.js
echo fnMonthLookUp['9'] = 'SEP'; >> tmpDate.js
echo fnMonthLookUp['10'] = 'OCT'; >> tmpDate.js
echo fnMonthLookUp['11'] = 'NOV'; >> tmpDate.js
echo fnMonthLookUp['12'] = 'DEC'; >> tmpDate.js
:: Get Date
echo var D = new Date(); >> tmpDate.js
echo var dd = D.getDate(); >> tmpDate.js
echo var mm = D.getMonth()+1; >> tmpDate.js
echo var yyyy = D.getFullYear(); >> tmpDate.js
echo var mon = fnMonthLookUp[mm]; >> tmpDate.js
echo var date = dd + mon + yyyy; >> tmpDate.js
echo WScript.Echo('mkdir '+date); >>tmpDate.js
cscript //nologo tmpDate.js > tmpDate.bat
del tmpDate.js
call tmpDate
del tmpDate.bat

Copy and paste this code into a file with extension bat and save it into a path where you want to create the directory. Please do share your comments.

You can also download the batch file from here. You can safely download it, it is hosted is safe server of HostGator.

The above script creates a tempDate.js and tempDate.bat file. Windows can run javascript file using the command cscript . Output of this javascript file is taken to tmpDate.bat file.

A sample javascript file generated by the above code is given below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function fnMonthLookUp () {}
fnMonthLookUp['1'] = 'JAN';
fnMonthLookUp['2'] = 'FEB';
fnMonthLookUp['3'] = 'MAR';
fnMonthLookUp['4'] = 'APR';
fnMonthLookUp['5'] = 'MAY';
fnMonthLookUp['6'] = 'JUN';
fnMonthLookUp['7'] = 'JUL';
fnMonthLookUp['8'] = 'AUG';
fnMonthLookUp['9'] = 'SEP';
fnMonthLookUp['10'] = 'OCT';
fnMonthLookUp['11'] = 'NOV';
fnMonthLookUp['12'] = 'DEC';
var D = new Date();
var dd = D.getDate();
var mm = D.getMonth()+1;
var yyyy = D.getFullYear();
var mon = fnMonthLookUp[mm];
var date = dd + mon + yyyy;
WScript.Echo('mkdir '+date);

As you can see a javascript array is created that holds index for each month. Next we create a new Date object using javascript function new Date(). From the date object get the date, month and year. Next we get the MON format of month from the javascript array already created. Now in date variable we have the date in the required format. WScript.Echo() command will give the output. And the output will be mkdir DDMONYYYY. This output is taken into the tmpDate.bat file. Then the tmpDate.js file is deleted and tmpDate.bat file is executed using the call command.

In the next post I shall give an improved version.

For those who are new to windows batch files, visit this site by Microsoft.