2017-02-11 53 views
0

让批处理文件载入长数据列表的最简单方法是什么?现在我从一个单独的文本文件加载我的文件,但我怀疑这比在程序代码中将数据放在同一个文件中的速度要慢 - 比如同时加载 - 。有没有更好的方式来处理它比这就是我现在正在做的,这样的:Windows Batch是否有与BASIC的“数据”语句类似的东西?

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

定义easer /更好。更容易阅读/维护/理解你/​​后来的用户/计算机?在你的代码中'usebackq'和围绕if的括号不是必需的。 – LotPings

+0

如果程序代码和数据集都在单独的文件中,那么保持它们肯定更容易。但他们似乎更慢。这就是为什么我想知道是否有另一种方式来读取数据。 –

+0

有没有必要的'如果存在'。你强制两个磁盘读取和一个磁盘写入而不是一个和一个。只需删除该文件。如果它不存在,则不会改变,如果不存在,它将被删除。 – Freddie

回答

1

好让我们尝试另一种愚蠢的方式来阅读清单。我相信Aacini在Dostips.com论坛上提供了这种技术。对于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 . . . 
2

这里有一个方式做什么你问:

@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] 

好 - 这是过于复杂。显然,[enddata]设施可以被移除,但是这允许灵活性(两个或更多个数据段,[数据1] [数据2]等,各有其自己的结束数据截面)

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

它更适合于“固件”式的数据 - 半永久性的,比如服务器的列表 - 而不是动态的数据,但是你付出你的钱,你需要你的选择......

+0

谢谢,我怀疑你是对的,但我期待着尝试一下,并对它们进行计时。可能发生的最糟糕的是我会学习一种新技术。 –

3

我觉得这是最简单的方法:

@echo off 

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

除非在其中一个文件名中有逗号或分号。那么你需要引用所有的文件名。 – Squashman

相关问题