批处理文件:将文件名从FOR循环传递给子文件

时间:2018-11-19 15:49:51

标签: batch-file

这个问题是关于将文件名参数传递给批处理文件中的子例程的正确语法的。以前应该问过类似这个问题的感觉,但是我似乎找不到正确的答案。假设在名为tabItem(tabName = "intro", div(img(src = "http://world.edu/wp-content/uploads/2017/12/moocXX.jpg", height = 200, width = 600), style="text-align: center;"), fluidRow( box( h2("Welcome to the Massive Open Online Course (MOOC) Instructor Dashboard", style="text-align: center;"), br(), p( span("This is an educational dashboard built by ", style = "font-size:20px"), a(href = "http://crossvalidated.rbind.io/","Jason Baik", style = "font-size:20px"), span("as part of the Research Experience for Undergraduates (REU) program at", style = "font-size:20px"), a(href = "https://cs.gmu.edu/~reu/","George Mason University.", style = "font-size:20px") ), br(), p( span("Intended audience: MOOC instructors and administrators", style = "font-size:20px;font-weight:bold") ), br(), p( h4("Explanation of Tabs", style = "font-size:25px"), span("Overview of Class: Total number of students, dropouts, final grades, and normalized module usage", style = "font-size:20px") ), p( span("Student Selector: Filters to customize view of dataset", style = "font-size:20px") ), p( span("Effort Level: Information on Weekly Effort Levels", style = "font-size:20px") ), p( span("Dropout Definition provided by", style = "font-size:20px"), a(href = "https://www.researchgate.net/profile/Anja_Lorenz/publication/263543544_Open_Online_Courses_in_the_context_of_higher_education_an_evaluation_of_a_German_cMOOC/links/54941c560cf2e1b6095f97bc/Open-Online-Courses-in-the-context-of-higher-education-an-evaluation-of-a-German-cMOOC.pdf#page=58", "Halawa, S., Greene, D., & Mitchell, J. (2014). Dropout Prediction in MOOCs using learner activity features. eLearning Papers, 37, 7-16.", style = "font-size:20px") ), 的文件夹中有三个文本文件。以下代码(部分从this answer借用)将输出文件名:

C:\Batch File Example

输出:

@echo off
SETLOCAL Enableextensions

ECHO Line before FOR
FOR /R "C:\Batch File Example\" %%i in (*.txt) DO ECHO %%i
ECHO Line after FOR & PAUSE>NUL

现在,我想通过使用子例程来产生相同的输出,如下所示:

Line before FOR
C:\Batch File Example\File1.txt
C:\Batch File Example\FIle2.txt
C:\Batch File Example\File3.txt
Line after FOR

但这会给出以下输出,结果将被截断:

@echo off
SETLOCAL Enableextensions

ECHO Line before FOR
FOR /R "C:\Batch File Example\" %%i in (*.txt) DO CALL :doecho %%i
ECHO Line after FOR & PAUSE>NUL
GOTO :EOF

:doecho
SET VAR=%1
ECHO VAR is %VAR%
EXIT /b

以上结果表明,空格被视为定界符,Line before FOR VAR is C:\Batch VAR is C:\Batch VAR is C:\Batch Line after FOR 仅包含%1,因此我尝试使用以下for循环,并带有C:\Batch标志和逗号分隔符以消除空格:

/F

但是,这还会返回截断的结果,唯一的区别是只有一次迭代而不是三次。

我尝试使用FOR /F "delims=," %%i IN ("C:\Batch File Example\*.txt") DO CALL :doecho %%i 之类的enhanced variable substitionts来代替%~I%%i,但是我遇到了麻烦。那我想念什么?

1 个答案:

答案 0 :(得分:2)

参数由空格分隔-因此C:\Batch File Example\file.txt三个参数(如果文件名还包含空格,则为更多)。

使用%*(“所有参数”)代替%1或使用引号:... CALL :doecho "%%I",则它是一个(带引号的)参数。如果需要删除子例程中的引号,请使用%~1