Steed
Steed

Reputation: 61

how to set a text file to be altered by a batch script and output to a .txt and erase the contents of the original text file?

ok so i have a text file i am trying to work on. the batch reverses text. the batch works fine in cmd>batch.bat i am happy [enter] yppah ma i

however i have been trying to do this. cmd>batch.bat input.txt [enter] output.txt

so basically if input.txt says abcdef - output.txt will say fedcba and input.txt will be blank.

i have tried many snippets of code from similar questions on here and none have done well; input.txt is remained unchanged and output.txt remains empty.

when i try to run this to set the file to be run against the batch file, C:....>batch.bat set /p Build=input.txt

i just get something like C:....>echo txt.tupni=dliuB p/ tes txt.tupni=dliuB p/ tes

i have tried code from so many different questions on here and they all come out backwards rather than the file contents. i must be making a noob mistake. maybe there is a way i can edit the batch by adding a line in it... something like:

set file=input.txt ...

outfile=output.txt

i think that would simplify things but everytime i try to add set file and outfile to the batch file the whole batch stops working..

heres the batch file. it works it just doesnt like to work anyway other than C:...>batch.bat "sometext" ... "txetemos"

i know me asking this question will annoy some of the more serious coders here but im doing my best to learn, and i feel like a simple alteration to this code would make it perfect... so heres the batch.

setlocal
if [%1] neq [] goto start
echo Reverse Given Text
echo Usage: %0 text

goto :end

:start
    set _len=0
    set _str=%*

:: Get the length of the sentence
    set _subs=%_str%

:loop
    if not defined _subs goto :result

    ::remove the first char
    set _subs=%_subs:~1%
    set /a _len+=1
    goto loop   

:result
    set /a _len-=1
    for /l %%g in (0,1,%_len%) do (
        call :build %%g
    )

    echo %s%

    goto :end

:build
    :: get the next character
    call set _digit=%%_str:~%1,1%%%
    set s=%_digit%%s%

:end
endlocal

i have set sever different methods of implementing a set file= and an >outfile and have had no luck so i tried to add code on the cmd line and i just get echo "edoc sdrawkcab" "backwards code"

EDIT:cut out useless code and made the following edit;

EDIT!: i have found that running batch.bat "text">>log.txt [enter] makes,,, "txet" being appended into a log.txt... this solves the output to a text file problem..

now i just need to figure out how to get input.txt's text to be ran against batch.bat which will reverse its text using the working batch.bat i think i can achieve this by making a maincaller "maincaller.bat" file to run this:

batch.bat [set-file=input.txt]>>log.txt

or something similar to result in something like this:; input.txt reads abra cadabra i need a command to result in

"arbadac arba" within the >>log.txt file

EDIT* OUTPUT.TXT will now be reffered to as log.txt

Upvotes: 1

Views: 576

Answers (1)

Stephan
Stephan

Reputation: 56180

I don't quite get, what you want, but here is some code to "revert" a string:

@echo off
setlocal enabledelayedexpansion
set "string=Hello World"
set "reverse="
for /l %%i in (0,1,100) do set "reverse=!string:~%%i,1!!reverse!"
echo %string% - %reverse%

Upvotes: 1

Related Questions