tags:

views:

971

answers:

4

In Vista, I want to run a batch script to open the most recent file, based either on last modified date, or the date in the filename. It's for Malwarebytes' Anti-Malware logs in the %username&/appdata/roaming/Malwarebytes/Malwarebytes' Anti-Malware/Logs folder.

Log files are in this format here

mbam-log-2009-03-21 (00-20-21).txt

mbam-log-2009-03-21 (09-42-40).txt

mbam-log-2009-03-21 (11-02-43).txt

mbam-log-2009-03-21 (11-12-01).txt

mbam-log-2009-03-21 (12-01-42).txt

mbam-log-2009-03-21 (12-04-49).txt

mbam-log-2009-03-21 (14-01-41).txt

So its 24-hr format. I read on another page on here, and got this script here..

@echo off
dir *.txt /b /on > systext.bak 
FOR /F %%i in (systext.bak) do set sysRunCommand=%%i 
call %sysRunCommand%
del systext.bak /y

but it doesn't like the space in the filename.. always get an error. Anyone have any ideas?

A: 

The following one-line batch file will open the most recently modified file:

for /f "usebackq delims=" %%i in (`dir /b /o-d`) do @start "%%i"&goto :eof

Using dir strikes me as much simpler than trying to dissect the date in the filename. You can also order by filename (as the date format is somewhat ISO-8601-ish is sorts well).

The goto :eof is just there to make sure only the most recent file will be opened and not all in order of date/time.

As for your space problem, surrounding the file name with quotes usually should fix that, but sometimes it's a little difficult to know where they have to be. Also, for by default tokenizes its input at spaces, that's why I included delims= in there which essentially says »Put everything into the variable and don't do any tokenizing«.

Joey
hmm, right, "call" is for calling batch files. My bad. You should really use "start" there, but without the notepad. This should start the text file with whatever application is set for text files on your machine.
Joey
Actually, Windows seems pretty intelligent about that, 'calling' the text file resulted in it being opened by Notepad.
paxdiablo
+1  A: 

You actually have three problems in the script. The first is that %%i is likely to be set to the words with a filename- this can be fixed by using "delims=" in the for statement.

The second is that you need to quote spacey filenames in you call statement.

The third is that I'm not aware of a /y option for del, perhaps you mean del /f.

Amyway, give this one a shot:

@echo off
dir *.txt /b /on > systext.bak 
FOR /F "delims=" %%i in (systext.bak) do set sysRunCommand=%%i 
call "%sysRunCommand%"
del /f systext.bak
paxdiablo
instead of creating a temporary file, you could put the dir command within the for condition like ('dir *.txt /b /on')
Jay
A: 

I get this here to put the most recent mbam log file into a variable in Vista; but it doesn't work in XP. How can I get it to work in XP? I get the error '%%i was unexpected @ this time'

@echo off

dir "%userprofile%\Appdata\Roaming\Malwarebytes\Malwarebytes' Anti-Malware\Logs" /b /on > systext.bak 
FOR /F "delims=" %%i in (systext.bak) do set mbamlog=%%i
Except for the obvious which is to change it to %appdata% instead of %userprofile%\Appdata\Roaming
A: 

I know this is an old post, and I don't mean to revive it - I have an alternative approach to the question.

Malwarebytes' Anti-Malware includes the ability to reroute the log file to an alternate location (folder or specified file).

You can run "%programfiles%\malwarebytes' anti-malware\mbam.exe" /logtofile c:\logs\bob-mbam-log.txt. /logtofolder also works. (the above command assumes you're using a 32 bit system.)

When specifying a file, mbam appends instead of overwriting. So, that may make the log a little more difficult to parse.

Josh E. Roberts