China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 00:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to run the latest executable file in a directory using batch processing? View 3,075 Replies 17
Original Poster Posted 2007-03-13 03:30 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
How to use batch processing to run the latest executable file in a directory? Ignoring subdirectories, without using third-party tools, and the file names have no pattern, ^_^ Let's see how everyone implements it
Floor 2 Posted 2007-03-13 03:36 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Run it in the directory where the file to be executed is located, not tested:

@echo off
for /f "delims=" %%a in ('dir /od/b *.exe') do set name="%%a"
%name%
Floor 3 Posted 2007-03-13 03:42 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
^_^
dir /o:d is correct or dir /o/d
Floor 4 Posted 2007-03-13 03:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by yishanju at 2007-3-12 14:42:
^_^
dir /o:d is correct, or dir /o/d


  The effect is the same, just try it and you'll know.
Floor 5 Posted 2007-03-13 03:49 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
Floor 6 Posted 2007-03-13 03:50 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
^_^What if there are spaces in the file name... What will happen?
Floor 7 Posted 2007-03-13 03:53 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
D:\Documents and Settings\Administrator>%name%
'd' is not an internal or external command, nor is it a runnable program
or batch file.

D:\Documents and Settings\Administrator>
Floor 8 Posted 2007-03-13 03:55 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
for /f "delims=" %%a in ('dir /od/b *.exe') do set name="%%a"
"%name%"
Adding quotes, if there are spaces in the file name, it shouldn't go wrong, right
Floor 9 Posted 2007-03-13 03:59 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
^_^ Think of a problem. If you put this batch file into the directory of the executable program to be run, then this batch file will become the latest file in the directory. This should cause an infinite loop.
Floor 10 Posted 2007-03-13 04:01 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
for /f "delims=" %%a in ('dir /od/b *.exe') do set name="%%a"
%name%
path %name%
start %name%
This might also work, but I haven't tried it.
学无尽止
Floor 11 Posted 2007-03-13 04:04 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by yishanju at 2007-3-12 14:59:
^_^ Remember a question, if you put this batch file into the directory of the executable program to be run, then this batch file becomes the latest file in the directory

This should cause an infinite loop


  Re yishanju:
  
  It should not cause an infinite loop because this batch file searches only for executable files (*.exe), not batch files (*.bat, *.cmd), so what is executed after running cannot be the batch file;

  Adding quotes is necessary to prevent errors when running the executable file due to spaces in the executable file name. I overlooked this.
Floor 12 Posted 2007-03-13 04:10 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
It should not cause an infinite loop because this batch processing only searches for executable files (*.exe), not batch files (*.bat, *.cmd), so what is executed after running cannot be a batch process; ^_^ Well, I removed the .exe when I was thinking about it
Floor 13 Posted 2007-03-13 04:56 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
for /f "delims=" %%a in ('dir /od/b *.exe') do set name="%%a"
start "f:\tem%name%"
It should be added with start like this. Otherwise, the code below may not execute.
Floor 14 Posted 2007-03-13 06:53 ·  中国 广东 广州 海珠区 电信
初级用户
★★
Credits 170
Posts 72
Joined 2007-03-11 22:16
19-year member
UID 81435
Gender Male
Status Offline
The LZ seems to be going to implement intelligent judgment of the latest executable file in the directory.

It's quite interesting. I'll study it when I have time. The early ideas:

Output file information to log.txt (information collection)
Then import log.txt for judgment (which file is the latest executable file) and then execute

I don't know if the idea is correct.
Floor 15 Posted 2007-03-13 07:05 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
dir /t:c /o:d Sort by creation time
dir /t:a /o:d Sort by access time
dir /t:w /o:d Sort by modification time
Good method of lxmxn, I didn't notice it before
Forum Jump: