2014-12-27 20 views
1

我遍歷了大約300個目錄並嘗試在每個目錄中創建一個文本文件。我的所有代碼都可以工作,除了文件創建和後續寫入。以下是我正在試圖寫入文件:(我要指出,這個代碼是內嵌套的for循環使用SETLOCAL EnableDelayedExpansion)使FilePaths在批處理中正常工作(寫入文本文件)

if !q! == 0 (
    set myString=%%f 
    set myString=!myString:~0,-12!\temporary.txt   
    echo !m! >> !myString! 
) 

其中%% f是路徑,另一個文本文件我做一個分析,所以我必須縮短路徑。這個文本文件的名稱在所有目錄中是統一的。我曾嘗試只是使用:

echo !myString! 

產生的示例輸出(正確的文件路徑):

C:\mywork2\MAX\BOTH0\temporary.txt 

然而,當我運行的完整代碼塊上方我接收:

The fimename, directory name, or volume label syntax is incorrect. 

如果我使用:

if !q! == 0 (
    set myString=%%f  
    echo !m! >> !myString:~0,-12!\temporary.txt 
) 

程序運行時沒有錯誤,但從未創建文本文件。但是,如果我手動鍵入路徑(不可能迭代),則創建文本文件。我究竟做錯了什麼?


這裏是目錄佈局:C:\ mywork2 \ MAX \(arbitrary_name)\ dataSet.txt ||和C:\ mywork2 \ Trees_Orig \(系統發育樹)。

實施例的數據集(從系統發生樹):

#NEXUS 
begin taxa; 
    dimensions ntax=5020; 
    taxlabels 
    Tachyglossus_aculeatus 
    Zaglossus_bruijni 
    Zaglossus_bartoni 
    Ornithorhynchus_anatinus 
    Anomalurus_derbianus 
    Anomalurus_beecrofti 
    Anomalurus_pelii 
    Anomalurus_pusillus 
    Microtus_townsendii  
    Peromyscus_californicus 
    Odocoileus_hemionus 
    Canis_latrans 
    Canis_lupus 
    Urocyon_cinereoargenteus 
    Vulpes_vulpes 
    Lynx_lynx 
    Euchoreutes_naso 
    Jaculus_blanfordi 
    Jaculus_jaculus 
    Jaculus_orientalis 
    Stylodipus_andrewsi 
    Stylodipus_sungorus 
    Monodelphis_iheringi 

實施例的數據集(從dataSet.txt):

Species 
Microtus_townsendii  
Peromyscus_californicus  
Odocoileus_hemionus  
Canis_latrans  
Canis_lupus  
Urocyon_cinereoargenteus   
Vulpes_vulpes  
Lynx_lynx  
Puma_concolor  
Gulo_gulo  
Mephitis_mephitis  

變量名:

k: is used to simulate an array, by serving as the indexes to each species in dataSet.txt 
f: is a dataSet.txt from C:\mywork2\MAX\* 
i: represents the first tab delimited term of each dataSet.txt row (I've truncated this for you) 
g: is a .tre phylogenetic tree (there's 2), but I have truncated it's length due to example 
j: represents the first term of each row within the phylogenetic tree 

q: serves as a boolean variable which is 1 when a species from the phylogenetic tree 
    is also in the dataSet.txt file. Otherwise 0. 
n: serves as a boolean variable to tell when the batch file is examining species from the 
    phylogenetic tree and not meta data. 

m: is the index of each species within the phylogenetic tree. These are written to a text file 
    when q = 0. This is because I will be creating a Paup block within a .NEX file to remove all 
    species within the phylogenetic tree not present in dataSet.txt. 

myString: this variable is used to hold a truncated file path. I want each temporary.txt file to 
      be in the same directory as its respective dataSet.txt file. 

編輯:這是我的代碼 - >

setlocal EnableDelayedExpansion 
SET "TabChar= " 

for /r C:\mywork2\MAX\ %%f in (*) do (
    set k=0 
    for /f "delims=%TabChar%" %%i in (%%f) do ( 
     if NOT %%i == Species (
      set /A k+=1 
      set elem[!k!]=%%i 
     ) 
    ) 

    for /r C:\mywork2\Trees_Orig\ %%g in (*) do (
     set m=0 
     set n=0 

     for /f %%j in (%%g) do (
      set q=0 

      if !n! == 1 (
       set /A m+=1 
       for /L %%i in (1,1,!k!) do (
        if !elem[%%i]! == %%j (
         set q=1 
        ) 
       ) 

       if !q! == 0 (
        set myString=%%f  
        echo !m! >> !myString:~0,-12!\temporary.txt 
       ) 
      ) 

      if %%j == taxlabels (
       set n=1 
      ) 

      if %%j == Monodelphis_iheringi (
       set n=0 
      ) 

     ) 

     PAUSE 
    ) 
) 

PAUSE 
+0

請發表您足夠的代碼,使別人的問題的再現機器。包括一個代表性的子樹列表和足夠的變量目的細節以便分析。 – Magoo

+0

現在處理變量。 – user3834916

+0

更好嗎? – user3834916

回答

2

有足夠的聖誕節期間的精神給我留下回應。

nq被用作標誌。更好地命名爲目的標誌刪除要求以參考文檔。而標誌設置爲1/0和使用delayedexpansion訪問他們的動態值將工作,更好的方案是將它們設置爲什麼東西然後用if defined flagvalue其中工程delayedexpansion是否被調用或沒有。

mk是計數器值,更好地命名爲目的計數器則不需要文檔。

mystring是另一個沒有意義的變量名。更好地命名爲destinationdirectory來記錄目的。設置這個變量沒有任何意義 - 它完全取決於%%f,因此在for %%f塊的起始處設置它意味着它不會每次執行內循環時都會爲每個%%f設置一次(在數據提供,一次15次。)

分配給mystring的值也顯得不穩定 - 依賴於文件名的長度在%%f。由於您只是選擇驅動器和路徑,因此%%~dpf是更好的選擇。

如果你用引號括的目標文件名,進程創建的明顯預期,因爲這是在同一目錄下的另一個文件是由%%f掃描,接着文件名temporary.txt會出現在目錄和目標文件您的進程將嘗試使用該文件作爲數據源,試圖創建dirname\t\temporary.txt,並且該目錄不存在(tdirname\temporary.txt來的dirname\temporary.txt由子字符串處理到位置-12。使用%%~dpf可以解決該問題(但不掃描文件由此創建)

要跳過對temporary.txt文件的掃描,您可以在for ... %%f...do之後添加if /i "%%~nxf" neq "temporary.txt"作爲門,或者使用dir/s/b掃描所需目錄作爲源%%f的列表,因爲該列表在處理髮生之前完全生成,所以不包括在該過程中生成的文件。

我從提供的數據中獲得的結果是1 2 3 4 5 6 7 8 17 18 19 20 21 22 23但是您沒有發佈預期結果,所以我不知道該結果是否正確。

+0

非常感謝,它的作品!我迫不及待想要閱讀%%〜dpf,%%〜nxf和掃描。 – user3834916

1

只有一些注射:

  • 省略"delims=%TabChar%",如下列任何一項都總是有效的分隔符:逗號,分號;的Equals=空間標籤
  • 比較wi th ==總是文本;因此省略所有的==周圍空間;爲了最好的結果,將比較的字符串與例如報價如下:if NOT "%%i"=="Species" ( ...
  • 不是重定向回聲到一個文件的徹頭徹尾,儘量echo !m! ^>^> !myString:~0,-12!\temporary.txt看到所有命令
相關問題