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-03 22:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read the values in a txt file and assign them to variables? View 2,080 Replies 19
Original Poster Posted 2007-03-22 00:47 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
How can I read the numbers from a txt file in cmd and assign them to variables?

Seeking advice, thank you
Floor 2 Posted 2007-03-22 02:10 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
This depends on the format of your TXT file.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2007-03-22 02:11 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
How to say?

There is only one line of numbers in this txt file
Floor 4 Posted 2007-03-22 03:57 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
For example.
It needs to be analyzed specifically for specific problems.
Windows 一键还原
http://www.yjhy.com
Floor 5 Posted 2007-03-22 04:08 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
I want to let cmd judge the date and execute a specific command after the specified date.

My cmd will judge whether tmp.txt exists when it runs for the first time. If not, it will generate a number (such as 20070320) of the current date and save it in this tmp.txt file. Then every time cmd runs, it will judge whether a certain date (such as 15 days) has passed. If it is greater than 15 days, it will prompt "XXXXX!". My code is as follows

@Echo Off
set tm1=%date:~0,4%
set tm2=%date:~5,2%
set tm3=%date:~8,2%
set /a number=%tm1%*365+%tm2%*30+%tm3%
if not exist %windir%:\tmp.txt echo %number%>%windir%:\tmp.txt & goto end
goto begin

:begin
if .....Here it involves how to read the value in the txt and compare it with the number converted from the current date, I don't know.

:end

Ask the expert, thank you
Floor 6 Posted 2007-03-22 05:53 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
Give it a bump, looking forward to it......
Floor 7 Posted 2007-03-22 23:44 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
Or there are other ways to achieve this purpose, that's fine too
Floor 8 Posted 2007-03-23 00:47 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
>txt
set/p

or

for ... do (set ...)
Floor 9 Posted 2007-03-23 01:37 ·  中国 陕西 西安 电信
初级用户
Credits 120
Posts 45
Joined 2007-03-13 05:57
19-year member
UID 81568
Gender Male
Status Offline
for /f "usebackq tokens=*" %i in ("%windir%\tem.txt") do ..

You can use this method to read the numbers from the txt.
test
Floor 10 Posted 2007-03-23 01:49 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
```
@echo off
set/p St=<tmp.txt
echo %St%
pause&exit/b
```
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
tianlijian +2 2007-03-23 02:01
hy433124shc +2 2007-03-27 03:41
oilio +2 2007-03-29 03:10
Floor 11 Posted 2007-03-24 00:52 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
Thanks, but can you explain what it means?

set/p St=<tmp.txt This line
Floor 12 Posted 2007-03-24 01:05 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Assign the first line of the tmp.txt file to the St variable.
Floor 13 Posted 2007-03-24 01:34 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
Thanks to the brother upstairs!!! But I have ransacked the help documentation of set and can't find this usage.
Floor 14 Posted 2007-03-24 01:36 ·  中国 江苏 扬州 电信
中级用户
Credits 203
Posts 44
Joined 2003-08-14 00:00
22-year member
UID 8600
Gender Male
Status Offline
I'm currently sharing my code to offer some help to brothers in need:

@Echo Off
set tm1=%date:~0,4%
set tm2=%date:~5,2%
set tm3=%date:~8,2%
set /a number=%tm1%*365+%tm2%*30+%tm3%
if not exist c:\tmp.txt echo %number%>c:\tmp.txt & exit
goto begin

:begin
set /p St=<c:\tmp.txt
set /a number2=%number%-%St%
if %number2% gtr 15 echo 你的使用期限已过15天& pause & exit
echo 您还在使用期,请继续使用
pause & exit
Floor 15 Posted 2007-03-24 03:11 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by wjgyz740526 at 2007-3-23 12:34:
Thank you, brother above!!! But I have ransacked the help documentation of set and can't find this usage.


Many usages are explored by people, and it may not be in the help.
Forum Jump: