2017-02-11 51 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

相關問題