递增FOR循环的每一行中的文本文件(批次) [英] Incrementing FOR loop for each line in a text file (batch)

查看:210
本文介绍了递增FOR循环的每一行中的文本文件(批次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,以前我说明我的问题,让我为你提供一些背景:

我创造了我的朋友们的消息服务,我给的消息对方学校。它的工作原理基本上是每个用户开始.bat文件,我把自己的电脑上。然后,两个窗口打开:一个具有一个输入,即文本:,另一个是真的是显示被更新一次的第二文本文件另一个文件。当用户输入文本时,它被连结到文本文件中。这是部分的样子:

 :消息
如果%A%== 1(
回声%计算机%已加入聊天室在%TIME%! >> %PLACE%chatroom.chatfile
设定α= 0

CLS
回声。
回声。
集/ p文本=TEXT
如果文本%%==跳槽转到退出
如果文本%%==清后藤清
如果文本%%==重启转到启动
转到tidyup
:发送
回声%计算机%的%TIME%:%文本%GT;>%到位%chatroom.chatfile
转到消息

和更新文本文件的部分:

 :窗口
CLS
键入%位置%chatroom.chatfile
TIMEOUT / T 1 / NOBREAK> NUL
转到窗口

由于我一起感动,我意识到如果文本文件是超过20行左右,那么你看不到底部。我试过更多命令来解决这个问题,但是这个烦人向下滚动,让你不能真正读什么。

在这之后,我发现了一个pretty很好的解决方案:存储一个单独的文件名为history.txt和主文本文件的第一行移动到这一项上线是更大的原始文本文件的号码的情况超过20个。

同样,我遇到了另一个问题:这不仅能消除的有一个的从文本文件行。比方说,原来的文本文件是25线长。如何将移动这些行?

这是我迄今为止:

 :tidyup
TIMEOUT / T 1 / NOBREAK> NUL
设置文件=%到位%chatroom.chatfile
集/一个CNT = 0
FOR / F %%一个在('键入%文件%^ |寻找/ V / C')就设置/ A CNT = %%一
设置/ p FIRSTLINE =<%文件%
如果CNT%GTR%20(
集/ A CNT2 =%CNT -20%
在(1,1,%CNT%)/升%% g执行(
回声%FIRSTLINE%GT;>%到位%history.chatfile ::要注意这条线

FOR / F跳过=%CNT%delims = *%%一中(%到位%chatroom.chatfile)做(
回声%% A>>%到位%chatroom2.chatfile

XCOPY%到位%chatroom2.chatfile%到​​位%chatroom.chatfile / Y> NUL
德尔%到位%chatroom2.chatfile / F / Q)
转到发送

在这里我把行 ::注意这行是我需要这个问题的回答:

我如何递增循环,因此从原来的文本文件移动线CNT%%量history.chatfile

有没有更简单的完成我在做什么呀?即A腰斩的批处理命令


解决方案

@ECHO OFF
SETLOCAL
设置文件= q27350263.txt
::除去起始$变量
FOR / Fdelims ==%% a。在('设置$ 2 ^>讷')DO SET一%% =
::从文件中读取行到变量$ 1 .. $ asmanylinesasthereare
SET / A CNT = 0
FOR / Fdelims =%%一中(类型文件%%')做(
 集/一个CNT + = 1
 呼叫建立$ %% CNT %% = %%一

SET / A cnthist = CNT-20
SET / A cntlive = cnthist + 1
IF%CNT%GTR 20(
 DEL%文件%
 FOR / L %%一个IN(1,1,%cnthist%)DO CALL>>中的文件%%.history中ECHO(%% $ %%一个%%
 FOR / L %%一个IN(%cntlive%,1%CNT%)DO>>中的文件%%CALL ECHO(%% $ %%一个%%

GOTO:EOF

我用命名包含了一些数据,我的测试文件 q27350263.txt 。所有你需要做的是改变文件名,以适应自己。

从本质上讲,这样做是因为我们都知道算到你chatfile q27350263.txt 的内容加载到变量(或任何)$ 1 .. $器25然后线(在 CNT ),我们可以判断哪些线在历史文件中的土地起来($ 1 .. $(CNT-20)),并返回到聊天文件( $(CNT-20 + 1).. $ CNT)。

呼叫回声语法允许您访问变量的值,因为它的变化。在一审中,一批解析呼叫设置$ %% CNT %% = %% A 并执行设置$%CNT(%)=从行值所以 $ thecurrentvalueofcnt 接收来自当前行的文本。

在第二对实例,%%一个包含许多这样的呼叫回声的目标%$ 1%(其中1 的一个%%值

请注意重定向器可以放置在通话两侧

Ok, before I state my problem, let me provide you with some background:

I am creating a messaging service for my friends and I to message each other at school. How it works is basically each user starts the .bat file I put on their computer. Then, two windows open: one that has an input which reads "Text:" and another which is really another file which is displaying a text file that is updated once a second. When the user inputs text, it is concatenated to the text file. This is what that part looks like:

:message 
if %a%==1 (
echo %computername% has joined the chatroom at %time%! >> %place%chatroom.chatfile
set a=0
)
cls
echo.
echo.
set /p text="Text: "
if "%text%"=="quit" goto quit
if "%text%"=="clear" goto clear
if "%text%"=="restart" goto startup
goto tidyup
:send
echo %computername% at %time%: %text% >>%place%chatroom.chatfile
goto message  

And the updating text file part:

:window
cls
type %place%chatroom.chatfile
TIMEOUT /T 1 /NOBREAK > nul
goto window

As I moved along, I realized if the text file is more than 20 lines or so, then you couldn't see the bottom part. I tried the morecommand to solve this, but this annoyingly scrolls down so that you can't actually read anything.

After that, I found a pretty good solution: Store a separate file called history.txt and move the first line of the main text file to this one on the condition of the original text file's number of lines being greater than 20.

Again, I encountered another problem: This only removes one line from the text file. Let's say the original text file is 25 lines long. How would I move these lines?

This is what I have so far:

:tidyup
TIMEOUT /T 1 /NOBREAK > nul
set file=%place%chatroom.chatfile
set /a cnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a cnt=%%a
set /p firstline=<%file%
if %cnt% GTR 20 (
set /a cnt2=%cnt%-20
for /l %%g in (1, 1, %cnt%) do (
echo %firstline%>>%place%history.chatfile  ::pay attention to this line
)
for /f "skip=%cnt% delims=*" %%a in (%place%chatroom.chatfile) do (
echo %%a >>%place%chatroom2.chatfile    
)
xcopy %place%chatroom2.chatfile %place%chatroom.chatfile /y >nul
del %place%chatroom2.chatfile /f /q)
goto send

The line where I placed ::pay attention to this line is where I need this question answered:

How do I increment the for loop so it moves %cnt% amount of lines from the original text file to history.chatfile

OR

Is there a much simpler way to accomplish what I am doing? I.e. A "cut" command in batch

解决方案

@ECHO OFF
SETLOCAL
SET "file=q27350263.txt"
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
:: read lines from file into variables "$1..$asmanylinesasthereare"
SET /a cnt=0
for /f "delims=" %%a in ('type "%file%"') do (
 set /a cnt+=1
 CALL SET $%%cnt%%=%%a
)
SET /a cnthist=cnt-20
SET /a cntlive=cnthist+1
IF %cnt% gtr 20 (
 DEL "%file%"
 FOR /l %%a IN (1,1,%cnthist%) DO CALL >>"%file%.history" ECHO(%%$%%a%%
 FOR /l %%a IN (%cntlive%,1,%cnt%) DO >>"%file%" CALL ECHO(%%$%%a%%
)


GOTO :EOF

I used a file named q27350263.txt containing some data for my testing. All you'd need do is change the filenames to suit yourself.

Essentially, what this does is to load the contents of your chatfile q27350263.txt into variables $1..$25 (or whatever) then since we are aware of the count of lines (in cnt) we can determine which lines land up in the history file ($1..$(cnt-20)) and which go back into the chat file ($(cnt-20+1)..$cnt).

The call echo syntax allows you to access the value of the variable as it changes. In the first instance, batch parses CALL SET $%%cnt%%=%%a and executes set $%cnt%=value from line so $thecurrentvalueofcnt receives the text from the current line.

In the second pair of instances, %%a contains a number so the call echo's target is %$1% (where 1 is the value of %%a)

Note that the redirector can be placed either side of the call

这篇关于递增FOR循环的每一行中的文本文件(批次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆