batch file open last modified file

May 23, 2011 at 20:16:06
Specs: Windows 7
Hi,

I need to create a batch file to open the last modified excel file in a folder C:\Test\Unit 2. This folder also contained several sub-folders and the last modified file may exists in any of the sub-folders. I've been reading several posts in this forum on opening the latest modified file but still couldn't find the solution. I'm using Windows 7 64-bit. Any help is very much appreciated

Thanks


See More: batch file open last modified file

Report •


#1
May 25, 2011 at 04:21:48
Run this and post a few lines of the output.

:: ==========================================
::
:: bydate.bat Wed 25-05-2011 18:19:47.17
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d') do (
echo.%%~Ta %%a
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
May 25, 2011 at 07:39:07
I run this code by couldn't get any output

:: ==========================================
::
:: bydate.bat Wed 25-05-2011 18:19:47.17
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir "c:\test\unit 2" /b/s/a-d') do (
echo.%%~Ta %%a
)

I tried another way like this

@echo off & setLocal enableDELAYedeXpansioN

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)
for /f "tokens=* delims= " %%a in ('dir "c:\test\unit 2\%year%\%month%\%day%" /b/o-d') do (
start excel %%a
goto :eof
)

A new folder will be manually created every day in c:\test\unit 2\2011\05\[day number] and inside this folder there are excel files as follows

Test1.xls <--- last modified
Test2.xls
...etc

However when I ran the batch, I keep getting error message of " 'Test1.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct." The batch could identify the Test1.xls file as the last modified, so i guess the path is correct.


Report •

#3
May 25, 2011 at 08:57:52
" I run this code by couldn't get any output"

LOL Did you run it in a directory that HAS some files?


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
May 25, 2011 at 16:06:29
you're right. the directory doesn't has any files. however in my earlier question, I did mention that the files are in the subfolders of the directory, not straight in the directory

Report •

#5
May 25, 2011 at 22:49:16
If dir/b/s/a-d doesn't show them, it's anybody's guess.


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
May 26, 2011 at 01:57:59
Dear M2

I used

do (
start excel "%%a"
goto :eof
)

instead of

do (
echo.%%~Ta %%a
)

and it works. Thanks a lot for your guidance

one more, is there a way to always open my excel as read only?


Report •

#7
May 26, 2011 at 19:54:44
Dear M2,

Now I get what u mean. dir/b/s/a-d sort the file by file name but not by last modified date. I try dir/b/s/a-d/o-d but it sort the file by first folder--> last modified to first modified and after that it goes to the second folder --> last-modified to first modified etc

It doesn't do what I want which is sort the file by last modified date regardless the folder name. Can this be done?


Report •


Ask Question