Windows Batch是否有类似于BASIC"数据"声明?

时间:2017-02-11 22:34:46

标签: batch-file

批处理文件加载长数据列表的最简单方法是什么?现在我从一个单独的文本文件中加载我的文件,但我怀疑它比同一个文件中的数据要慢 - 即在程序代码的同时加载。有没有比我现在做的更好的方法来处理它,这就是:

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

3 个答案:

答案 0 :(得分:3)

这是一种做你要求的方法:

Session

嗯 - 这太复杂了。显然,可以删除@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] 工具,但这允许灵活性(两个或多个数据部分,[data1] [data2]等,每个部分都有自己的数据末尾部分)

我怀疑它会比你现在的系统慢,而且使用起来要困难得多。使用外部文件,您只需使用编辑器(或以编程方式)更改该文件,而使用此系统,您需要自行维护批处理文件。

它更适合“固件”式数据 - 半永久性数据,如服务器列表 - 而非动态数据,但您付钱,您可以选择......

答案 1 :(得分:3)

我认为这是最简单的方法:

@echo off

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

答案 2 :(得分:1)

好吧,让我们尝试另一种愚蠢的方式来阅读列表。在Dostips.com论坛上我从Aacini那里得到了这种技术。您可以为SET命令设置最多字符数,以便在尝试分配给许多条目时失败。

 @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

输出

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

我测试了所有三组代码,因为数据集非常小,我的机器上的时差可以忽略不计。我使用36行作为数据集,并将代码循环运行10次。

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 . . .
相关问题