Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rename File

2 views
Skip to first unread message

BFH

unread,
May 21, 2001, 3:09:16 AM5/21/01
to
Hello All;

Quick question;

I do a nightly backup via batch file. The batch file says name it "Nightly
Backup"

Now, each night the file would be over written.

What I would like to do is after the batch runs, rename it with todays
date (what ever the date might be).

Does anyone know how I can do this in a batch file?

Thanks

---Ben


Timo Salmi

unread,
May 21, 2001, 3:47:18 AM5/21/01
to
BFH <bharmo...@fuse.net> wrote:
> What I would like to do is after the batch runs, rename it with todays
> date (what ever the date might be).
> Does anyone know how I can do this in a batch file?

The tools for the task top the most frequently asked questions in
this MS-DOS batch newsgroup. See e.g.

34) Enticing the current date into an environment variable
59) How get today's date into a six-character environment variable?

184193 Mar 8 2001 ftp://garbo.uwasa.fi/pc/ts/tsbat67.zip
tsbat67.zip A collection of useful batch files and tricks, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

William Allen

unread,
May 21, 2001, 4:11:21 AM5/21/01
to
BFH wrote in message
...snip

> What I would like to do is after the batch runs, rename it with todays
> date (what ever the date might be).
>
> Does anyone know how I can do this in a batch file?

This was covered at some length recently in my post.

From: "William Allen"
Newsgroups: alt.msdos.batch
Subject: Date and time to use in filenames (was Re: rename
Date: Mon, 23 Apr 2001 12:43:02 +0100
Message-ID: <9c14i5$bho7p$1...@ID-55970.news.dfncis.de>

That post contains several examples of different ways of writing
the code you need for this task. If you find that you can't follow
what you need, please post again with specific questions, since
that will help me to re-work any parts that aren't clear.

You can read old Usenet posts at:
http://groups.google.com/advanced_group_search

Note: Google search by Message-ID is usually the quickest.
When doing so, it's better to include the <angle> brackets,
since they are strictly part of the Message-ID (see rfc850/1036);
although Google Message-ID search can manage without them.
For anyone confused by exact information from headers posted
in a message body, consult rfc1036, which explains that the
first blank line in a news message terminates all header
information. All rfc850/1036 compliant readers will permit this.

--
William Allen


William Allen

unread,
May 21, 2001, 5:26:46 AM5/21/01
to
BFH wrote in message
...snip
> What I would like to do is after the batch runs, rename it with todays
> date (what ever the date might be).
>
> Does anyone know how I can do this in a batch file?

I suppose it's a while since demo code was posted, so it
won't hurt to rework it, I guess<G>.

Note that getting the date and time in a usable way isn't
a primitive operation in Batch scripting. Many utilities exist
to make it easier, if you prefer that route, but they aren't
necessary and doing it yourself avoids copyright problems.

This batch script contains the "hard" part in a re-usable
subroutine, and has several lines of demo code to show
how to use the results the subroutine call generates.

Effectively, the subroutine creates a temporary file in your
TEMP folder, and grabs the creation time and date in two
environment variables DATE and TIME. To instantiate
(use their current values as part of another batch command)
such variables, simply use syntax: %DATE% or %TIME%.

If you run the script, the demo part creates two files in
your current folder, displays their folder-index details
and then renames them, re-displaying details. Then the
script tidies up by deleting the demo files for you unless you
answer N to the prompt within 10 seconds (because you
might want to verify them or look at them for yourself).

::====DATIME.BAT
@ECHO OFF
IF (%1)==(GOTO:) %1%2 (Subroutine handler)
:: Re-usable date/time subroutine - William Allen 21 May 2001

:: Application code can go here

:: This subroutine call sets variables DATE and TIME
CALL %0 GOTO: _DATIME

:: These are purely demo lines to illustrate syntax
CLS
ECHO.
ECHO. The subroutine loads date in %%DATE%% and time in %%TIME%%
ECHO. in this format: current date %DATE%, current time %TIME%
ECHO.
ECHO. Create a file for date demo>DATEFILE.DEM
ECHO. And another one for the time demo file>TIMEFILE.DEM
ECHO. If we create these two files:
DIR /-w /os *.DEM | find "DEM"
ECHO.
ECHO. We can rename them using %%DATE%% and %%TIME%% to:
REN DATEFILE.DEM %DATE%.DEM
REN TIMEFILE.DEM %TIME%.DEM
DIR /-w /os *.DEM | find "DEM"
ECHO.
choice /c:yn /ty,10 "Erase demo files "
IF NOT ERRORLEVEL 2 deltree /y %DATE%.DEM %TIME%.DEM>NUL
SET DATE=
SET TIME=
:: End Demo lines

:: More application code can go here

GOTO EOF (=Subroutine code follows=)
:_DATIME (Usage: CALL %0 GOTO: _DATIME)
REM Create reconfigurable name file>%TEMP%.\CALL%%T%%.BAT
DIR /-w %TEMP%.\CALL%%T%%.BAT | find "%%">>%TEMP%.\CALL%%T%%.BAT
SET T= %0 GOTO: _DATIME1
FOR %%C IN (CALL DEL) DO %%C %TEMP%.\CALL%%T%%.BAT
SET T=
GOTO EOF
:_DATIME1 (recall point within _DATIME routine)
ECHO.e10b 2d>%TEMP%.\SCR
FOR %%F IN (e10e''2d e11e''2d w q) DO ECHO.%%F>>%TEMP%.\SCR
ECHO.SET DATE=%5>%TEMP%.\DT.BAT
ECHO.SET TIME=%6>>%TEMP%.\DT.BAT
debug %TEMP%.\DT.BAT<%TEMP%.\SCR>NUL
CALL %TEMP%.\DT
deltree /y %TEMP%.\DT.BAT %TEMP%.\SCR>NUL

:EOF (End of file)
::====

--
William Allen


William Allen

unread,
May 21, 2001, 8:05:47 AM5/21/01
to
William Allen wrote in message
...snip

> I suppose it's a while since demo code was posted, so it
> won't hurt to rework it, I guess<G>.

This reworks the old code a little more to remove a
redundant workfile, slims down the subroutine and
expands the demo code area. Tested in Win9x DVM.

One or two people emailed me about lines like:


IF (%1)==(GOTO:) %1%2 (Subroutine handler)

This is a subroutine recursive call trap that's part of
a standard template we use here. The text:
"(Subroutine handler)" is purely a functionless comment,
which Windows ignores when processing the line.
This line traps all re-calls of the code that have the %1
command-line token set to GOTO: and a destination label
is expected in the %2 token. Unless the %1 token contains
GOTO: (which can't occur as a filename) the line has no effect.

In the full current version template, the line would be:
IF (%1)==(GOTO:) %1%2 SUBCALL (Subroutine handler)
and a further section of the template with the :SUBCALL label
traps syntax errors in the subroutine calls. This part of the
template is purely for diagnostic use when writing new code.
If the recall syntax is correct, Windows also treats "SUBCALL"
as a functionless comment.

Watch line wrap. Lines that don't begin with two
[Space]s have accidentally wrapped in transmission.
You can omit the top/bottom ::==== delimiting lines.

Lines that begin with :: are non-executable comments
and can be removed if you wish.

The REM line in the subroutine is used both to comment
and create a zero-byte file and should _not_ be removed.

::====DATIME.BAT
@ECHO OFF
IF (%1)==(GOTO:) %1%2 (Subroutine handler)

:: Date/time subroutine - William Allen 21 May 2001 v2

:: This call sets variables DATE and TIME to current values
CALL %0 GOTO: _DATIME

::----Demo-starts----These lines show usage and syntax
CLS
:: Clear any left over demo files
IF EXIST %DATE%.DEM DEL %DATE%.DEM
IF EXIST %TIME%.DEM DEL %TIME%.DEM


ECHO.
ECHO. The subroutine loads date in %%DATE%% and time in %%TIME%%
ECHO. in this format: current date %DATE%, current time %TIME%
ECHO.
ECHO. Create a file for date demo>DATEFILE.DEM
ECHO. And another one for the time demo file>TIMEFILE.DEM
ECHO. If we create these two files:

DIR /-w /os *EFILE.DEM | find "DEM"


ECHO.
ECHO. We can rename them using %%DATE%% and %%TIME%% to:
REN DATEFILE.DEM %DATE%.DEM
REN TIMEFILE.DEM %TIME%.DEM

DIR /-w /os *.DEM | find "%DATE%"
DIR /-w /os *.DEM | find "%TIME%"


ECHO.
choice /c:yn /ty,10 "Erase demo files "
IF NOT ERRORLEVEL 2 deltree /y %DATE%.DEM %TIME%.DEM>NUL
SET DATE=
SET TIME=

::----End-of-Demo----

GOTO EOF (=Subroutine code follows=)
:_DATIME (Usage: CALL %0 GOTO: _DATIME)

REM Make 0-byte file with flexible name>%TEMP%.\CALL%%T%%.BAT


DIR /-w %TEMP%.\CALL%%T%%.BAT | find "%%">>%TEMP%.\CALL%%T%%.BAT
SET T= %0 GOTO: _DATIME1
FOR %%C IN (CALL DEL) DO %%C %TEMP%.\CALL%%T%%.BAT
SET T=
GOTO EOF
:_DATIME1 (recall point within _DATIME routine)

ECHO.e100'SET DATE=%5'd a'SET TIME=%6'd a 1a>%TEMP%.\DT.BAT
FOR %%C IN (e10b''2d e10e''2d e11e''2d w q) DO ECHO.%%C>>%TEMP%.\DT.BAT
TYPE %TEMP%.\DT.BAT | debug %TEMP%.\DT.BAT>NUL
FOR %%C IN (CALL DEL) DO %%C %TEMP%.\DT.BAT

William Allen

unread,
May 21, 2001, 8:42:11 AM5/21/01
to
William Allen wrote in message
> This reworks the old code a little more to remove a
> redundant workfile, slims down the subroutine and
> expands the demo code area. Tested in Win9x DVM.

More re-working and tidying up gives DATIME.BAT demo v3:

::====DATIME.BAT
@ECHO OFF
IF (%1)==(GOTO:) %1%2 (Subroutine handler)

:: Date/time subroutine - William Allen 21 May 2001 v3

:: This call sets variables DATE and TIME to current values
CALL %0 GOTO: _DATIME

::----Demo-starts----These lines show usage and syntax
CLS
:: Clear any left over demo files
IF EXIST %DATE%.DEM DEL %DATE%.DEM
IF EXIST %TIME%.DEM DEL %TIME%.DEM
ECHO.
ECHO. The subroutine loads date in %%DATE%% and time in %%TIME%%
ECHO. in this format: current date %DATE%, current time %TIME%
ECHO.
ECHO. Create a file for date demo>DATEFILE.DEM
ECHO. And another one for the time demo file>TIMEFILE.DEM
ECHO. If we create these two files:

DIR /-w DATEFILE.DEM | find "DEM"
DIR /-w TIMEFILE.DEM | find "DEM"


ECHO.
ECHO. We can rename them using %%DATE%% and %%TIME%% to:
REN DATEFILE.DEM %DATE%.DEM
REN TIMEFILE.DEM %TIME%.DEM

DIR /-w *.DEM | find "%DATE%"
DIR /-w *.DEM | find "%TIME%"


ECHO.
choice /c:yn /ty,10 "Erase demo files "
IF NOT ERRORLEVEL 2 deltree /y %DATE%.DEM %TIME%.DEM>NUL
SET DATE=
SET TIME=
::----End-of-Demo----

GOTO EOF (=Subroutine code follows=)
:_DATIME (Usage: CALL %0 GOTO: _DATIME)

ECHO.SET T= %0 GOTO: _DATIME1>%TEMP%.\CALL%%T%%.BAT


DIR /-w %TEMP%.\CALL%%T%%.BAT | find "%%">>%TEMP%.\CALL%%T%%.BAT

Outsider

unread,
May 21, 2001, 8:56:52 AM5/21/01
to
William Allen wrote:
>

[snipped]



> ::====DATIME.BAT
> @ECHO OFF
> IF (%1)==(GOTO:) %1%2 (Subroutine handler)
> :: Date/time subroutine - William Allen 21 May 2001 v2

[snipped]


Very clever work William. I have doubts about batch amateurs
understanding it, excellent documentation notwithstanding.

--
<!-Outsider//->
MS-DOS 6.22, Windows for Workgroups 3.11, Netscape Navigator 4.08
MS-DOS 7.1, Windows 4.1 (a.k.a. 98), Netscape Navigator 4.74

Dr John Stockton

unread,
May 21, 2001, 3:26:51 PM5/21/01
to
JRS: In article <tghg0r3...@corp.supernews.com>, seen in
news:alt.msdos.batch, BFH <bharmo...@fuse.net> wrote at Mon, 21 May
2001 03:09:16 :-

> I do a nightly backup via batch file. The batch file says name it "Nightly
>Backup"
>
> Now, each night the file would be over written.
>
> What I would like to do is after the batch runs, rename it with todays
>date (what ever the date might be).

If you read the group before asking, you can get answers sooner. I have
in essence answered this one yesterday, in

From: Dr John Stockton <sp...@merlyn.demon.co.uk>
Newsgroups: alt.msdos.batch
Subject: Re: ## Need to rename file with date stamp
Date: Sun, 20 May 2001 20:27:23 +0100
Organization: Home, 'Surrey', UK
Message-ID: <xLeA+tOb...@merlyn.demon.co.uk>

Note that if you do not have control over when at night the backup is
done - perhaps you work into the small hours on occasion - you may
actually need yesterday's date. Assuming "night" always lies sometime
within 12:00:00 today to 11:59:59 tomorrow, the simple addition of an
"h12" parameter to NOWMINUS will accommodate this.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL: http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm &c.

0 new messages