如何在批处理中进行循环?

发布于 2024-10-31 03:16:54 字数 210 浏览 4 评论 0原文

我想创建类似这个

dup.bat infile outfile times

示例的用法,

dup.bat a.txt a5.txt 5

它会创建文件 a5.txt,其中 a.txt 的内容重复 5 次

,但是我不知道如何批量执行 for 循环,该怎么做?

I want to create something like this

dup.bat infile outfile times

example usage would be

dup.bat a.txt a5.txt 5

at it would create file a5.txt that has the content of a.txt repeated 5 times

however I do not know how to do for loop in batch, how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无语# 2024-11-07 03:16:54

您可以像这样执行循环:

SET infile=%1
SET outfile=%2
SET times=%3

FOR /L %%i IN (1,1,%times%) DO (
    REM do what you need here
    ECHO %infile%
    ECHO %outfile%
)

然后要获取输入文件并重复它,您可以使用带有重定向的 MORE 将输入文件的内容附加到输出文件。请注意,这假设这些是文本文件。

@ECHO off
SET infile=%1
SET outfile=%2
SET times=%3

IF EXIST %outfile% DEL %outfile%
FOR /L %%i IN (1,1,%times%) DO (
    MORE %infile% >> %outfile%
)

You can do the loop like this:

SET infile=%1
SET outfile=%2
SET times=%3

FOR /L %%i IN (1,1,%times%) DO (
    REM do what you need here
    ECHO %infile%
    ECHO %outfile%
)

Then to take the input file and repeat it, you could use MORE with redirection to append the contents of the input file to the output file. Note this assumes these are text files.

@ECHO off
SET infile=%1
SET outfile=%2
SET times=%3

IF EXIST %outfile% DEL %outfile%
FOR /L %%i IN (1,1,%times%) DO (
    MORE %infile% >> %outfile%
)
无声情话 2024-11-07 03:16:54

For 命令行 args

set input=%1
set output=%2
set times=%3

要执行一个简单的 for 循环,请从 input 文件中读取数据,然后写入 output 文件:

FOR /L %%i IN (1,1,%times%) DO (
    FOR /F %%j IN (%input%) DO (
        @echo %%j >> %output%
    )      
)

您也可以不接收输出文件,而是通过命令行执行此操作:

dup.bat a.txt 5 > a5.txt

For command line args

set input=%1
set output=%2
set times=%3

To do a simple for loop, read in from the input file, and write to the output file:

FOR /L %%i IN (1,1,%times%) DO (
    FOR /F %%j IN (%input%) DO (
        @echo %%j >> %output%
    )      
)

Instead of taking in an output file, you could also do it via command line:

dup.bat a.txt 5 > a5.txt
星星的軌跡 2024-11-07 03:16:54

叹息

紧凑的设计:

SETLOCAL ENABLEDELAYEDEXPANSION
SET times=5
:Beginning
IF %times% NEQ 0 (TYPE a.txt>>a5.txt & SET /a times=%times%-1 & GOTO Beginning) ELSE ( ENDLOCAL & set times= & GOTO:eof)

易于阅读:

SETLOCAL ENABLEDELAYEDEXPANSION
SET times=5
:Beginning
IF %times% NEQ 0 (
TYPE a.txt>>a5.txt
SET /a times=%times%-1
GOTO Beginning
) ELSE (
ENDLOCAL
set times=
GOTO:eof
)

设置计数器(次数 = 5)
启动子程序开始
如果您的计数器不等于 0,则读取 a.txt 并将其内容附加到 a5.txt,然后将您的计数器减 1。这将重复五次,直到您的计数器等于 0,然后它将清除您的变量并结束脚本。
SET ENABLEDELAYEDEXPANSION 对于在循环内递增变量非常重要。

sigh

Compact Design:

SETLOCAL ENABLEDELAYEDEXPANSION
SET times=5
:Beginning
IF %times% NEQ 0 (TYPE a.txt>>a5.txt & SET /a times=%times%-1 & GOTO Beginning) ELSE ( ENDLOCAL & set times= & GOTO:eof)

Easy Reading:

SETLOCAL ENABLEDELAYEDEXPANSION
SET times=5
:Beginning
IF %times% NEQ 0 (
TYPE a.txt>>a5.txt
SET /a times=%times%-1
GOTO Beginning
) ELSE (
ENDLOCAL
set times=
GOTO:eof
)

Set your counter (times=5)
Start subroutine Beginning
If your counter doesn't equal 0, read a.txt and APPEND its contents to a5.txt, then decrement your counter by 1. This will repeat five times until your counter equals 0, then it will cleanup your variable and end the script.
SET ENABLEDELAYEDEXPANSION is important to increment variables within loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文