Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: pball
I want to to be able to output a list using a batch file.
I have most of what is needed but I cannot add a couple of things to it. I'm trying to make a javascript file with a list of pictures in it for a website. I need to get to relative location of the pictures and put parentheses around the names and a comma after each name.sample of what's needed,
var imgList = [
"pic/1.gif",
"pic/2.jpg",
"pic/3.jpg",
"4.jpg",
"5.jpg",
"6.jpg"
];using this
echo var imgList = [ > "imglist.js"
dir /A:-D /s /b >> "imglist.js"
echo ]; >> "imglist.js"I get
var imgList = [
E:\PROGRAMS\Random Image Javascript\images\4.jpg
E:\PROGRAMS\Random Image Javascript\images\5.jpg
E:\PROGRAMS\Random Image Javascript\images\6.jpg
E:\PROGRAMS\Random Image Javascript\images\pic\1.gif
E:\PROGRAMS\Random Image Javascript\images\pic\2.jpg
E:\PROGRAMS\Random Image Javascript\images\pic\3.jpg
];I just need a way to get the dir command to only search in folders "below" where it is run. So I would run the batch file from images and it would return what the first list has. Also as noted before parentheses have to be added and a comma.
Is this easily possible? I have been trying to modify something i had from another project to get what I need but I couldn't get it to work.

Thanks this does create the properly formated list I need. There is only one thing missing. Is there an easy way to have this search the folders below where the batch file is run from instead of the folder it is in.
All I need now is to be able to search multiple subdirectories. So if I run it in d:\pics it will search all of the subdirectories in d:\pics

This gets all files in subfolders but NOT the starting folder.
::=====================
@echo off > filelist
setLocal EnableDelayedExpansionset WD=%CD%
for /f "tokens=* delims= " %%d in ('dir/s/b/ad') do (
pushd "%%d"
for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^> nul') do (
echo %%f >> %WD%\filelist
)
)
::===========================
=====================================
If at first you don't succeed, you're about average.M2

Thanks Mechanix2Go, it makes the list nicely. I just threw in the directory i needed in the first for loop and put parenthesis and a comma on the file name.
This is what I did
setLocal EnableDelayedExpansion
set WD=%CD%
echo var imgList = [ > %WD%\imglist.js
for /f "tokens=* delims= " %%d in ('dir "E:\pics\1280 up" /s/b/ad') do (
pushd "%%d"for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^> nul') do (
echo "%%f", >> %WD%\imglist.js)
)echo ]; >> %WD%\imglist.js
I played with this script for a while and only have one more question. I would the like the name of the folder then the picture name.
"E:\pics\1280 up\Akira\Akira_001.jpg",
"E:\pics\1280 up\Black Cat\Black Cat_001.jpg",If i stick %%d\ infront of the %%f I get what is above. Is there way to remove the E:\pics\1280 up\ or a different way to get just the folder the picture is in?

You mean the full path to the pictures?
@echo off
setLocal EnableDelayedExpansionset WD=%CD%
echo var imgList = [ > %WD%\imglist.js
for /f "tokens=* delims= " %%d in ('dir "E:\pics\1280 up" /s/b/ad') do (
pushd "%%d"for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^> nul') do (
echo "%%~ff", >> %WD%\imglist.js)
)echo ]; >> %WD%\imglist.js
"Foolproof systems don't take into account the ingenuity of fools."

tonysathre
I do not want the full path to the picture just the name of the folder the picture is in.Is there a way to find and replace, so I could replace E:\pics\1280 up with nothing. This would leave only the folder then the pictures that are in the folder

You could add another loop after the second:
setLocal EnableDelayedExpansionDEL imglist.js.tmp 2>NUL
ECHO var imgList = [ > imglist.jsFOR /R %%a IN (*.*) DO @ECHO "%%~Fa", >> imglist.js.tmp
FOR /F "delims=" %%a IN (imglist.js.tmp) DO @(SET _tmp=%%a
ECHO !_tmp:E:\pics\1280=!) >> "imglist.js"echo ]; >> imglist.js

@echo off > filelist
setLocal EnableDelayedExpansionset WD=%CD%
for /f "tokens=* delims= " %%d in ('dir/s/b/ad') do (
pushd "%%d"for /f "tokens=* delims= " %%x in ('echo !CD!') do (
set CWD=%%~Nx
)for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^> nul') do (
echo !CWD!\%%f >> %WD%\filelist
)
)
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |