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-06 09:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to get a substring from a string? View 1,737 Replies 15
Original Poster Posted 2007-11-01 12:07 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
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
Floor 2 Posted 2007-11-01 12:25 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
OP, what does your date output format look like??
Floor 3 Posted 2007-11-01 13:14 ·  中国 广东 广州 白云区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
Try this


(echo+%date:~0,10%|find " ") && set "ddtmp=%date:~-10%" || set "ddtmp=%date:~0,10%"
echo+%ddtmp%
pause
正在潜水修练的批处理小白
Floor 4 Posted 2007-11-01 16:09 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
Floor 5 Posted 2007-11-01 16:10 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
Originally posted by dikex at 2007-11-1 13:14:
Try this


(echo+%date:~0,10%|find " ") && set "ddtmp=%date:~-10%" || set "ddtmp=%date:~0,10%"
echo+%ddtmp%
pause


This is too complicated...
Your environment isn't plain ms-dos, right? It won't run under my dos
%date "...."
Floor 6 Posted 2007-11-01 16:24 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Floor 7 Posted 2007-11-01 16:32 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
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??
Floor 8 Posted 2007-11-01 16:37 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
What I mean is there's no need for for, just echo it directly. You can directly use
%date:~6% as a variable.
Floor 9 Posted 2007-11-01 16:55 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
Yeah, under some consoles it works, but under some it shows %i, still don't know the reason for now
Floor 10 Posted 2007-11-01 17:01 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
If using for

for /f "tokens=1*" %%i in ('date /t') do @echo %%j
Floor 11 Posted 2007-11-01 17:53 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
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
Floor 12 Posted 2007-11-01 18:02 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
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
Floor 13 Posted 2007-11-01 19:06 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 98
Posts 34
Joined 2007-10-16 12:24
18-year member
UID 99881
Gender Male
Status Offline
That was a real enlightenment!!! 3x
Floor 14 Posted 2007-11-01 19:25 ·  中国 广东 广州 白云区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
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
正在潜水修练的批处理小白
Floor 15 Posted 2007-11-01 22:35 ·  中国 上海 东方有线
初级用户
Credits 110
Posts 44
Joined 2007-11-01 21:56
18-year member
UID 101435
Gender Male
Status Offline
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
Forum Jump: