Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I generate a file with date and time as file name.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I want a batch file which will generate a file with primary name as date and time.txt

Let us assume that there is a file called demo.txt. I want this file to be copied as Current date and time. txt which is for example, 09/03/2002 15:30.txt.

Thank you,
The receiver.
 
/ and : are not valid characters for file creation. However you can create a file name of 09032002_1250pm.txt
Spaces are allowed.

@echo off

set dd=%date%
set year=%dd:~12,2%
set day=%dd:~7,2%
set month=%dd:~4,2%
set folder=%month%%day%%year%

you can do the same with time...

mkdir %folder% where ever you want the folder to be created

 
Question to SSphoenix:
I tried your batch file solution and it did not work. I made sure I had all the 5 "set"
properly copied. I named my batch file folder.bat, should I have used another name?
Thank you for your answer.
Charles Keller.
 
There is a small utility called envtime.exe that will set some variables up in the environment, these can then be used to construct a filename.
envtime.exe can be downloaded from:-
envtime needs to be placed in any directory that is pathed
I.E. C:\WINDOWS\COMMAND
NB: as ssphoenix has said you cannot use \ or : in your filename!
The example given on the above web page is flawed and will not work without some minor alterations.

Here is batch file you could try with the above utility
N.B. If running from START-RUN you will HAVE to include the full path to the file.

@ECHO OFF
@if '%1.txt'=='' goto NOINPUT
@if not exist %1.txt goto DOH

@ENVTIME > {TIME}.BAT
@CALL {TIME}.BAT
@ECHO.
@REN %1.txt %YEAR%-%MON%-%DOM%_%HOUR%%MIN%.TXT
@DEL {TIME}.BAT
:: Clean away variables used
FOR %%V IN (YEAR PYEAR YR PYR MON DOM DOW HOUR MIN YDOM YMON YYEAR YYR) DO SET %%V=
goto END
:DOH
@ECHO File %1.txt not Found!!!
goto END:
:NOINPUT
@ECHO A FILENAME is required!
@ECHO Example: %0 demo
@ECHO The txt extension is not required.
:END
 
To PMR (visitor)
Your batch file sample re. date and time sounds interesting, but I cannot make it work.
I wonder if there is something missing in the line @if '%1.txt'==" goto NOINPUT.
Can I use .==. instead?
I put the envtime.exe and {TIME}.bat and %0 demo.txt in the path C:\windows\command\ and it still doesn't work.
Should I use a redirection command between {TIME}.bat and %1.txt?
What did I do wrong?

Charles Keller.
 
{TIME}.bat is created by running the batch file.

Copy the script into a file called Demo.bat (or any other that you want to call it.)
Then if you have a file called fred.txt you would enter
demo fred
This will then rename fred.txt to 20002-09-07_0927.txt
or whatever the date and time is.
The line:-
@if '%1.txt'=='' goto NOINPUT
Checks to see if you have entered a filename and is correct.
The next line
@if not exist %1.txt goto DOH
Checks if the file really exists.

Good luck.
 
To PMR (Visitor)
Thank you for your answer. Slowly I'm getting there; however a new bug is cropping up: The screen says:
ENVTIME - Copyright 1992 Steve Antonoff......
and then it says:
Out of environment space (6 times)

What could it be?

I have all the necessary files (demo.bat, fred.txt, envtime.exe)
in the following paths:
c:\windows\command\
as well as c:\batchHow can I increase the environment space?
Thank you very much for your help.
Charles Keller
 
An alternative to using ENVTIME would be to use something like this:-

::Copy script to filern.bat and put in c:\windows\command
:: Usage:- filern FileName (no txt extension)

@ECHO OFF
@if '%1'=='' goto NOINPUT
@if not exist %1.txt goto DOH

::puts Date and Time prompt commands into {Temp1}.bat
@ECHO @prompt set $d$_ set $d$_ set $d$_ set HR $t$_ set M $t > %temp%\{Temp1}.bat

:: Runs {Temp1}.bat and puts output into {Temp2}.bat
%comspec% /e:2048 /c %temp%\{Temp1}.bat > %temp%\{Temp2}.bat

:: Creates DEBUG Script in {temp3}.dat file
echo e 106 44 41 59 3d>%temp%.\{Temp3}.DAT
echo e 10c 0d 0a 20 20 20 20 20 20>>%temp%.\{Temp3}.DAT
echo e 11b 20 20 20 4d 4f 4e 3d>>%temp%.\{Temp3}.DAT
echo e 124 0d 0a 20 20 20>>%temp%.\{Temp3}.DAT
echo e 130 20 20 20 20 20 59 45 41 52 3d>>%temp%.\{Temp3}.DAT
echo e 147 3d>>%temp%.\{Temp3}.DAT
echo e 14a 0d 0a 20 20 20 20 20 20 20>>%temp%.\{Temp3}.DAT
echo e 15a 20 4d 49 4e 3d>>%temp%.\{Temp3}.DAT
echo e 161 0d 0a 20 20 20 20>>%temp%.\{Temp3}.DAT
::Write the changes
echo w>>%temp%.\{Temp3}.DAT
::Quit DEBUG
echo q>>%temp%.\{Temp3}.DAT

::Runs DEBUG to edit {Temp2}.BAT to correct set commands
%comspec% /e:2048 /c DEBUG %temp%\{Temp2}.BAT < %temp%.\{Temp3}.DAT > nul

:: Runs {Temp2}.bat to set up environment variables
call %temp%\{Temp2}.BAT

:: Renames file
@REN %1.txt %YEAR%-%MON%-%DAY%_%HR%%MIN%.TXT

:: Cleans away variables used
FOR %%V IN (DAY MON YEAR HR MIN) DO SET %%V=

::Deletes temp files
del %temp%.\{Temp?}.*
goto END

:DOH
@ECHO File %1.txt not Found!!!
goto END:
:NOINPUT
@ECHO A FILENAME is required!
@ECHO Example: %0 demo
@ECHO The txt extension is not required.
:END
 
To PMR. (Visitor)
Thank you for your last posting. Now my batch file works very well and I thank you for your valuable help.
Cheers:
Charles Keller.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top