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-24 05:12
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read the first letter of each line and execute this command. View 5,248 Replies 17
Original Poster Posted 2006-11-06 17:54 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
The content of 1.txt is as follows: (one letter per line)

c
h
k
d
s
k

I want to combine them to execute the chkdsk command~~ I don't know how to write it.

[ Last edited by namejm on 2006-11-6 at 09:08 PM ]
Floor 2 Posted 2006-11-06 22:07 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally, I wanted to communicate with the LZ about the questions raised above (very interesting), but after thinking for a long time, I didn't know how to answer : )
Now I don't even know whether to consider it as purely DOS discussion or the CMD Shell of the XP system that everyone uses...

After I searched most of the LZ's posts in the forum search, I determined that this question is about non-pure DOS issues, haha...

  Problem about errorlevel return value~`
  Problem about %random% in DOS batch processing
  Usage of sc command in DOS batch processing
  What does ><nul mean??
  A question? By the way, apprenticeship~```
  Who can specifically explain the for /f command for me?
  Please, expert, help me compile a batch processing for batch-renaming filenames...
  ……



Seeing that there are problems about <> and for /f reading files and other problems mentioned by the LZ,
Then are these interesting problems supposed to be interesting solutions?

The interesting solution is as follows, it is the way of "writing" itself to read chkdsk and then directly run automatically.
For fun, so it can only run correctly once, haha...
But the LZ's wanted those problem characteristics and redirection and reading files are all used up : )
Also, the LZ may be interested in how not to write redundant carriage return characters at the end... is also used up : )

The following example is only for fun, not practical : )

@for /f %%. in (1.txt) do @set /p=%%.<nul>>"%~f0"
@,


The @, in the last line has a reason, it is to prevent you from directly saving the first line and not giving a line feed,
So, @, is that when encountering the following DOS command can also execute normally,
But this interesting method can only execute normally once~ : D



The "%~f0" used above is the full name of the batch processing's own drive letter path filename, this information can find more messages in the help information of for /? : )
Using Del "%~f0" can also let the batch processing "commit suicide", that is, delete itself~

Self (batch processing itself) writing new information to itself can also be used for special applications such as simple running times or running logs...

The >>"%~f0" used in the above batch processing is appending the output information to cover the target file content's situation to append to the target file content's behind.
And why does "%~f0" want to add %~f0 (that is, batch processing oneself's entire path and other information) with double quotes? It is because if you run where there are spaces (such as desktop),
Then in order to cause the system wrong judgment middle spaces...
For example: CD ABC 123 is not getting into "ABC 123" this directory, to add double quotes the system just knows this is integral,
So the above batch processing oneself information's expression要用 double quotes enclose : )


set /p=%%.<nul is to remove that your 1.txt all took carriage return line feed characters of "c h k d s k",
As for why it can "remove" carriage return and line feed symbols, there is a allusion inside, hee hee...

Need to appreciate the following wonderful article like reading a story:
----------------------------------------------------------------------------------------------------
Brief Analysis of Helpless Signature Code
http://www.cn-dos.net/forum/viewthread.php?tid=18838&fpage=1&highlight=%E7%AD%BE%E5%90%8D
----------------------------------------------------------------------------------------------------
After reading the above article, the LZ can make more interesting things~ : )
(If the friend who hasn't read this article, please put down any thing in hand, go to see "Brief Analysis of Helpless Signature Code" now)
(The most useless is collection, the most useful is to use go to see go to study go to do once to replace the action of collecting uselessly)

[ Last edited by redtek on 2006-11-6 at 10:38 PM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-11-06 22:19 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
Floor 4 Posted 2006-11-06 23:22 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Amazing! Interesting!
Floor 5 Posted 2006-11-07 01:38 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The principle has been posted by redtek, so I won't say more. Here's a piece of code that can get the correct result when executed multiple times:

@echo off
setlocal enabledelayedexpansion
for /f %%i in (1.txt) do set var=!var!%%i
echo %var%
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-07 22:53
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-11-07 01:45 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Great~~ quite interesting~~
Floor 7 Posted 2006-11-07 08:11 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
Thanks a lot to the two great experts redtek and namejm. The problem has been successfully solved, but I don't understand the code very much. I'm continuing to study...
Floor 8 Posted 2006-11-07 08:56 ·  中国 广东 河源 电信
中级用户
★★
Credits 266
Posts 98
Joined 2006-04-21 20:29
20-year member
UID 54223
Gender Male
Status Offline
set var=!var!%%i
Set a variable var, and its value is!var!(which is itself.) %%i is read from the text.
!var! is empty at the beginning. So in the first time of the for loop, it is var=c. Then it loops again.. At this time, var=C plus h, which is var=ch..
In the end, it becomes chkdsk...
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-07 09:30
纵是千年万年 亦难以忘记
Floor 9 Posted 2006-11-07 08:58 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I don't know if the first letter has any special characters
Floor 10 Posted 2006-11-08 09:49 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
Thanks to the explanation from user 8...

Little brother has another question...
If the content in the text is not as simple as the above, but I want to specify and extract the content inside to display, still like the above example to extract chkdsk to one line (the first two lines and the last line are deliberately written more)

yyyyy
bbbb
abca
ghdk
jkl
cde
sss
kkmm
hhhhyy

I wonder if there is a way?
Floor 11 Posted 2006-11-08 11:28 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
May I ask, what is the regularity of your text content? And what is the regularity of the content to be extracted? Your example in 10F is completely confusing.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-11-08 12:42 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

Re namejm:
Actually, he still wants to extract chkdsk, but the first two lines and the last line are deliberately confusing you, heh heh``
Floor 13 Posted 2006-11-08 13:26 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
lxmxn is right, just want to extract chkdsk from the example of 10F.. (The first two lines and the last line are written intentionally more)
Floor 14 Posted 2006-11-08 13:51 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  But let me tell you, this is completely meaningless. Because the letters in the line where the letters to be extracted are located have no pattern at all. If you want to test the "encryption" effect, you can place each character in a command at the same position in each line. For example, the following lines can be used as the text to extract the "ping" command.

-----------------------------------
abcpdafd
wdeiaedlafjklsjfeil
efanfeildjf
sajglielajgfia
-----------------------------------
  Extract the fourth character of each line.
Floor 15 Posted 2006-11-08 13:52 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
It's better to just chkdsk.

Or just one line: abcdefghijklmnopqrstuvwxyz
Then extract with %N:~n,1%
Forum Jump: