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

get most recent filename

582 views
Skip to first unread message

Rob McCauley

unread,
Aug 28, 2002, 6:24:54 PM8/28/02
to
Help I'm trying to write a simple batch to copy the most recently
modified file to a new folder.
I started with the DOS command DIR /O-D /B which gave me a list of
filenames with the most recent at the top. How can I take this one
filename and use it as part of a DOS COPY command?
Or is there an easier approach altogether?
Thanks
Rob - rob...@yahoo.com

Larry__Weiss

unread,
Aug 28, 2002, 6:49:59 PM8/28/02
to


Here's what I use to get the most recently modified filename
into an environment variable's value, packaged here as FRESH-E.BAT

@echo off %:: fresh-e.bat ::%
if (%1)==() goto usage
if (%1)==(-recursive-) goto recursive
:: create the debug script needed to turn the first CR in a file to a ^Z character
echo.ea0 51 be 0 1 80 3c d 75 7 b8 1a 0 88 4 eb 3 46 e2 f1 59 c3>%temp%.\losecr
for %%c in (rip a0 g w q) do echo.%%c>>%temp%.\losecr
echo call %0 -recursive- %1 >%temp%.\helper1.txt
debug %temp%.\helper1.txt <%temp%.\losecr>nul
copy %temp%.\helper1.txt/a %temp%.\helper2.txt/b>nul
dir /b /o-d /a-d>>%temp%.\helper2.txt
debug %temp%.\helper2.txt <%temp%.\losecr>nul
copy %temp%.\helper2.txt/a %temp%.\helper.bat/b>nul
call %temp%.\helper.bat
erase %temp%.\losecr
erase %temp%.\helper1.txt
erase %temp%.\helper2.txt
erase %temp%.\helper.bat
goto eof
:recursive
set %2=%3
goto eof
:usage
echo usage: fresh-e envvar
echo sets environment variable passed as only argument to the file
echo in current directory with most recent date
echo example:
echo call fresh-e lastmodifiedfile
echo echo %%lastmodifiedfile%%
goto eof
:eof

Timo Salmi

unread,
Aug 28, 2002, 11:56:04 PM8/28/02
to
Rob McCauley <rob...@yahoo.com> wrote:
> Help I'm trying to write a simple batch to copy the most recently
> modified file to a new folder.

@echo off
dir /o-d /b|gawk 'NR==1{printf"@set file_=%%s\n",$1}'>>tmp$$$.bat
for %%c in (call del) do %%c tmp$$$.bat
copy %file_% WhereEver
set file_=

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
Useful batch files and tricks ftp://garbo.uwasa.fi/pc/link/tsbat.zip

Larry__Weiss

unread,
Aug 29, 2002, 1:00:12 AM8/29/02
to
Timo Salmi wrote:
> Rob McCauley <rob...@yahoo.com> wrote:
> > Help I'm trying to write a simple batch to copy the most recently
> > modified file to a new folder.
>
> @echo off
> dir /o-d /b|gawk 'NR==1{printf"@set file_=%%s\n",$1}'>>tmp$$$.bat
> for %%c in (call del) do %%c tmp$$$.bat
> copy %file_% WhereEver
> set file_=
>

It's really too bad that GAWK isn't a standard part of the MSDOS batch
tools. As your example shows, GAWK enables some very simple solutions
to frequently needed batch scripting challenges.

Since GAWK is free and very widely available, I welcome solutions that
use it. A large part of my personal batch script collection uses it.

- LarryW

Timo Salmi

unread,
Aug 29, 2002, 2:27:23 AM8/29/02
to
Larry__Weiss <l...@airmail.net> wrote:

> Timo Salmi wrote:
> > dir /o-d /b|gawk 'NR==1{printf"@set file_=%%s\n",$1}'>>tmp$$$.bat

> It's really too bad that GAWK isn't a standard part of the MSDOS batch


> tools. As your example shows, GAWK enables some very simple solutions
> to frequently needed batch scripting challenges.

Yes. For exactly this practical reason I have added two
Unix-originating tools, awk and sed, to my regular MS-DOS commands
arsenal, my batch FAQ inclusive. What I particularly like about
these two is that they do not interfere with the original platform.
Also, they sometimes much enhance portability from Unix ideas if a
solution already happens to be available on that side.

QBASIC, which can more legitimately be considered part of the
original MS-DOS, is also a tool that can be used to solve many, even
tricky MS-DOS batch problems. But not nearly as conveniently as
especially gawk.

On the other hand, if one is just interested in the pure art of
batch programming, producing batches with strictly the original set
becomes an interesting challenge. Benny Pedersen, in particular, has
put much nice effort in presenting such batch solutions.

> Since GAWK is free and very widely available, I welcome solutions that
> use it. A large part of my personal batch script collection uses it.

For those who do not have the links, e.g. the ones I use:

233923 Mar 25 1995 ftp://garbo.uwasa.fi/pc/unix/gawk2156.zip
gawk2156.zip GNU awk text scanning and processing language

20368 Oct 1 1991 ftp://garbo.uwasa.fi/pc/unix/sed15x.zip
sed15x.zip HHSED executable, E.Raymond+D.Kirschbaum+H.Helman

William Allen

unread,
Aug 29, 2002, 3:07:40 AM8/29/02
to
"Rob McCauley" wrote in message

> Help I'm trying to write a simple batch to copy the most recently
> modified file to a new folder.
> I started with the DOS command DIR /O-D /B which gave me a list of
> filenames with the most recent at the top. How can I take this one
> filename and use it as part of a DOS COPY command?

Windows 95/98/ME only:
You can use the DIR listing you suggest, combined with
the Universal Prefixer technique to snip off the first line
prefixed with SET F/N=
The resulting workfile can be run as a Batch file to set
the name of the most recent file in the variable F/N

This demo shows working syntax:

====Begin cut-and-paste (omit this line)
@ECHO OFF

:: Make Universal Prefix "SET F/N=" (no terminal cr/lf)
ECHO. @PROMPT e0'SET F/N='$_rcx$_8$_w0$_q>%TEMP%.\_P.BAT
%COMSPEC% /e:4096 /c %TEMP%.\_P.BAT|debug %TEMP%.\_P.BAT>NUL

DIR *.* /b /-p /o-d>>%TEMP%.\_P.BAT
:: Snip off the top line of the list
TYPE %TEMP%.\_P.BAT|find "SET F/N=">%TEMP%.\_P.BAT

:: Grab the text in variable and delete workfile
FOR %%C IN (CALL DEL) DO %%C %TEMP%.\_P.BAT
ECHO. Demo command to copy most recently modified file
ECHO. (enclose long file names with [Space]s in "quotes"):
:: Remove the ECHO.{demo!} to activate command
ECHO.{demo!}COPY /y "%F/N%" C:\DestinationFolder\*.*

====End cut-and-paste (omit this line)
Win9x GUI study/demo only. Cut-and-paste as Batch script (file with .BAT
extension). Lines that don't begin with 2 spaces have wrapped by mistake.

Note: How the Universal Prefixer file works

First, you create a file consisting of say the eight characters:
SET F/N=
and by that I mean _only_ those seven characters, with no
final CarriageReturn Linefeed. This file can be created with
the standard Windows hex editor DEBUG.EXE.
In the demo the file is created as _P.BAT in the folder
TEMP (which is a standard folder designed for transient files)

Then if you _append_ a DIR listing to that file by using, say
DIR *.* /b /-p /o-d>>%TEMP%.\_P.BAT
then the result would be that the {special SET file} has
the contents of first line of that DIR listing appended on the
same line. So the {special SET file} becomes:
SET F/N=fist name on list
{rest of list follows}

A FIND filter is used to isolate out the line containing
the SET F/N= and the result is run to recover the first name.

Because it allows you to prefix almost anything with a SET
command, this technique is called the Universal Prefixer.

--
(pp) William Allen


Dr John Stockton

unread,
Aug 29, 2002, 6:20:15 AM8/29/02
to
JRS: In article <1ae42f1f.02082...@posting.google.com>, seen
in news:alt.msdos.batch, Rob McCauley <rob...@yahoo.com> posted at Wed,
28 Aug 2002 15:24:54 :-

>Help I'm trying to write a simple batch to copy the most recently
>modified file to a new folder.
>I started with the DOS command DIR /O-D /B which gave me a list of
>filenames with the most recent at the top. How can I take this one
>filename and use it as part of a DOS COPY command?

dir /o-d /b | COLS 'copy * 1- * 'newfldr . > x.bat
call x
del x.bat

To copy those new today, however,

HUNT * n "copy" "newfldr"

COLS, HUNT via sig line 3.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP <URL:http://www.merlyn.demon.co.uk/programs/> - 00index.txt.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Dr John Stockton

unread,
Aug 29, 2002, 6:13:25 AM8/29/02
to
JRS: In article <CCC8D997694455C2.7788429220C7A1D6.86C091173AD5F69B@lp.
airnews.net>, seen in news:alt.msdos.batch, Larry__Weiss
<l...@airmail.net> posted at Thu, 29 Aug 2002 00:00:12 :-

>
>Since GAWK is free and very widely available, I welcome solutions that
>use it. A large part of my personal batch script collection uses it.

Such solutions should refer to the version(s) of GAWK for which they
have been tested. The version which has been recommended via here in
the past is defective, except for NA users, when date/time functions are
used in other than a simple manner.



--
© 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 etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.txt
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Rob McCauley

unread,
Aug 29, 2002, 3:03:17 PM8/29/02
to
Thanks Larry, your batch works like a champ!


Larry__Weiss <l...@airmail.net> wrote in message news:<89A953AA298D2B86.3FBD6FB6...@lp.airnews.net>...

James Quigley

unread,
Sep 3, 2002, 4:15:07 PM9/3/02
to
t...@UWasa.Fi (Timo Salmi) wrote in message news:<akk60k$f...@poiju.uwasa.fi>...

> Rob McCauley <rob...@yahoo.com> wrote:
> > Help I'm trying to write a simple batch to copy the most recently
> > modified file to a new folder.
>
> @echo off
> dir /o-d /b|gawk 'NR==1{printf"@set file_=%%s\n",$1}'>>tmp$$$.bat
> for %%c in (call del) do %%c tmp$$$.bat
> copy %file_% WhereEver
> set file_=
>
> All the best, Timo

To avoid having this batch file generate a subdirectory name if a subdirectory
was the most recently created entry, shouldn't you add "/a-d"
(attribute not directory) to the dir command, so it would read:
dir /o-d /a-d /b|gawk 'NR==1{printf"@set file_=%%s\n",$1}'>>tmp$$$.bat

Regards, Jim

Timo Salmi

unread,
Sep 3, 2002, 11:51:02 PM9/3/02
to
In article <d56b694f.0209...@posting.google.com>,

James Quigley <jimqu...@juno.com> wrote:
> t...@UWasa.Fi (Timo Salmi) wrote in message news:<akk60k$f...@poiju.uwasa.fi>...
> > dir /o-d /b|gawk 'NR==1{printf"@set file_=%%s\n",$1}'>>tmp$$$.bat

> was the most recently created entry, shouldn't you add "/a-d"

> (attribute not directory) to the dir command, so it would read:

Yes. Good catch, Jim!

0 new messages