简体   繁体   中英

How do you correctly handle redirection in FOR loop in one line of command?

I want to output the file cat.txt's content and save them in another text file using FOR /F loop ( in one line of command of standard cmd shell.)

for example, here is the source file.

# content of cat.txt:
meow
meo
me
m

after use this command, I've got some unintended result:

FOR /F "tokens=*" %G in (cat.txt) DO echo %G > D:\\copy-cat.txt

# I got output:
meow > D:\copy-cat.txt
meo > D:\copy-cat.txt
me > D:\copy-cat.txt
m > D:\copy-cat.txt

I understand in this scenario, the redirection is being affected by echo, hence recognized as string and echoed out.

Question: How do I semantically separate the code so that I can redirect all the content that were written to stdout into D:\\copy-cat.txt?

Note: as you can see by %G, I need to write this directly in commandline, not in a batch script.

>copy-cat.txt (FOR /F "delims=" %G in (cat.txt) DO @echo %G)
  • Instead of redirecting each output line, redirect the full for output
  • To avoid the inclusion of the echo command itself, use prefix the command with @ to not echo the echo (single command echo off )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM