Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Reino17/900aac57dc1609796ac80bdddb69af37 to your computer and use it in GitHub Desktop.
Save Reino17/900aac57dc1609796ac80bdddb69af37 to your computer and use it in GitHub Desktop.

Directory structure:

dir1\image.jpg
dir1\image.png
dir2\image.jpg
dir2\image.png
image.jpg
image.png

Create a batch-script like the following:

@ECHO OFF
ECHO ^<html^>
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=" %%A IN ('DIR /A:-D /B /O:N /S *.jpg *.png') DO (
  SET "var=%%A"
  SET "var=!var:%CD%\=!"
  ECHO   ^<img src="!var:\=/!"/^>
)
ECHO ^</html^>

DIR /A:-D /B /S *.jpg *.png returns:

<full-path>\image.jpg
<full-path>\image.png
<full-path>\dir1\image.jpg
<full-path>\dir1\image.png
<full-path>\dir2\image.jpg
<full-path>\dir2\image.png

You can add /O:D for instance if you want to sort by date.

"var=!var:%CD%\=!" changes the full path to a relative one:

<full-path>\dir1\image.jpg --> dir1\image.jpg

"!var:\=/!" changes the backslash to a for HTML valid forward slash:

dir1\image.jpg --> dir1/image.jpg

Output:

<html>
  <img src="image.jpg"/>
  <img src="image.png"/>
  <img src="dir1/image.jpg"/>
  <img src="dir1/image.png"/>
  <img src="dir2/image.jpg"/>
  <img src="dir2/image.png"/>
</html>

Although this batch-script works in this particular case, cmd/Batch doesn't support HTML/XML at all. I suggest you have a look at a tool that does, like xidel.

xidel.exe -se "file:list(.,true(),'*.(jpg|png)')"
dir1\image.jpg
dir1\image.png
dir2\image.jpg
dir2\image.png
image.jpg
image.png

xidel.exe -se "for $x in file:list(.,true(),'*.(jpg|png)') return replace($x,'\\','/')"
dir1/image.jpg
dir1/image.png
dir2/image.jpg
dir2/image.png
image.jpg
image.png

xidel.exe -s --xquery "for $x in file:list(.,true(),'*.(jpg|png)') return serialize(<img src='{replace($x,'\\','/')}'/>)"
<img src="dir1/image.jpg"/>
<img src="dir1/image.png"/>
<img src="dir2/image.jpg"/>
<img src="dir2/image.png"/>
<img src="image.jpg"/>
<img src="image.png"/>

xidel.exe -s --xquery "serialize(<html>{for $x in file:list(.,true(),'*.(jpg|png)') return <img src='{replace($x,'\\','/')}'/>}</html>,{'indent':true()})"
<html>
  <img src="dir1/image.jpg"/>
  <img src="dir1/image.png"/>
  <img src="dir2/image.jpg"/>
  <img src="dir2/image.png"/>
  <img src="image.jpg"/>
  <img src="image.png"/>
</html>

The final command/query "prettified" (and with the necessary escape-characters):

xidel.exe -s --xquery ^"^
  serialize(^
    ^<html^>{^
      for $x in file:list(.,true(),'*.(jpg^|png)') return^
      ^<img src='{replace($x,'\\','/')}'/^>^
    }^</html^>,^
    {'indent':true()}^
  )^
"
@vaatimidona
Copy link

Hi Reino, thanks a lot for taking the time to answer to my problem. I didn't agree either with my question being closed but it was the first time I asked a question on this community and I found it a bit harsh so I kinda gave up ...
But I was really happy to see your help ! I didn't have a lot of time to test this code yet but I strongly intend to in the next few days ! I just wanted to thank you (also thank you for detailing it simply), and I will come back in a few days to give you my feedback (and probably some questions hehe)

@vaatimidona
Copy link

Hi Reino, I finally got time to test it and it works like a charm ! Thank you again for taking the time to help me with this, and to detail every step of it so I can understand :)

@Reino17
Copy link
Author

Reino17 commented Jun 7, 2021

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment