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 8750353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:51:04+00:00 2026-06-13T12:51:04+00:00

I am trying to create a simple .bat file that will generate a .html

  • 0

I am trying to create a simple .bat file that will generate a .html file that contains image links to all the images in the current directory.

Usage:
C:> gallery

Output:
gallery.html

File contents:

<HTML><HEAD></HEAD><BODY>
<IMG SRC="C:/path/path/path/image1.gif">
<IMG SRC="C:/path/path/path/randomepic.gif">
<IMG SRC="C:/path/path/path/asdfsadfasd.gif">
</BODY></HTML>

Note the image files will have unpredictable names, but will always be .gif.

What I’ve got: (in gallery.bat)

 @echo off
    set file_start=<HTML><HEAD></HEAD><BODY>
    set image_start=<IMG SRC="
    set image_dir=`cd`
    set image_end=">
    set file_end=</BODY></HTML>
    set file_name=gallery.html

    echo %file_start% > %file_name%
    for %%image in (*.gif) do @echo %image_start%%image_dir%%%image%image_end% >> $file_name%
    echo %file_end% >> %file_name%

But it doesn’t work. I can’t get the less-than symbol to appear, because the batch file thinks it is an input redirect. If I quote the lines with the < symbol, then the quotes appear in the echo statements.

Also, I can’t get the cd command output to save to the variable %image_dir%

Lastly, the echo statement in the for-loop isn’t printing out correctly. This might be a symptom of the previous problems though.

I’m hoping its just minor syntax problems I am having. Does anyone know how to fix this? Thanks for your time!

Patrick Q

  • 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-13T12:51:04+00:00Added an answer on June 13, 2026 at 12:51 pm

    FOR variables must be a single character. You cannot use something like %%image.

    You have problems with < and > twice over. Once when you define your variables, and again when you echo the expanded variable. Those characters must either be quoted or escaped with ^.

    You can use quotes during the assignment without including quotes in the variable –
    set "var=value". The assignment of image_end is a bit tricky because of the embedded quote.

    You can avoid the problem with ECHO by escaping the problem characters in your original assignment.

    You cannot use CD to get the current directory. The proper syntax is %CD%. But you don’t need your image_dir variable. You can use the ~f modifier to get the full path of the file name. Type help for from a command prompt for more information about FOR variable modifiers.

    It is more efficient to enclose all the commands that produce output in parentheses and redirect to your output file just once.

    @echo off
    setlocal
    set "file_start=^<HTML^>^<HEAD^>^</HEAD^>^<BODY^>"
    set "image_start=^<IMG SRC=""
    set ^"image_end="^>"
    set "file_end=^</BODY^>^</HTML^>"
    set "file_name=gallery.html"
    
    >"%file_name%" (
      echo %file_start%
      for %%I in (*.gif) do echo %image_start%%%~fI%image_end%
      echo %file_end%
    )
    

    Another way to avoid problems while ECHOing unquoted characters is to use delayed expansion. But ! is valid in file names, and delayed expansion will corrupt expansion of FOR variables if they contain the ! character. So delayed expansion must be toggled on and off within the loop.

    @echo off
    setlocal enableDelayedExpansion
    set "file_start=<HTML><HEAD></HEAD><BODY>"
    set "image_start=<IMG SRC=""
    set ^"image_end=">"
    set "file_end=</BODY></HTML>"
    set "file_name=gallery.html"
    
    >"%file_name%" (
      echo !file_start!
      setlocal disableDelayedExpansion
      for %%I in (*.gif) do (
        set "image=%%~fI"
        setlocal enableDelayedExpansion
        echo !image_start!!image!!image_end!
        endlocal
      )
      endlocal
      echo !file_end!
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to create a bat file using csharp which contains some commands. So
I am trying to make a .bat file that creates a simple text file.
I'm trying to create simple slot machine with three slots. Each slot will generate
I'm trying to create simple slideshow where if I click the image it will
I am trying to create simple user registration form. I have an index.html file
I'm trying to create simple function that will work two ways. Basically, when I
I'm trying to create simple script that subscribes a user to a company calendar.
I am trying to create a simple about page that uses JQuery click to
I'm trying to create a simple web page that resembles the following: I've got
I am trying to create a script that will self update when it detects

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.