-1

I've set up an app for a couple of friends and me in batch with a auto-updating system, but I need to add a line of code in the auto-updating system. I decided to completely rewrite the file, it takes a long time to add 'echo' in from to every line and, '>>text.txt' at the end of every line and added '^' when needed, so I was wondering if there was an easier way of writing lot's of code to a file in batch. Example:

@echo off
rem I need a way to do the following without adding 'echo' and '>>text.txt'

echo echo Checking for updates... >text.txt
echo echo 1.4 ^>^>new.txt >>text.txt
echo ping localhost -n 2 ^>nul >>text.txt
rem and so on and so on.

Or if there is a way to simply add a new line of code in a specific place in the file, that would also help! Thanks in advance!

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    Your exact task is unclear, and you're only supposed to ask one question. Please edit your question to provide an actual single task, which can be replicated by anyone willing to provide specific help. Please also search this site, _it is unlikely that you've got a requirement which has never been asked or answered previously_. – Compo Apr 12 '20 at 21:34
  • 1
    [this](https://stackoverflow.com/a/40452235/2152082) might be interesting to you. – Stephan Apr 13 '20 at 10:05
  • if u have time, plz either accept an answer or at least reply with the answer cannot solve your question. – ScriptKidd Apr 14 '20 at 14:42

3 Answers3

0

The following is how you can more easily and efficiently do what your current code does, by removing all of those individual write processes.

@(  Echo @Echo Checking for updates...
    Echo @(Echo 1.4^)^>^>"new.txt"
    Echo @(%__AppDir__%ping.exe LocalHost -n 2^)^>NUL
)>"text.txt"

There are other possibilities, but at this time, based on the lack of information in your question, I'm reluctant to expand further at this time.

Compo
  • 36,585
  • 5
  • 27
  • 39
0

If I understand correctly, then you could do the following:

  • in the batch file, prepend each line of text that you want to output with :::: (this constitutes an invalid label that is going to be ignored);
  • then use the following code:

    rem // Redirect to file:
    > "text.txt" (
        rem // Find all lines that begin with `::::` and loop over them:
        for /F "delims=" %%T in ('findstr "^::::" "%~f0"') do (
            rem // Store currently iterated line:
            set "TEXT=%%T"
            rem // Toggle delayed expansion to avoid loss of `!`:
            setlocal EnableDelayedExpansion
            rem // Remove `::::` prefix and output remaining line:
            echo(!TEXT:*::::=!
            endlocal
        )
    )
    

    replace set "TEXT=%%T" by call set "TEXT=%%T" if you want to enable percent expansion within the returned text (so it could, for example, contain %~nx0, which would then be expanded to the file name of the script).

I am using this technique a lot (without the output redirection) for help systems in my batch files (/?).

aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

Your asked

I need a way to do the following without adding echo and >>text.txt

The script takes advantage of the line continuation character, the caret ^.
The first character after the caret ^ is always escaped, so do linefeed characters:

@echo off
SETLOCAL EnableDelayedExpansion

call :init

>text.txt (
echo @echo off%NL%
Checking for updates...%NL%
>^>new.txt echo 1.4%NL%
>NUL ping localhost -n 2
)

ENDLOCAL
exit /b

:init
( set LF=^
%= 0X0D FORM FEED =%
)
::0X0A Carriage Return
FOR /F %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"

::Create newline/line continuation character
set ^"NL=^^^%LF%%LF%^%LF%%LF%^^" %= Unix-Style Endings \n =%
::set ^"NL=%CR%^^^%LF%%LF%^%LF%%LF%^^" %= Windows-Style Endings \r\n =%
exit /b

The variable %LF% is a escaped linefeed, and %NL% is a escaped %LF% plus a escaped caret ^ for line continuation.

The code

>^>new.txt echo 1.4%NL%
>NUL ping localhost -n 2

might seem strange. Why isn't the first caret ^ escaped?
Because %NL% already escaped it.
Sources:

ScriptKidd
  • 803
  • 1
  • 5
  • 19