Output in Reverse in CMD

亡梦爱人 提交于 2020-03-04 16:38:27

问题


I have a phrase that needs to be outputted in results.txt. The phrase comes from x.txt. For example: "I have two kids", it should output "kids two have I" in results.txt.

UPDATE

Its already working but i want no loop. Pls see code below

Code

@ECHO OFF
set /p content=<x.txt
SET var=!content: =,!
SET rev=
:LOOP
IF NOT "!var!"=="" (
    FOR /F "delims=, tokens=1,*" %%F IN ("!var!") DO (
        SET rev=%%F,!rev!
        SET var=%%G
    )
) ELSE (
    SET rev=!rev:~0,-1!
    GOTO ENDLOOP
)
GOTO LOOP
:ENDLOOP
ECHO !rev:,= ! > results.txt

回答1:


This takes the first four words (of each line) of the text file and rewrites them in reverse order to result.txt:

>result.txt (for /f "tokens=1-4" %%a in (x.txt) do echo %%d %%c %%b %%a)

Another solution (for a one-line text file, unspecified number of words):

@ECHO OFF
setlocal enabledelayedexpansion
echo I have two little but wonderful kids>x.txt
<x.txt set /p x=
for %%a in (%x%) do set "res=%%a !res!"
>result.txt echo %res:~0,-1%

(although technically, the for command is a loop on its own)

Without any form of a loop, if you can live with some spaces at the front:

@ECHO OFF
setlocal 
echo I have two kids>x.txt
<x.txt set /p x=
call :reverse %x%
goto :eof
:reverse
set "rev=%9 %8 %7 %6 %5 %4 %3 %2 %1"
echo Reverse without any form of loop: "%rev%"
for /f "tokens=*" %%a in ("%rev%") do echo To get rid of the spaces, you need a FOR loop: "%%a"

This is limited to a maximum of nine words because cmd supports only %1to %9
You can use more parameters (words) by using the shift command, but that would mean using a loop.




回答2:


Here are three different ways.

  1. Replacement magic (borrowed from Aacini)
  2. For tokens
  3. Recursion
@echo off
setlocal EnableDelayedExpansion

set "var=One two three four five"

call :rev1
call :rev2
call :rev3
exit /b


:rev1 -- replacement magic
setlocal
set "rev="
set rev=%var: = !rev!&Set rev=% !rev!

echo %0 !rev!
exit /b

:rev2 -- FOR toekns
setlocal
FOR /F "tokens=1-5" %%1 in ("%var%") do set "rev=%%5 %%4 %%3 %%2 %%1"
echo %0 !rev!
exit /b

:rev3 -- recursion
setlocal
set "rev="
call :__rev3_rec %var%
echo %0 !rev!
exit /b

:__rev3_rec
if "%1" == "" exit /b
call :__rev3_rec %2 %3 %4 %5
set "rev=%rev%%1 "
exit /b



回答3:


I'm not sure if I've understood your question fully, so this is intended to reverse the order of each space separated substring per line.

For the task, I'd leverage PowerShell.



At the powershell prompt, (powershell.exe):
GC '.\x.txt'|%{$L=$_.Split(' ');[Array]::Reverse($L);$L -Join ' '}|SC '.\results.txt'

At the command-prompt, (cmd.exe):

"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "GC '.\x.txt'|%{$L=$_.Split(' ');[Array]::Reverse($L);$L -Join ' '}|SC '.\results.txt'

From a batch-file:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "GC '.\x.txt'|%%{$L=$_.Split(' ');[Array]::Reverse($L);$L -Join ' '}|SC '.\results.txt'



If you're only working on the first line of the file, as opposed to all of them, a small modification is needed.

At the powershell prompt, (powershell.exe):

GC '.\x.txt' -To 1|%{$L=$_.Split(' ');[Array]::Reverse($L);$L -Join ' '}|SC '.\results.txt'

At the command-prompt, (cmd.exe):

"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "GC '.\x.txt' -To 1|%{$L=$_.Split(' ');[Array]::Reverse($L);$L -Join ' '}|SC '.\results.txt'

From a batch-file:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "GC '.\x.txt' -To 1|%%{$L=$_.Split(' ');[Array]::Reverse($L);$L -Join ' '}|SC '.\results.txt'



Please note that, in both cases, if your strings are actually doublequoted, those will not move position. This means that "I have two kids", would output as kids" two have "I, not "kids two have I".

来源:https://stackoverflow.com/questions/60371486/output-in-reverse-in-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!