Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使文件路径成批正常工作(写入文本文件)_Windows_Batch File - Fatal编程技术网

Windows 使文件路径成批正常工作(写入文本文件)

Windows 使文件路径成批正常工作(写入文本文件),windows,batch-file,Windows,Batch File,我正在遍历大约300个目录,并试图在每个目录中创建一个文本文件。除了文件创建和后续编写之外,我的所有代码都可以工作。以下是我尝试写入文件的方式:(我应该注意,此代码位于使用setlocal EnableDelayedExpansion的嵌套for循环中) 其中%%f是我正在分析的另一个文本文件的路径,因此我必须缩短路径。此文本文件的名称在所有目录中都是统一的。我试过使用: echo !myString! 这将生成(正确的文件路径)的示例输出: 但是,当我运行上面的完整代码块时,我收到: The

我正在遍历大约300个目录,并试图在每个目录中创建一个文本文件。除了文件创建和后续编写之外,我的所有代码都可以工作。以下是我尝试写入文件的方式:(我应该注意,此代码位于使用setlocal EnableDelayedExpansion的嵌套for循环中)

其中%%f是我正在分析的另一个文本文件的路径,因此我必须缩短路径。此文本文件的名称在所有目录中都是统一的。我试过使用:

echo !myString!
这将生成(正确的文件路径)的示例输出:

但是,当我运行上面的完整代码块时,我收到:

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\(任意名称)\dataSet.txt;|和C:\mywork2\Trees\u Orig\(系统发育树)

示例数据集(来自系统发育树):

示例数据集(来自dataSet.txt):


变量名:

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.

编辑:这是我的代码-->


还有足够的圣诞精神留给我去回应

n
q
被用作标志。最好命名为purposeflag,以删除参考文档的要求。虽然将标志设置为1/0并使用delayedexpansion访问其动态值会起作用,但更好的方案是将它们设置为nothing或其他值,然后使用
if defined flagvalue
,无论是否调用delayedexpansion都会起作用

m
k
是计数器值,最好命名为purposecounter,这样就不需要文档

mystring
是另一个无意义的变量名。更好地命名为
destinationdirectory
,以记录目的。在执行此操作时设置此变量没有意义-它完全取决于
%%f
,因此在%%f块的
开头设置此变量意味着只为每个
%%f
设置一次,而不是每次执行内部循环时(根据提供的数据,每15次设置一次)

分配给
mystring
的值也不稳定-取决于
%%f
中文件名的长度。由于您只需选择驱动器和路径,
%%~dpf
是一个更好的选择

如果用引号括住目标文件名,进程按照预期创建目标文件,但是由于这是与被
%%f
扫描的目录相同的另一个文件,因此文件名
temporary.txt
将出现在该目录中,并且您的进程将尝试将该文件用作数据源,试图创建
dirname\t\temporary.txt
但此目录不存在(
t
来自子字符串处理的
dirname\temporary.txt
,定位
-12
。使用
%%~dpf
可以解决此问题(但不能扫描此过程创建的文件)

要跳过对
temporary.txt
文件的扫描,您可以添加
if/i“%%~nxf”neq“temporary.txt”
作为
之后的一个门,用于…%%f…执行
或使用所需目录的
目录/s/b
扫描作为源
%%f
的列表,因为该列表将在处理发生之前完全生成,因此排除在处理过程中生成的文件

我从提供的数据中获得的结果是1 2 3 4 5 6 7 8 17 18 19 20 21 22 23,但您尚未发布预期结果,因此我不知道该结果是否正确。

仅某些注射:

  • 省略
    “delims=%TabChar%”
    ,因为以下任何一个始终是有效的分隔符:逗号
    ,分号
    ,等于
    =
    ,空格
    和制表符
  • =
    的比较始终是文本性的;因此,省略所有
    =
    周围的空格;为了达到最佳效果,请用引号将比较过的字符串括起来,例如:
    如果不是“%%i”==“Species”(
  • 不要直接将echo重定向到文件,请尝试
    echo!m!^>^>!myString:~0,-12!\temporary.txt
    查看所有命令的原样

请发布足够的代码,以便在其他机器上重现问题。包括一个具有代表性的子树列表和足够详细的变量用途,以便进行分析。现在就开始处理变量。非常感谢,它很有效!我迫不及待地要阅读%%~dpf、%%~nxf和扫描。
if !q! == 0 (
    set myString=%%f    
    echo !m! >> !myString:~0,-12!\temporary.txt
)
#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
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