Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Making a list with batch

Original Message
Name: pball
Date: November 16, 2007 at 23:16:38 Pacific
Subject: Making a list with batch
OS: XP
CPU/Ram: 2.0 ghz / 1 gig
Model/Manufacturer: 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.


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: November 17, 2007 at 10:06:21 Pacific
Subject: Making a list with batch
Reply: (edit)
FOR %%a IN (*.*) DO ECHO "%%~NXa", >> "imglist.js"

Report Offensive Follow Up For Removal

Response Number 2
Name: pball
Date: November 17, 2007 at 23:15:34 Pacific
Subject: Making a list with batch
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: November 18, 2007 at 00:38:04 Pacific
Subject: Making a list with batch
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 4
Name: pball
Date: November 18, 2007 at 14:30:36 Pacific
Subject: Making a list with batch
Reply: (edit)
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?


Report Offensive Follow Up For Removal

Response Number 5
Name: tonysathre
Date: November 18, 2007 at 16:21:59 Pacific
Subject: Making a list with batch
Reply: (edit)
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."


Report Offensive Follow Up For Removal


Response Number 6
Name: pball
Date: November 18, 2007 at 18:29:49 Pacific
Subject: Making a list with batch
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 7
Name: Razor2.3
Date: November 18, 2007 at 19:27:38 Pacific
Subject: Making a list with batch
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: November 19, 2007 at 06:49:52 Pacific
Subject: Making a list with batch
Reply: (edit)
@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



Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Making a list with batch

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




how to setup call of duty to joytok

WindowsME / HotMail Problem

Corrupt memory

Convert fat32 to Ntfs

Best WinMo phone of 2008


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC