批处理文件来随意删除一个文本文件的一半行? [英] Batch file to delete half the lines of a text file at random?

查看:715
本文介绍了批处理文件来随意删除一个文本文件的一半行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来使用批处理来看看一个文本文件中的每一行,并删除一半的行中的文本文件,选择哪些行删除随机的。

I need a way to use batch to look at every line of a text file, and delete half of the lines in that text file, choosing which lines to delete at random.

这是模拟D&放大器的比赛之内的比赛; d。所有我需要的是紧缩了每一轮锦标赛的优胜者的一种方式。我可以很容易地使那份文本文件并将其更名为每一轮的批处理文件,但它是由我不擅长与半部分的减少。

This is to simulate a tournament within a game of D&D. All I need is a way to crunch out the winners of each tourney round. I can easily make a batch file that copies the text file and renames it for each round, but it's the reduce by half part that I'm not good with.

编辑:我想一个批处理文件来做到这一点,因为它会采取办法不多的时间用手工在表做。另外游戏中的非常古老和角色扮演的重点,所以在锦标赛NPC角色的实际列表和PC会想知道他们的朋友在竞争中如何表现。

I want a batch file to do this because it would take way to much time to do by hand at the table. Also the game's very old and roleplay focused, so there is an actual list of NPC characters in the tourney and the PCs will want to know how their friends fared in the competition.

推荐答案

在这个问题中指定的设计是有缺陷的。你可以不是随机删除一半的线,因为这样对于任何给定的游戏,你可能最终有2名或没有赢家。输入文件必须有指定结构参赛者打对方,然后一个赢家必须从每个竞赛中随机抽取。

The design specified in the question is flawed. You cannot randomly delete half the lines because then for any given game, you might end up with 2 winners, or no winners. The input file must have a structure that specifies which contestants play each other, and then a winner must be randomly selected from each contest.

溶液下面假定每行重新presents一个播放器,并且对于每个圆,第1行戏剧线路2,线路3戏剧线4等的行数必须始终是2> = 2的幂(2,4,8,16,32,64,...)

The solution below assumes each line represents a player, and for each round, line 1 plays line 2, line 3 plays line 4, etc. The number of lines must always be a power of 2 >=2 (2, 4, 8, 16, 32, 64, ...)

@echo off
setlocal enableDelayedExpansion
set "file=tournament.txt"
for /f %%C in ('find /c /v "" ^<"%file%"') do (
  for /l %%N in (1 2 %%C) do (
    set /p "p1="
    set /p "p2="
    set /a "1/(!random!%%2)" 2>nul&&echo !p1!||echo !p2!
  )
) <"%file%" >"%file%.new"
move /y "%file%.new" "%file%" >nul

外环计数的行数。内环从1数奇数来计数,所以会完全遍历(线数除以2)次。内环已输入重定向到源文件,并输出重定向到一个临时文件中。

The outer loop counts the number of lines. The inner loop counts the odd numbers from 1 to the count, so it iterates exactly (line count divided by 2) times. The inner loop has input redirected to the source file, and the output redirected to a temporary file.

每个内循环迭代重新presents在比赛游戏。 SET / P用于向玩家姓名装入变量。然后1由随机数模2,这将导致一个除以0错误的50%的时间划分。错误消息重定向到NUL,然后条件运算用于打印的一个或另一个玩家的输出文件中。

Each inner loop iteration represents a game in the tournament. SET /P is used to load the player names into variables. Then 1 is divided by a random number modulo 2, which will result in a divide by 0 error 50% of the time. The error message is redirected to nul, and then conditional operators are used to print one or the other player to the output file.

一旦循环结束后,将临时文件被移动到替换原始文件

Upon completion of the loop, the temporary file is moved to replace the original file.

这篇关于批处理文件来随意删除一个文本文件的一半行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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