简体   繁体   English

此批处理文件代码有什么作用?

[英]What does this batch file code do?

What does this bat code do? 这个蝙蝠代码做什么?

for /f %%i in ('dir /b Client\Javascript\*_min.js') do (
    set n=%%~ni
    set t=!n:~0,-4!
    cp Client\Javascript\%%i build\Client\Javascript\!t!.js
)

What does %%~ni,~n:~0,-4!,%%i,!t! %%~ni,~n:~0,-4!,%%i,!t! mean? 意思?

Keep in mind that in batch files, you need to escape percentage signs unless you're referring to arguments given to the batch file. 请记住,在批处理文件中,除非引用分配给批处理文件的参数,否则需要转义百分号。 Once you remove those, you get 删除这些内容后,您将获得

for /f %i in ('dir /b Client\Javascript\*_min.js') do (
        set n=%~ni
        set t=!n:~0,-4!
        cp Client\Javascript\%i build\Client\Javascript\!t!.js
)

%i is the declaration of a variable used to place the current file for has found. %i是用于放置已找到的当前文件的变量的声明。 %~ni extracts the filename portion of %i. %〜ni提取%i的文件名部分。 !n:~0,-4! n:〜0,-4! uses delayed expansion to remove the last four characters from %n% (set in the previous line) !t! 使用延迟扩展从%n%中删除最后四个字符(在上一行中设置)!t! is simply delayed expansion of the %t% variable set in the previous line. 只是延迟了上一行中设置的%t%变量的扩展。

Delayed expansion is used because otherwise, the variables will be substituted as soon as the line is encountered, and future iterations will not re-expand the variable. 之所以使用延迟扩展,是因为否则将在遇到该行后立即替换变量,并且以后的迭代将不会重新扩展该变量。

for /f %%i in ('dir /b Client\Javascript\*_min.js') do (

Iterate over every file in the Client\\Javascript folder that match "*_min.js". The 遍历Client\\Javascript文件夹中与"*_min.js". The匹配的每个文件"*_min.js". The "*_min.js". The dir command and for /f` are totally unneeded here, though and only complicate things, especially when file names contain spaces, commas and the like. "*_min.js". The DIR command and用于/ F`是完全不必要在这里,虽然在文件名中包含空格,逗号等,并且只有特别复杂的事情。 A more robust and simpler alternative would be 一个更强大,更简单的替代方法是

for %%i in (Client\Javascript\*_min.js) do (

But that's just beside the point. 但这只是重点。 People tend to write unelegant batch files sometimes, ignoring the pitfalls and common errors. 人们有时会写出混乱的批处理文件,而忽略了陷阱和常见错误。 That's just one example of that. 那只是一个例子。

        set n=%%~ni

Creates a variable n , containing the file name (without any directory information or extension) of the file currently processed. 创建一个变量n ,其中包含当前正在处理的文件的文件名(不包含任何目录信息或扩展名)。 We remember that the for statement iterates over every file it finds. 我们记得, for语句遍历它找到的每个文件。 With this line starts what it does with those files. 这行代码开始处理这些文件。

        set t=!n:~0,-4!

Creates a second variable, t , containing everything but the last four characters of the file name. 创建第二个变量t ,其中包含文件名的最后四个字符以外的所有内容。 This essentially strips away the "_min" 这实质上剥夺了"_min"

        cp Client\Javascript\%%i build\Client\Javascript\!t!.js

Finally, this copies the original file to the directory build\\Client\\Javascript with the new name, just constructed. 最后,这会将原始文件复制到刚刚构建的具有新名称的目录build\\Client\\Javascript So a file like Client\\Javascript\\foo_min.js will be copied to Client\\Javascript\\foo.js . 因此,像Client\\Javascript\\foo_min.js这样的文件将被复制到Client\\Javascript\\foo.js The !t! !t! here is just a delayed-evaluated environment variable. 这只是一个延迟评估的环境变量。 More on that below. 下面的更多内容。 Here it should suffice that it just inserts the contents of said variable at that point in the line. 这里只需在行中的该点插入所述变量的内容就足够了。

Again, bad practice here that will break in numerous interesting ways: 同样,这里的错误做法将以多种有趣的方式打破:

  1. cp is not a command on Windows so this batch will assume cygwin, GNUWin32 or similar things installed. cp在Windows上不是命令,因此此批处理将假定安装了cygwin,GNUWin32或类似的东西。 I tend to avoid having too many unneeded dependencies and stick to what Windows provides; 我倾向于避免有太多不必要的依赖关系,并坚持使用Windows提供的功能。 in this case the copy command. 在这种情况下, copy命令。 Two bytes won't kill anyone here, I think. 我认为,两个字节不会杀死任何人。
  2. No quotes are around either argument. 两种说法都没有引号。 Leads to interesting results when spaces start appearing in the file name. 当文件名中开始出现空格时,将导致有趣的结果。 Not good, either. 也不好。

As for why delayed expansion was used ( ! instead of % surrounding the variables: The for command consists of everything in the block delimited by parentheses here as well. The entire block is parsed at once and normal variable expansion takes place when a line/command is parsed. That would mean that every variable in the block would be evaluated before the loop even runs, leaving just the following: 至于为什么使用延迟扩展( !而不是变量周围的%for命令也包含块中由括号括起来的所有内容。整个块被立即解析,并且在执行行/命令时进行常规变量扩展将被解析,这意味着该块中的每个变量都将在循环甚至运行之前进行评估,只剩下以下内容:

for /f %%i in ('dir /b Client\Javascript\*_min.js') do (
        set n=%%~ni
        set t=
        cp Client\Javascript\%%i build\Client\Javascript\.js
)

which is certainly not what you want in this case. 在这种情况下,这当然不是您想要的。

Delayed expansion is always needed when creating and using variables in a loop such as this. 在这样的循环中创建和使用变量时,总是需要延迟扩展。 A workaround not needing delayed expansion would be to offload the loop interior into a subroutine: 一种不需要延迟扩展的解决方法是将循环内部卸载到子例程中:

for /f %%i in ('dir /b Client\Javascript\*_min.js') do call :process "%%i"
goto :eof
:process
set n=%~n1
set t=%n:0,-4%
copy "Client\Javascript\%~1" "build\Client\Javascript\%t%.js"
goto :eof

Since the subroutine is not a single "block" (something delimited by parentheses) it will be parsed line by line as usual. 由于子例程不是单个的“块”(用括号分隔),因此将照常逐行对其进行解析。 Therefore it's safe to use normal expansion instead of delayed expansion here. 因此,在此处使用常规扩展而不是延迟扩展是安全的。

%~ni expands to just the filename part of i . %~ni扩展为i的文件名部分。

!n:~0,-4! expands to all but the last four characters of n . 扩展到n的最后四个字符以外的所有字符。

In general, help for at the command prompt will give an overview of the multitude of ways for can expand variables these days. 通常,命令提示符help for将概述这些天for扩展变量的多种方式。

A complete help for the FOR command can be found on the Microsoft TechNet site . 可以在Microsoft TechNet网站上找到FOR命令的完整帮助。 See here for more information on delayed expansion : 有关延迟扩展的更多信息,请参见此处

// Pseudo code
for each file named *_min.js in the specified directory
    n is set to the file name (*_min)
    t is set to the file name, excluding the last 4 characters (*)
    the file is copied and renamed t.js to the specified directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM