Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:11:22+00:00 2026-06-18T02:11:22+00:00

having a bit of trouble with a windows batch file I’m writing. I need

  • 0

having a bit of trouble with a windows batch file I’m writing.

I need the batch file to write some particular lines to another batch file, the method I have been using is:

type NUL > batchfile.bat
ECHO texttobewrittentofile >> batchfile.bat
ECHO texttobewrittentofile >> batchfile.bat
ECHO texttobewrittentofile >> batchfile.bat
...
etc

Most of the lines write fine, there are a few different problems I’m having with my batch file writing to another batch file though.

The code for my batch file to write to another batch file is:

ECHO @echo off >> GenerateEmail.bat
ECHO ECHO Opening Stunnel >> GenerateEmail.bat
ECHO pushd .\stunnel\ >> GenerateEmail.bat
ECHO start "" stunnel.exe stunnel.conf >> GenerateEmail.bat
ECHO popd >> GenerateEmail.bat
ECHO ECHO Determining latest log for use with blat >> GenerateEmail.bat
ECHO pushd O:\Logs\%clientname%\ >> GenerateEmail.bat
ECHO for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a >> GenerateEmail.bat
ECHO popd >> GenerateEmail.bat
ECHO ECHO Generating email containing contents of latest log >> GenerateEmail.bat
ECHO pushd .\blat307\full\ >> GenerateEmail.bat
ECHO ECHO Y | xcopy "O:\Logs\%clientname%\%newest%" ".\" >> GenerateEmail.bat
ECHO blat.exe "%newest%" -to %clientemail% -cc %gmailemail% -server 127.0.0.1:1099 -subject "Offsite Backup for "%1" "%2" - %clientname%" -sig sig.txt >> GenerateEmail.bat
ECHO DEL .\*.log >> GenerateEmail.bat
ECHO popd >> GenerateEmail.bat
ECHO ECHO Closing Stunnel >> GenerateEmail.bat
ECHO pushd .\stunnel\ >> GenerateEmail.bat
ECHO stunnel.exe -exit >> GenerateEmail.bat
ECHO popd >> GenerateEmail.bat

I’m not getting the desired output that I would like, I’m having issues with:

ECHO Y | xcopy "O:\Logs\%clientname%\%newest%" ".\" >> GenerateEmail.bat

coming out as

0 File(s) copied

and everywhere that I need %% it comes out as %, as well as the line starting with “blat.exe” isn’t written at all amongst other problems. Is there any way to get a batch file to write lines of text without acknowledging any commands or symbols contained in those lines?

Kane.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:11:23+00:00Added an answer on June 18, 2026 at 2:11 am

    All special characters like ^ & | < > must be escaped as ^^ ^& ^| ^< ^> or else enclosed in quotes.

    Every percent must be doubled always. For example:

    • "%1" becomes "%%1"
    • %newest% becomes %%newest%%
    • %%a becomes %%%%a

    You don’t need to clear the output file and redirect each line if you put parentheses around all the ECHO statements. Right parentheses must also be escaped if doing this.

    >GenerateEmail.bat (
      echo Line 1
      echo Line 2
      echo these must be escaped ^^ ^& ^| ^> ^< ^), these not "^ & | > < )"
    )
    

    The rules become more complicated if you have delayed expansion enabled and you have output that includes !, or ! with ^.

    There are other methods that avoid having to figure out how to escape special characters and double up percents.

    You can prefix each line with ::: and use FOR /F with FINDSTR to get the desired output. Make sure delayed expansion is off if your output contains !. The only restriction with the code below is your output line cannot begin with : (it can begin with a space, followed by :).

    (for /f "tokens=* delims=:" %%L in ('findstr /b ::: "%~f0"') do echo %%L) >GenerateEmail.bat
    :::echo special chars & | < > don't need to be escaped
    :::echo % does not need to be doubled
    ::: :Label
    ::: :: Comment and label must be indented with at least one space
    :::etc
    

    Another option is to use one of the techniques found at:

    • Echo the contents of a paragraph into a file without any modifications to the paragraph?
    • printing a paragraph in windows batch
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble with crashes on Windows 7 32 bit in my
I'm having some trouble parsing a *.CSV file in windows server 2008 64bit version.
I'm having a bit of trouble loading an html file in a WebView control
I'm new to Windows Phone 7 development, and am having a bit of trouble
I'm having a bit of trouble finding a reliable method of determining CPU frequency
I am having a bit of trouble writing an android app that can communicate
I'm having a bit of trouble including headers from Windows Driver Kit to Visual
I'm having a bit of trouble trying to compile Qt programs under Windows. I'm
im having a bit of trouble writing a keyboard hook in C++. I can
I'm having a bit of trouble populating a listbox on a windows form from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.