Question Locked

Create a folder with current date

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have an old computer with Windows 98SE and I'd like to get a help on how to create a folder with the current date, "08-26-2008" for example (without the quotes), using a script.

I have tried

md %date:~4,2%-%date:~7,2%-%date:~10,4%

It works on Windows XP but not on Windows 98.

Thanks in advance.

6 replies

by bizzo

A small batch file which will do what your require:

NB. You may need to swap over DateDay and DateMonth, depending on your date format.

------------------------------------------

for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)

set CurrentDate=%DateMonth%-%DateDay%-%DateYear%

md %CurrentDate%

by bcpgm

It worked on Windows XP but not on 98.

by Former member

Please read this introduction if you are unfamiliar with batch files. The batch file is listed after this introduction. (Experienced DOS users can skip this section.)

Batch files are texts that contain instructions for the MS-DOS command processor. They are texts that contain the same kind of commands that you could type on the DOS command line, either in a DOS window while running Windows or when the PC is running only DOS.
Create a batch file using Notepad or any other editor (or, less suitably, a word processor) with word-wrap turned off. Save the file as a text using the name listed above in big type. Make sure the file has a .BAT filename extension, like this: MYFILE.BAT. You can cut and paste from your browser window to create the batch file listed here.
You run a batch file by typing the first part of its name from a DOS command line or from another batch file. To run MYFILE.BAT, you'd type "MYFILE" at the command line, or you'd run it from a batch file with a line that says the same thing. You should make sure that all your batch files are located in the path that MS-DOS uses. This could be "C:\Windows\Command" under Windows 95 or "C:\DOS" under Windows 3.1 and 3.11, or it could be a special folder (called "BAT," maybe) somewhere else. If you use a special folder, make sure the location of that folder is part of the path statement in your PC's AUTOEXEC.BAT. (Note well: If you have Windows 95, you do not need to do anything fancy; just put the batch folder's name in the AUTOEXEC.BAT this way:
SET PATH=C:\BAT
and leave out the "C:\Windows; C:\Windows\Command" stuff entirely. Windows 95 ALWAYS puts its own folders in the path.)

********** BATCH FILE BEGINS HERE. **********

@echo off
if %4()==() goto top
goto routines

current.bat
Al Fasoldt 2-22-98

With appreciation to Ronny Richardson
for the original idea of using the DOS date command
to create a temporary batch file.

(Please do not put REMs in front of any lines.)

NOTE: This is not your usual batch file.
It creates a temporary batch file, then runs it.
That batch file then runs this batch file.
I like recursiveness (I like recursiveness).

This MUST be called CURRENT.BAT or it will not run.

If you run this from a floppy disk, it will go REAL slow.
Run it from your hard drive.

---------------------------------------------
DO NOT RUN THIS FROM THE AUTOEXEC.BAT file!
---------------------------------------------

IT MUST RUN FROM WITHIN A WINDOWS 95 DOS WINDOW.
IT WILL DESTROY ALL LONG FILENAMES FOR ANY SETUP PROGRAMS
THAT ARE TEMPORARILY USING THE TEMP FOLDER
IF IT IS RUN FROM THE AUTOEXEC.BAT.

:top
echo. | date | find "Current ">storedat.bat
storedat
goto routines

EXPLANATION OF THE CODE ABOVE
=============================

When CURRENT.BAT is run, it looks for a fourth parameter.
If it can't find one, it creates STOREDAT.BAT and then
runs the batch file it just created. This guarantess
that CURRENT.BAT will be rerun with the proper date params.

STOREDAT.BAT consists of one line,
created by the DOS date function.
It looks like this:
Current date is Wed 02-18-1998

Since there is an executable file called CURRENT.BAT
(this file), STOREDAT.BAT runs CURRENT.BAT (it's the
first instruction in STOREDAT.BAT) with parameters,
namely DATE IS [DAY] [DATE nn-nn-nnn]. The CALDATE variable
used below is picked up from the fourth parameter.
DAYWEEK is, as can be seen, the third parameter. It is
used merely to show the day. If you're clever, you can
place the DAYWEEK variable into the Windows 95 global
environment using WINSET, a free program from Microsoft
included on the Windows 95 CD. (That's right. A DOS batch
file can insert a global environment variable using WINSET.
See the WINSET documentation.)

:routines
set DAYWEEK=%3
set CALDATE=%4
if %DAYWEEK%==Mon set DAY=Monday
if %DAYWEEK%==Tue set DAY=Tuesday
if %DAYWEEK%==Wed set DAY=Wednesday
if %DAYWEEK%==Thu set DAY=Thursday
if %DAYWEEK%==Fri set DAY=Friday
if %DAYWEEK%==Sat set DAY=Saturday
if %DAYWEEK%==Sun set DAY=Sunday
set DAYWEEK=

echo Today is %DAY%. The date is %CALDATE%.
echo.

if exist c:\temphold\nul deltree /y c:\temphold
:: Gets rid of this folder if it exists.
md c:\temphold
:: Creates it.

xcopy c:\windows\temp c:\temphold /s /h /c /i /r /d:%CALDATE%
:: Copies all files with the current day's date.
:: Xcopy's switches copy all files and folders (even hidden ones)
:: and do other necessary things. See Xcopy's help screen.

deltree /y c:\windows\temp >nul
:: Removes the temp folder. If a file is in use,
:: the folder will NOT be deleted.

xcopy c:\temphold c:\windows\temp /s /h /c /i /r >nul
:: Copies the live stuff back.

deltree /y c:\temphold
:: And now we are at last free of the older temp junk.

del storedat.bat
:: And we've cleaned up the temp batch file that this one uses.
:: This actually isn't needed, since the routines used at top
:: create the new temp batch file anyway, whether the old one
:: exists or not, but I like clean programming.

:end

********** BATCH FILE ENDS HERE. **********
.............................................
Method Two.

This is a recipe I discovered while working on a few old skool dos batch files. This is a quick .bat file to create a folder based on the current date however, this sets it up in the MMDDYYYY format. Useful for those .bat scripters who need to use the current date variable in such a format.

Just copy and paste the text in quotes to a text file. Then rename file test.bat and run from command line to see (echo is on by default).

This can be used in a very powerful way, the FOR command can be reused and changed to query to see if a file exists and then if so create the folder, etc.

for now, lets start basic...

Quote:

echo on
@REM Seamonkey's quick date batch (MMDDYYYY format)
@REM Setups %date variable
@REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%

this does nothing but setup the %date variable to be todays date in MMDDYYYY format so it can be called later in the script, etc..
Thanks to Seamonkey.

Please post back if you have any more problems or questions.

by bcpgm

Thank you very much.
The method two did not work.

Because my goal was to create a folder with the current date in the format of MM-DD-YY,
I used
MK %CALDATE%

Thanks again.

by Former member

Please post back if you have any more problems or questions.

This discussion is closed to new replies.