我遍歷了大約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
請發表您足夠的代碼,使別人的問題的再現機器。包括一個代表性的子樹列表和足夠的變量目的細節以便分析。 – Magoo
現在處理變量。 – user3834916
更好嗎? – user3834916