我遍历了大约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