我试图编写一个简单的CMD脚本来自动生成我的回购。 这里是完整的脚本:
@echo off setlocal goto main :: Functions :buildSubdir pushd %1 for /f %%projectFile in ('dir /b /s project.json') do ( dnu restore "%%projectFile" dnu build "%%projectFile" dnu pack "%%projectFile" ) popd goto :EOF :main :: Check for dnu where dnu > NUL 2>&1 if %ERRORLEVEL% NEQ 0 ( echo dnu wasn't found in your PATH! 1>&2 echo See http://docs.asp.net/en/latest/getting-started/installing-on-windows.html for instructions on installing the DNX toolchain on your PC. 1>&2 exit /b %ERRORLEVEL% ) :: Do the actual work cd %~dp0 call :buildSubdir src call :buildSubdir test
基本上,它试图在几个select的目录( src和test )中find所有名为project.json的文件,然后对它们执行dnu restore , dnu build和dnu pack 。
出于某种原因,我似乎得到了一个语法错误,我input一个for /f循环,说%projectFile不被识别的东西。 当我删除@echo off语句并重新运行脚本时,我的terminal完整输出的要点如下。
谁能告诉我为什么发生这种情况,我能做些什么来解决这个问题? 谢谢。
Java程序将编译但不能在命令提示符下运行
如何识别Windows7 64位命令terminal中的ansi颜色转义码
PHP:在Windows命令行中回显UTF-8字符
在使用Windows命令提示符时获取R的输出和完整命令历史logging
编辑:只是改变它:
for /f %%p in ('dir /b /s project.json') do ( set projectFile=%%p dnu restore "%projectFile%" dnu build "%projectFile%" dnu pack "%projectFile%" )
仍然似乎没有工作,虽然现在的错误信息是不同的。 这是新产出的要点。 (请注意%projectFile%如何设置为空string。)
如何在Windows命令提示符下每5分钟安排一次任务?
“ping”不被识别为内部或外部命令可操作程序或batch file错误
在Windows批处理中运行Plink – 问题
for /f "delims=" %%p in ('dir /b /s /ad project.json') do ( dnu restore "%%p" dnu build "%%p" dnu pack "%%p" )
目录名称被分配给%%p ,因为这是你正在使用的所有内容,所以你不需要进一步分配它。
delims=确保将整行分配给%%p – 否则,将使用默认分隔符集为%%p分配第一个标记,其实际结果是在第一个空格处截断名称。
看到
for /?
从docco的提示。
/ad从dir输出中删除任何目录名称(以防万一有一个目录名称与提供的掩码相匹配 – 虽然可能性很小)
如果您想在%%p操作名称,而不是按原样使用它,则需要使用delayedexpansion或调用另一个例程来执行操作。 The basis of this characteristic is the delayedexpansion陷阱- batch will substitute the *parse-time* value of any The basis of this characteristic is the之前- batch will substitute the *parse-time* value of any %var% it finds in a code-block (parenthesised series of statements) for - batch will substitute the *parse-time* value of any %var% - batch will substitute the *parse-time* value of any ,因此自%projectFile%在for循环的开始处是未定义的,批处理将取代它当时的值。
有关文档,请参阅许多与delayedexpansion相关的文章,或阅读其中的少量文档
set /?
使用For构造函数,可以在For构造中使用字母作为变量:az和AZ(注意字母区分大小写很重要)。 有很多方法可以使用FOR,我会从这里开始 – http://ss64.com/nt/for_f.html
你也可以FOR /? 根据需要获得帮助。
我想你正在寻找这个:
@echo off setlocal goto main :: Functions :buildSubdir pushd %1 for /f %%p in ('dir /b /s project.json') do ( dnu restore "%%p" dnu build "%%p" dnu pack "%%p" ) popd goto :EOF :main :: Check for dnu where dnu > NUL 2>&1 if %ERRORLEVEL% NEQ 0 ( echo dnu wasn't found in your PATH! 1>&2 echo See http://docs.asp.net/en/latest/getting-started/installing-on-windows.html for instructions on installing the DNX toolchain on your PC. 1>&2 exit /b %ERRORLEVEL% ) :: Do the actual work cd %~dp0 call :buildSubdir src call :buildSubdir test
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。