6

I have up to 4 files based on this structure (note the prefixes are dates)

  • 0830filename.txt
  • 0907filename.txt
  • 0914filename.txt
  • 0921filename.txt

I want to open the the most recent one (0921filename.txt). how can i do this in a batch file?

Thanks.

5 Answers 5

9

This method uses the actual file modification date, to figure out which one is the latest file:

@echo off
for /F %%i in ('dir /B /O:-D *.txt') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "dummy" "%~1"
exit /B 0

This method, however, chooses the last file in alphabetic order (or the first one, in reverse-alphabetic order), so if the filenames are consistent - it will work:

@echo off
for /F %%i in ('dir /B *.txt^|sort /R') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "dummy" "%~1"
exit /B 0

You actually have to choose which method is better for you.

4
  • Can you tell me what the %%i does in line 2 and what the %~1 does in line 7? Thanks!
    – Keng
    Commented Sep 9, 2008 at 13:47
  • %%i is the loop variable (it will get the value of the first word in each line that the command inside parentheses writes to standard output). %1 is a simple way to access the command line argument passed to the script or the label (like in my case). %~1, however, removes the quotes (if any).
    – Paulius
    Commented Sep 9, 2008 at 13:51
  • Vilnius, Lithuania...?....hmmmm...I know a programmer there...Gintaras Didzgalvis, he makes QuickMacros (QuickMacros.com). You should look him up sometime.
    – Keng
    Commented Jun 8, 2009 at 17:09
  • Just wanted to say I used the solution above to open the my latest to do text file everytime I start up my computer! Thanks for the help! Commented Sep 20, 2011 at 16:21
6

Sorry, for spamming this question, but I just really feel like posting The Real Answer. If you want your BATCH script to parse and compare the dates in filenames, then you can use something like this:

@echo off

rem Enter the ending of the filenames.
rem Basically, you must specify everything that comes after the date.
set fn_end=filename.txt

rem Do not touch anything bellow this line.
set max_month=00
set max_day=00

for /F %%i in ('dir /B *%fn_end%') do call :check "%%i"
call :open %max_month% %max_day%
exit /B 0

:check
    set name=%~1
    set date=%name:~0,4%
    set month=%date:~0,2%
    set day=%date:~2,2%
    if /I %month% GTR %max_month% (
        set max_month=%month%
        set max_day=%day%
    ) else if /I %month% EQU %max_month% (
        set max_month=%month%
        if /I %day% GTR %max_day% (
            set max_day=%day%
        )
    )
exit /B 0

:open
    set date=%~1
    set month=%~2
    set name=%date%%month%%fn_end%
    start "dummy" "%name%"
exit /B 0
5
  • I dot't at this time but dang if that ain't nice!!
    – Keng
    Commented Sep 9, 2008 at 13:49
  • MAN! Do you know of any good books on learning to write stuff like this?!
    – Keng
    Commented Sep 9, 2008 at 13:55
  • you could probably answer this question too! beta.stackoverflow.com/questions/51054/…
    – Keng
    Commented Sep 9, 2008 at 13:57
  • Actually, there's no real need for books. You can simply type HELP in command line, to get the list of all default commands. And then you can read the help of each individual command for more information - you just simple add the /? switch to the command. Or at least, that's how I do it.
    – Paulius
    Commented Sep 9, 2008 at 13:58
  • I had no idea SET could do substrings. This helped me a ton. Thanks!
    – Eddie Deyo
    Commented Sep 26, 2008 at 17:55
4

One liner, using EXIT trick:

FOR /F %%I IN ('DIR *.TXT /B /O:-D') DO NOTEPAD %%I & EXIT

EDIT:

@pam: you're right, I was assuming that the files were in date order, but you can change the command to:

FOR /F %%I IN ('DIR *.TXT /B /O:-N') DO NOTEPAD %%I & EXIT

then you have the file list sorted by name in reverse order.

0
1

Here you go... (hope no-one beat me to it...) (You'll need to save the file as lasttext.bat or something) This will open up / run the oldest .txt file

dir *.txt /b /od > systext.bak 
FOR /F %%i in (systext.bak) do set sysRunCommand=%%i 
call %sysRunCommand%
del systext.bak /Y

Probably XP only. BEHOLD The mighty power of DOS.
Although this takes the latest filename by date - NOT by filename..

If you want to get the latest filename, change /od to /on .
If you want to sort on something else, add a "sort" command to the second line.

1
  • 1
    Your method will work, but it will create unnecessary temp files. Also, when using del in BATCH scripts, I always add the /Y switch - otherwise the del command can be very annoying... :)
    – Paulius
    Commented Sep 9, 2008 at 13:29
-1

Use regular expression to parse the relevant integer out and compare them.

1
  • I think it's implied in the question that he only wants to use things which would be available from a command line. Do you know of a command line RE tool that would be available on Windows? Commented Sep 9, 2008 at 13:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.