Wally Walters
Wally Walters

Reputation: 75

Does Windows Batch have anything similar to BASIC's "data" statement?

What's the easiest way to have a batch file load long lists of data? Right now I'm loading mine from a separate text file, but I suspect that's slower than having the data in the same file as -- i.e., loaded at the same time as -- the program code. Is there a better way to handle it than what I'm doing now, which is this:

for /f "usebackq delims=" %%g in (list.txt) do (if exist "%%g.jpg" del "%%g.jpg")

Upvotes: 0

Views: 77

Answers (3)

Aacini
Aacini

Reputation: 67216

I think this is the simplest way:

@echo off

for %%g in (file1  file2  file3  "A long file name with spaces"
            fileN  fileM ) do (
   del "%%~g.jpg"
)

Upvotes: 3

Squashman
Squashman

Reputation: 14290

Well lets try another goofy way to read a list. Got this technique I believe from Aacini over on the Dostips.com forums. There is a max amount of characters that you can have for the SET command so it will fail when try to assign to many entries.

 @echo off
 setlocal EnableDelayedExpansion

 set str=file1.txt?^
 file2.txt?^
 file 4.txt?^
 file5.txt?^
 some other file.jpg?^
 and yet another file.mp3?

 for /F "delims=" %%s in (^"!str:?^=^
 %= Replace question with linefeeds =%
 !^") do (
   echo %%~s
 )
 pause

Output

file1.txt
file2.txt
file 4.txt
file5.txt
some other file.jpg
and yet another file.mp3
Press any key to continue . . .

I tested all three sets of code and because the data sets are so small the time difference is negligible on my machine. I used 36 lines for the data set and looped the code to run 10 times.

Wally time is 0.78 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.78 Seconds
Wally time is 0.79 Seconds
Squashman time is 0.79 Seconds
Magoo time is 0.78 Seconds
Wally time is 0.78 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.78 Seconds
Wally time is 0.78 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.79 Seconds
Wally time is 0.78 Seconds
Squashman time is 0.62 Seconds
Magoo time is 0.78 Seconds
Wally time is 0.78 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.94 Seconds
Wally time is 0.79 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.78 Seconds
Wally time is 0.78 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.94 Seconds
Wally time is 0.79 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.78 Seconds
Wally time is 0.78 Seconds
Squashman time is 0.78 Seconds
Magoo time is 0.78 Seconds
Press any key to continue . . .

Upvotes: 1

Magoo
Magoo

Reputation: 80013

Here's a way to do what you ask:

@ECHO OFF
SETLOCAL
SET "data="
FOR /f "usebackqdelims=" %%a IN ("%~f0") DO (
 IF /i "%%a"=="[enddata]" SET "data="
 IF DEFINED data ECHO execute command ON "%%a"
 IF /i "%%a"=="[data]" SET "data=Y"
)

GOTO :EOF

[data]
some filename.jpg
MORE data.jpg
[enddata]

Well - it's over-complicated. Obviously, the [enddata] facility could be removed, but this allows flexibility (two or more data-sections, [data1] [data2], etc, each with its own end-of-data-section)

I would suspect that it would be slower than your current system, and far harder to use. With an external file, you can simply change that file with an editor (or programmatically) whereas with this system, you'd need to maintain the batch file itself.

It is better suited to "firmware"-style data - semi-permanent like a list of servers - rather than dynamic data, but you pays your money, you takes your choice...

Upvotes: 3

Related Questions