中国DOS联盟论坛

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-06 10:13
48,039 topics / 350,124 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » How to get a substring from a string?
Printable Version  1,743 / 15
Floor1 biwier Posted 2007-11-01 12:07
初级用户 Posts 34 Credits 98
I used
for /F "usebackq delims=:" %%i in (`date /T`) do set ddtmp=%%i:~-10%
but failed to get month-day-year, very depressing
It seems that ~-10% doesn't work here
Floor2 abcd Posted 2007-11-01 12:25
银牌会员 Posts 739 Credits 1,436
OP, what does your date output format look like??
Floor3 dikex Posted 2007-11-01 13:14
高级用户 Posts 366 Credits 788
Try this

Floor4 biwier Posted 2007-11-01 16:09
初级用户 Posts 34 Credits 98
Floor5 biwier Posted 2007-11-01 16:10
初级用户 Posts 34 Credits 98
Originally posted by dikex at 2007-11-1 13:14:
Try this



This is too complicated...
Your environment isn't plain ms-dos, right? It won't run under my dos
%date "...."
Floor6 abcd Posted 2007-11-01 16:24
银牌会员 Posts 739 Credits 1,436
Floor7 biwier Posted 2007-11-01 16:32
初级用户 Posts 34 Credits 98
Thanks abcd

But when I put it into a batch file it seems not to work...
for /F "usebackq delims= " %%i in (`echo %date:* =%`) do set ddtmp=%%i
Feels like my original method should also work, maybe it needs a cmd /d command extensions delay something like that




By the way, could you explain what syntax this is? How can I find explanations of this kind of syntax??
Floor8 abcd Posted 2007-11-01 16:37
银牌会员 Posts 739 Credits 1,436
What I mean is there's no need for for, just echo it directly. You can directly use
%date:~6% as a variable.
Floor9 biwier Posted 2007-11-01 16:55
初级用户 Posts 34 Credits 98
Yeah, under some consoles it works, but under some it shows %i, still don't know the reason for now
Floor10 abcd Posted 2007-11-01 17:01
银牌会员 Posts 739 Credits 1,436
If using for

for /f "tokens=1*" %%i in ('date /t') do @echo %%j
Floor11 biwier Posted 2007-11-01 17:53
初级用户 Posts 34 Credits 98
Originally posted by abcd at 2007-11-1 17:01:
If using for

for /f "tokens=1*" %%i in ('date /t') do @echo %%j




I can do this step too
But if I replace echo with set xx=%j:~-10%
it seems it won't work
I'll try again, thanks
Floor12 abcd Posted 2007-11-01 18:02
银牌会员 Posts 739 Credits 1,436
Get this straight

Variable %%j and variable %j% are not the same

%%j can only be used inside for, and it can only be expanded, not sliced

And %j% can only be sliced or replaced, but cannot be expanded
Floor13 biwier Posted 2007-11-01 19:06
初级用户 Posts 34 Credits 98
That was a real enlightenment!!! 3x
Floor14 dikex Posted 2007-11-01 19:25
高级用户 Posts 366 Credits 788
Originally posted by biwier at 2007-11-1 04:10 PM:


This is too complicated...
Your environment isn't plain ms-dos, right? It won't run under my dos
%date "...."


ms-dos? dos?
As I recall, for in those doesn't have the /f parameter, so yours can't be ms-dos;
cmd in 2000, xp, etc. is the command prompt, not dos, and it's very different from dos.

And my runtime environment is xp sp2 pro;

The default date format is: 2007-11-01 Thursday
After changing the date format it becomes: Thu 2007-11-01

Both date formats can be handled in the command line and in batch files through
Floor15 RocheChild Posted 2007-11-01 22:35
初级用户 Posts 44 Credits 110
Originally posted by dikex at 2007-11-1 07:25 PM:


ms-dos? dos?
As I recall, for in those doesn't have the /f parameter, so yours can't be ms-dos;
cmd in 2000, xp, etc. is the command prompt, not dos, and it's very different from dos.

And my run ...



I tested it on my machine at home, yours does work. At the company I had written it in a script and tested it on an English edition OS.
But writing three statements like that is way too complicated.... an expert really thinks of everything ^_^


Rochechild=biwier
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023