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-06-29 16:19
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » [Question again] How can I get the filename under a folder when part of the name changes? View 959 Replies 10
Original Poster Posted 2005-12-08 18:40 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
I have a situation like this:
in the contact directory there will be a file called contact051107.xls, which is a phone list
but it gets updated from time to time, for example changing to contact051120.xls
I want to use Excel to open this file
each time I enter this directory, get the full name of the file whose fixed beginning is "contact"
and assign it to a variable, for example openit
then "%programfiles%\Microsoft Office\Excel.exe" %openit%
If I don't use external commands and only use commands from the command line, how can I do this?

My runtime environment is 2000 or XP
Please give me some pointers, I would be extremely grateful!

[ Last edited by pillow on 2006-1-4 at 11:38 ]
Floor 2 Posted 2005-12-09 20:36 ·  中国 广东 广州 番禺区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
If there really is only one XLS file, one command will do it:

for %a in (contact*.xls) do start %a

If you write this command into a batch file, just make a tiny change:
@for %%a in (contact*.xls) do start %%a

[ Last edited by JonePeng on 2005-12-9 at 20:38 ]
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 3 Posted 2005-12-10 12:10 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Jone's method is very good, no need for third-party tools, and I don't have any better way either.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 4 Posted 2005-12-10 17:53 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
Long time no see, brother Climbing! Thanks for the compliment.
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 5 Posted 2005-12-11 22:22 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re pillow:

If there are multiple files, you need to use for /f. Below is the code for setting a variable from the command line:

for /f "delims=" %f in ('dir contact*.xls /b /od') do set openit=%f

Of course, like JonePeng did, you can also directly open the corresponding file, but it needs to be written into a batch file:

for /f "delims=" %%f in ('dir contact*.xls /b /o-d') do start %%f & goto:EOF

The above code has not been tested; please debug it and make sure it is correct before using it cautiously
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2005-12-12 11:12 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Wow! Brilliant!!
Before this I knew almost nothing about FOR, and this problem had already been bothering me for several days. Thank you all so much!
Learned a new thing again!
Floor 7 Posted 2005-12-26 17:45 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Looks like I still didn't completely understand this problem. When I was using it recently, a new problem came up again.
What does the EOF in goto:EOF mean? I don't understand this...

[ Last edited by pillow on 2006-1-4 at 11:42 ]
Floor 8 Posted 2006-01-04 11:38 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
New problem: since on the LAN multiple people may open this file at the same time, when the second, third, or Nth person opens this XLS file, it will prompt that the file has already been opened by another user, and ask whether to view it as read-only or open it later.
So now I want to copy this XLS file directly to the user's desktop, and then open the file on the desktop.
Right now I can use the method JonePeng provided in reply #2 to COPY it to the desktop,
so the question is, can I directly get the name of this file and then start it at the same time as copying it?
If CMD can't do it, then can I add some other small program that can run under 2000 and XP?
If I have to DIR the newest file again and then use for token or something like that, it's really too troublesome.

Question 2: in a for statement, can I use a form like do (………………) & (…………) like this?

[ Last edited by pillow on 2006-1-4 at 15:18 ]
Floor 9 Posted 2006-01-04 15:22 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
I found a method myself:
first for %%a in (contact*.xls) do set name=%%a
then copy %name% to the target directory
Are there any other better methods? For example using & in that way?
Floor 10 Posted 2006-01-04 16:11 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re pillow:

for does allow syntax like do (statement1) & (statement2) & ... ; the second line of code in my post #5 is a similar implementation.

Are there any other better methods? For example using & in that way?


Below is still a batch file solution. Save it into a batch file and place it under the contact*.xls directory, then click to run it and it will open the corresponding Excel document.

for /f "delims=" %%f in ('dir contact*.xls /b /o-n') do copy "%%f" "dest_path\." && start "dest_path\%%~nxf" && goto:EOF

Or put the batch file in each user's target directory, with the batch file contents as:

for /f "delims=" %%f in ('dir src_path\contact*.xls /b /o-n') do copy "%%f" && start "%%~nxf" && goto:EOF

As for goto:EOF, :EOF is the default ending label of a batch file. goto:EOF means that after the for loop executes once, finds the newest file and processes it, it jumps out directly.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 11 Posted 2006-01-04 17:40 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Oh, I see... got it. Use two &'s. I had mistakenly thought it had to be in the form of () & () before. Thanks!

In your new method, you used extended variables for the filename, right? I've got a new idea now! Thanks!
Forum Jump: