|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
『楼 主』:
提取文件逐行內容,進行運算 問題! 麻煩高手解答.!
使用 LLM 解释/回答一下
如何提取一下文件中的 每一行中的 一欄字符並進行如下算法.
(函shu down time 的行累加,) 減去 (函 boot star time的行累加)
如54883+54891+55849-54877-54887-55843=?
for /f "delims=! tokens=2" %%i in (1.txt) do(??????????)
以下是文件.1.txt!
boot star time 2006-06-06 星期二 15:14:37.57 record! 54877
shut down time 2006-06-06 星期二 15:14:43.27 record! 54883
boot star time 2006-06-06 星期二 15:14:47.66 record! 54887
shut down time 2006-06-06 星期二 15:14:51.36 record! 54891
boot star time 2006-06-06 星期二 15:30:43.18 record! 55843
shut down time 2006-06-06 星期二 15:30:49.99 record! 55849
How to extract a column of characters from each line in the file and perform the following algorithm. (Sum of lines of function shu down time) minus (Sum of lines of boot star time) such as 54883 + 54891 + 55849 - 54877 - 54887 - 55843 =?
for /f "delims=! tokens=2" %%i in (1.txt) do(??????????)
The following is file.1.txt!
boot star time 2006-06-06 星期二 15:14:37.57 record! 54877
shut down time 2006-06-06 星期二 15:14:43.27 record! 54883
boot star time 2006-06-06 星期二 15:14:47.66 record! 54887
shut down time 2006-06-06 星期二 15:14:51.36 record! 54891
boot star time 2006-06-06 星期二 15:30:43.18 record! 55843
shut down time 2006-06-06 星期二 15:30:49.99 record! 55849
|
|
2006-6-7 09:31 |
|
|
doscc
中级用户
  
积分 256
发帖 93
注册 2006-3-26 来自 广东
状态 离线
|
『第 2 楼』:
试试看! 在xp下试验成功!
使用 LLM 解释/回答一下
boot star time 是单行
shut down time 是双行
那下面代码就可实现楼主的要求.
@echo off
set n=0
set sum=0
for /f "delims=! tokens=2" %%i in (1.txt) do (
call :s %%i
)
echo %sum%
goto :EOF
:s
set /A n+=1
if "%n%"=="1" (set /A sum-=%1)else set /A sum+=%1 & set n=0
goto :EOF
Last edited by doscc on 2006-6-7 at 12:15 ]
boot star time is a single line
shut down time is a double line
Then the following code can achieve the requirements of the landlord.
@echo off
set n=0
set sum=0
for /f "delims=! tokens=2" %%i in (1.txt) do (
call :s %%i
)
echo %sum%
goto :EOF
:s
set /A n+=1
if "%n%"=="1" (set /A sum-=%1)else set /A sum+=%1 & set n=0
goto :EOF
Last edited by doscc on 2006-6-7 at 12:15 ]
|
|
2006-6-7 12:12 |
|
|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
 『第 3 楼』:
使用 LLM 解释/回答一下
謝謝.!
又學到一點東西.!
GOTO:EOF就是返回到哪個FOR語句吧.!
%1就是繼承FOR語句的%i把!.
如果我想實現兩個for語句的牽套.實現吧如題的for /f "delims=! tokens=2" %%i in (1.txt) do(......)
分別定義給set1~set6這幾個變量.!例如(set1=54877 ,set2=54883
set3=54887......)
本人想看看兩個for語句的變量是怎麼傳遞的.!
謝謝
Thanks!
Learned another thing again.!
GOTO:EOF is to return to which FOR statement, right.!
%1 is to inherit the %i of the FOR statement, right!.
If I want to implement the nesting of two for statements, to achieve, for example, for /f "delims=! tokens=2" %%i in (1.txt) do(......)
Define these variables to set1~set6 respectively. For example (set1=54877 ,set2=54883
set3=54887......)
I want to see how the variables of the two for statements are passed.!
Thanks
|
|
2006-6-7 17:13 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
应该可以更加简洁,如下代码:
@echo off&setlocal enabledelayedexpansion
set /a l=0
for /f "tokens=2 delims=!" %%i in (1.txt) do (
set /a l=%%i+!l!
)
echo %l%
It should be able to be more concise. The following code:
@echo off&setlocal enabledelayedexpansion
set /a l=0
for /f "tokens=2 delims=!" %%i in (1.txt) do (
set /a l=%%i+!l!
)
echo %l%
|
|
2006-6-7 17:30 |
|
|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
re : bagpipe
set /a l=%%i+!l! 是將六個數累加.!與我的題議不符合.!
re : bagpipe
set /a l=%%i+!l! is to accumulate six numbers. This does not match my proposal.
|
|
2006-6-7 17:42 |
|
|
bagpipe
银牌会员
     DOS联盟捡破烂的
积分 1144
发帖 425
注册 2005-10-20 来自 北京
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
@echo off&setlocal enabledelayedexpansion
set /a l=0
for /f "tokens=2 delims=!" %%i in (1.txt) do (
set /a n+=1
if !n!==1 (
set /a l=!l!-%%i
) else (
set /a l=!l!+%%i&set n=0
)
)
echo %l%
流泪而走.........
@echo off&setlocal enabledelayedexpansion
set /a l=0
for /f "tokens=2 delims=!" %%i in (1.txt) do (
set /a n+=1
if !n!==1 (
set /a l=!l!-%%i
) else (
set /a l=!l!+%%i&set n=0
)
)
echo %l%
Weeping and leaving.........
|
|
2006-6-8 10:23 |
|
|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
|
2006-6-8 11:26 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Re piziliu2004(3楼):
实际上你的命题没有必要也很难用嵌套循环来实现,而循环之间的变量传递也不是很复杂,基本上可以说,在内层循环中可以直接应用外层循环的变量,只要它没有被内层循环的变量所覆盖,这取决于变量的命名。比如:
for /l %%i in (1,1,5) do for %%j in (a,b,c,d) do echo.Counter:%%i%%j
至于你提到的将文件内容逐行获取到环境变量,感觉没有太大意义,因为通常我们都是在获取某一行内容后即时进行各种处理后输出。不过仍然给出示例代码以供参考。
@echo off & setlocal EnableDelayedExpansion
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
set set!c!=%%i
)
set set
Re: piziliu2004 (Floor 3):
Actually, your proposition doesn't need and is difficult to implement with nested loops, and the variable transfer between loops is not very complicated. Basically, it can be said that in the inner loop, you can directly use the variables of the outer loop, as long as it is not overwritten by the variables of the inner loop, which depends on the naming of the variables. For example:
for /l %%i in (1,1,5) do for %%j in (a,b,c,d) do echo.Counter:%%i%%j
As for what you mentioned about getting the file content line by line into environment variables, it doesn't seem to make much sense, because usually we all get a line of content and then immediately perform various processing and output it. But still, sample code is given for reference.
@echo off & setlocal EnableDelayedExpansion
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
set set!c!=%%i
)
set set
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-8 11:59 |
|
|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
謝謝willsort
版主.
小弟想問幾個問題
1: .set set 是顯示當前所有設定的變量麼?.那位什麼我rem 掉"set set"會設定變量失敗呢?
2: 還有位什麼我在set set!c!=%%i後插入"echo %set!c!%"顯示失敗呢?
Thanks willsort
Moderator.
Little brother wants to ask a few questions
1:.set set is to display all current set variables? Why does my rem "set set" fail to set variables?
2: Also, why does inserting "echo %set!c!%" after set set!c!=%%i display a failure?
|
|
2006-6-8 13:44 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-6-8 14:07 |
|
|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
還是有點迷糊.
1: set set 会显示%set%的值! 那麼僅僅顯示%set%而已為什麼掉"set set"整個批處理會沒辦法實現設置變量的功能呢?.
2:set set!c!=%%i後不能有其他命令,否则都会成为变量!是為什麼?
難道是!c!的問題?.
3:順便問一下.!c!為什麼不能用%c%代替呢?
Still a bit confused.
1: "set set" will display the value of %set%! Then why does just displaying %set% make the entire batch script unable to achieve the function of setting variables when using "set set"?
2: After "set set!c!=%%i", there cannot be other commands, otherwise they will all become variables! Why is that? Is it because of!c!?
3: By the way, why can't!c! be replaced with %c%?
|
|
2006-6-8 14:40 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-6-8 14:55 |
|
|
piziliu2004
中级用户
   过度热情
积分 321
发帖 139
注册 2006-3-21
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
re:qwe1234567
版主 謝謝.! 是我誤解了.
我是這麼判斷的. rem set set--> 批處理執行完返回命令行後 ,--> 我執行"set"卻看不到set1~setn這些變量.
經過驗證原來我不去掉set set. 我執行set我同樣看不到set1~setn這些變量.
1: 為什麼看不到設定的set1~n變量. 是批處理setlocal EnableDelayedExpansion再作怪麼?
2: 如果我想讓批處理結束後. 返回dos命令下執行set仍然可以看到我設置的變量怎麼實現呢?
re:qwe1234567
Moderator, thank you! I misunderstood.
This is how I judged. rem set set--> After the batch processing is executed and returns to the command line, ,--> I execute "set" but can't see the set1~setn variables.
After verification, it turns out that if I don't remove set set. I execute set and I still can't see the set1~setn variables.
1: Why can't I see the set1~n variables I set. Is it because of the batch processing setlocal EnableDelayedExpansion?
2: If I want the batch processing to end and then execute set under the dos command and still see the variables I set, how to achieve it?
|
|
2006-6-8 15:12 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
───────────────── 版务记录 ─────────────────
执行:Will Sort
操作:删除主题:《21098 - 如果我想實現兩個for語句的嵌套傳遞變量.實現如題 》
说明:原主题与现主题部分内容重复
处罚:扣除因发表该主题而奖励的6点积分,扣除主题违规惩罚性2点积分
───────────────── 版务记录 ─────────────────
Re piziliu2004:
!c!是变量延迟扩展的用法,在本论坛已有多次解释,建议你先搜索一下,另外可以参考 cmd /? 的命令行帮助信息。
set1~n失效确实是setlocal的原因,具体细节,请参考setlocal /?以及Windows的帮助和支持中心的相关信息。
通常,我们不需要将批处理内的变量修改在批处理结束以后仍然保留,尤其是多个变量的情况,如果你有这方面的需要,则说明程序的结构设计存在一定的缺陷。当然,将变量修改保持到批处理以外是可以的,最简单的方法是不使用setlocal命令,如果必须要使用它,比如用它开启变量延迟扩展特性,则需要使用另外一个技巧。
这个技巧我很少用到,只有在难以避免使用setlocal且必须保存变量供给外部程序引用时才会使用。方法是使用利用率很低的endlocal命令。例如,在程序结束前想要保留变量envar的修改可以在程序末尾插入以下语句。
endlocal & set envar=%envar%
对于8楼的代码,则相对更为复杂一些,因为需要保留多个动态名的变量。可以使用以下的代码。
@echo off & setlocal EnableDelayedExpansion
if exist _setvar.bat del _setvar.bat
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
echo set set!c!=%%i>>_setvar.bat
)
endlocal & if exist _setvar.bat (call _setvar.bat & del _setvar.bat)
set set
Last edited by willsort on 2006-6-8 at 20:44 ]
───────────────── Moderation Record ─────────────────
Performed by: Will Sort
Operation: Delete topic: "21098 - If I want to implement the nested transfer of variables between two for statements. As the title says"
Explanation: The original topic and part of the content of the current topic are repeated
Punishment: Deduct 6 points of points awarded for posting this topic, deduct 2 points of points for the topic violation penalty
───────────────── Moderation Record ─────────────────
Re piziliu2004:
!c! is the usage of variable delayed expansion. There have been multiple explanations in this forum. It is recommended that you search first. In addition, you can refer to the command line help information of cmd /?.
The failure of set1~n is indeed due to setlocal. For specific details, please refer to setlocal /? and the relevant information in Windows Help and Support Center.
Usually, we don't need to keep the variable modifications in the batch file after the batch file ends, especially in the case of multiple variables. If you have such a need, it means that there is a certain defect in the program structure design. Of course, it is possible to keep variable modifications outside the batch file. The simplest way is not to use the setlocal command. If you must use it, such as using it to enable the variable delayed expansion feature, you need to use another trick.
I rarely use this trick. It is only used when it is difficult to avoid using setlocal and variables must be saved for external program reference. The method is to use the rarely used endlocal command. For example, if you want to keep the modification of variable envar before the program ends, you can insert the following statement at the end of the program.
endlocal & set envar=%envar%
For the code on floor 8, it is relatively more complicated because multiple variables with dynamic names need to be retained. The following code can be used.
@echo off & setlocal EnableDelayedExpansion
if exist _setvar.bat del _setvar.bat
for /f "tokens=8" %%i in (1.txt) do (
set /a c+=1
echo set set!c!=%%i>>_setvar.bat
)
endlocal & if exist _setvar.bat (call _setvar.bat & del _setvar.bat)
set set
Last edited by willsort on 2006-6-8 at 20:44 ]
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-8 20:40 |
|
|
wang6610
银牌会员
    
积分 1246
发帖 488
注册 2003-11-11
状态 离线
|
|
2006-6-10 15:28 |
|