![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-17 19:39 |
47,814 topics / 349,907 posts / today 4 new / 48,259 members |
| DOS疑难解答 & 问题讨论 (解答室) » [Question again] How can I get the filename under a folder when part of the name changes? |
| Printable Version 1,019 / 10 |
| Floor1 pillow | Posted 2005-12-08 18:40 |
| 初级用户 Posts 82 Credits 196 | |
|
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 ] |
|
| Floor2 JonePeng | Posted 2005-12-09 20:36 |
| 金牌会员 Posts 1,883 Credits 4,562 From 广东广州 | |
|
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 ] |
|
| Floor3 Climbing | Posted 2005-12-10 12:10 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
Jone's method is very good, no need for third-party tools, and I don't have any better way either.
|
|
| Floor4 JonePeng | Posted 2005-12-10 17:53 |
| 金牌会员 Posts 1,883 Credits 4,562 From 广东广州 | |
|
Long time no see, brother Climbing! Thanks for the compliment.
![]() |
|
| Floor5 willsort | Posted 2005-12-11 22:22 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 |
|
| Floor6 pillow | Posted 2005-12-12 11:12 |
| 初级用户 Posts 82 Credits 196 | |
|
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! |
|
| Floor7 pillow | Posted 2005-12-26 17:45 |
| 初级用户 Posts 82 Credits 196 | |
|
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 ] |
|
| Floor8 pillow | Posted 2006-01-04 11:38 |
| 初级用户 Posts 82 Credits 196 | |
|
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 ] |
|
| Floor9 pillow | Posted 2006-01-04 15:22 |
| 初级用户 Posts 82 Credits 196 | |
|
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? |
|
| Floor10 willsort | Posted 2006-01-04 16:11 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor11 pillow | Posted 2006-01-04 17:40 |
| 初级用户 Posts 82 Credits 196 | |
|
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! |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |