Computing.Net > Forums > Programming > Making a list with batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Making a list with batch

Reply to Message Icon

Name: pball
Date: November 16, 2007 at 23:16:38 Pacific
OS: XP
CPU/Ram: 2.0 ghz / 1 gig
Product: homemade
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: November 17, 2007 at 10:06:21 Pacific
Reply:

FOR %%a IN (*.*) DO ECHO "%%~NXa", >> "imglist.js"


0

Response Number 2
Name: pball
Date: November 17, 2007 at 23:15:34 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: November 18, 2007 at 00:38:04 Pacific
Reply:

This gets all files in subfolders but NOT the starting folder.

::=====================
@echo off > filelist
setLocal EnableDelayedExpansion

set 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



0

Response Number 4
Name: pball
Date: November 18, 2007 at 14:30:36 Pacific
Reply:

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?


0

Response Number 5
Name: tonysathre
Date: November 18, 2007 at 16:21:59 Pacific
Reply:

You mean the full path to the pictures?

@echo off
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 "%%~ff", >> %WD%\imglist.js

)
)

echo ]; >> %WD%\imglist.js

"Foolproof systems don't take into account the ingenuity of fools."


0

Related Posts

See More



Response Number 6
Name: pball
Date: November 18, 2007 at 18:29:49 Pacific
Reply:

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


0

Response Number 7
Name: Razor2.3
Date: November 18, 2007 at 19:27:38 Pacific
Reply:

You could add another loop after the second:

setLocal EnableDelayedExpansion

DEL imglist.js.tmp 2>NUL
ECHO var imgList = [ > imglist.js

FOR /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


0

Response Number 8
Name: Mechanix2Go
Date: November 19, 2007 at 06:49:52 Pacific
Reply:

@echo off > filelist
setLocal EnableDelayedExpansion

set 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



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Making a list with batch

Making a Keyogger with *.bat www.computing.net/answers/programming/making-a-keyogger-with-bat/17691.html

Comparing files/ lists with batch www.computing.net/answers/programming/comparing-files-lists-with-batch/12686.html

Create a registry backup with batch www.computing.net/answers/programming/create-a-registry-backup-with-batch/8549.html