我正在遍历大约 300 个目录并尝试在每个目录中创建一个文本文件。除了文件创建和后续编写之外,我的所有代码都有效。以下是我尝试写入文件的方式:(我应该注意到这段代码位于使用 setlocal EnableDelayedExpansion 的嵌套 for 循环中)
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
最佳答案
我有足够的圣诞精神来回应。
n 和 q 被用作标志。最好将其命名为目的标志以移除引用文档的要求。虽然将标志设置为 1/0 并使用 delayedexpansion 来访问它们的动态值将起作用,但更好的方案是将它们设置为nothing 或 something 然后使用 if定义的 flagvalue 无论是否调用 delayedexpansion 都有效。
m 和 k 是计数器值,最好命名为purposecounter 然后就不需要文档了。
mystring 是另一个无意义的变量名。最好命名为 destinationdirectory 来记录目的。在你正在做的地方设置这个变量是没有意义的 - 它完全取决于 %%f 所以在 for %%f block 的开头设置它意味着它将只为每个 %%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" 作为门在 for ... %%f...do 之后或使用所需目录的 dir/s/b 扫描作为源 %% 的列表f 因为该列表将在处理发生之前完全生成,所以不包括在该过程中生成的文件。
我从提供的数据中获得的结果是 1 2 3 4 5 6 7 8 17 18 19 20 21 22 23 但您没有发布预期结果,所以我不知道该结果是否正确。
关于windows - 让 FilePaths 在 Batch 中正常工作(写入文本文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664810/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall
之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m