1.

Solve : Batch file with dynamic file name?

Answer»

I'd like to have a shortcut/batch file on my desktop that will open a file which changes name somewhat each time it is updated. I'm not even sure if this is POSSIBLE, but here's the situation.

I have an Access file called "Status Log". Each time I update the log I append the current date to the filename; for example, "Status Log 071509" which means that the last update was July 15, 2009. If I then update the log 3 days later, I do a "Save As" and rename the file "Status Log 071809" and so on. The log is updated on an as-is basis only, so there is no pattern. The files are all saved in the same file, so the path never changes, just the latter part of the filename.

Is is possible to create a shortcut/batch file/whatever that I can place on my desktop that will search the folder, find the most current file by date, and open it?

I'm an absolute novice, so I wouldn't even know how to begin.

Thanks in advance.hm, well i'm still learning so theres probably an easier way, but heres what i have.


edit the 'c:\path\of\file' for the path you want,

Code: [Select]@echo off
for /f "tokens=1" %%a in ('dir /b /o:d c:\path\of\file\') do set file=%%a
notepad.exe %file%
Chris - Welcome to the CH forums.

Try this code - not tested... You must supply the correct path.

Code: [Select]@Echo Off
setlocal
cls

for /f "delims=*" %%# in ('dir /tw /o-d /b "path\to\files\Status Log *"') do (
start notepad %%#& exit

)


Good luckQuote from: BatchFileBasics on August 08, 2009, 06:52:59 PM

hm, well i'm still learning so theres probably an easier way, but heres what i have.


edit the 'c:\path\of\file' for the path you want,

Code: [Select]@echo off
for /f "tokens=1" %%a in ('dir /b /o:d c:\path\of\file\') do set file=%%a
notepad.exe %file%


I tried this (after creating a sample text file called 'log"):

@echo off
for /f "tokens=1" %%a in ('dir /b /o:d c:\Users\Desktop\Chris Cione\log.txt') do set file=%%a
notepad.exe %file%

When I ran the batch, I got the message "The system cannot find the path specified."

I know that's the path because I checked the file properties.

Is there SOMETHING I need to modify in the code?

Here you go, it should handle spaces now.


Code: [Select]@echo off
for /f "tokens=1" %%a in ('dir /b /o:d "c:\path\of\file\"') do set file=%%a
start notepad "%file%"Quote from: Dusty on August 08, 2009, 07:38:11 PM
Chris - Welcome to the CH forums.

Try this code - not tested... You must supply the correct path.

Code: [Select]@Echo Off
setlocal
cls

for /f "delims=*" %%# in ('dir /tw /o-d /b "path\to\files\Status Log *"') do (
start notepad %%#& exit

)


Tried it. A DOS window briefly (very briefly) open and closes. Nothing else happens.

Good luck
I used this:

@echo off
for /f "tokens=1" %%a in ('dir /b /o:d "c:\Users\Chris Cione\Desktop\log*"') do set file=%%a
start notepad "%file%"

I have two files I created for testing purposes - "log 080109" and "log 080509". The code won't open either; I'm prompted to create a new file called "log".

What I'm attempting is to create logic so that the batch searches for the most current file (by date) and open that file.
well this is all i got left, kinda sloppy because it wont handle double spaces....

Code: [Select]for /f "tokens=1-4 delims= " %%a in ('dir /b /w /o:d "c:\tests\"') do set file=%%a %%b
notepad.exe %file%Quote
Tried it. A DOS window briefly (very briefly) open and closes. Nothing else happens.

Sorry about that. I have tested this ONE and it works on my Win 2k when opened in Explorer, from a desktop shortcut and from the command line. Please INSERT your path to the files in the Pushd command line.

Code: [Select]@Echo Off
setlocal
cls

pushd "path\to\files\"

for /f "delims=*" %%# in ('dir /tw /o-d /b "status log *"') do (
start notepad %%#& exit

)



Discussion

No Comment Found