windows命令行一些常见问题

来源:互联网 发布:西安淘宝运营人才兼职 编辑:程序博客网 时间:2024/06/05 13:23
  1. 带空格的路径
    如果命令路径有空格,需要加”“来运行,否则会有“C:\Program”命令找不到的错误。
    • 经常有一些程序的启动脚本写的不完善,需要自己去手动去加“”在变量上。
    • 如果是需要在命令行中使用的命令,尽量单独放在某目录下,这样可以最大程度减少路径问题引发的错误。
"C:\Program Files\java\jdk\bin\java" -version#或者cd C:\Program^ Filescd "%ProgramFiles%\batch.cmd"cd "%ProgramFiles(x86)%\batch.cmd"

2.junction 软连接
A junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer. Otherwise, junctions operate identically to hard links. Junctions are implemented through reparse points.

C:\Windows\system32>mklink /J "C:\Program_Files" "C:\Program Files"# ouptut Junction created for C:\Program_Files <<===>> C:\Program Files

3.%~dp0的使用
- %0-9是输入到batchfile的输入参数占位符;
- %后面跟一个~,可以在参数前加一个修饰符;
- d表示驱动器,如C、D盘符;
- p表示当前文件的路径;
例如,新建一个test.bat

@echo offrem 打印当前路径,与%CD%效果相同pushd %~dp0../ #展开当前路径并改变当前路径为上一层

引用stackoverflow上的一段解释如下

The %~dp0 (that’s a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file.
The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.
If you follow the percent character (%) with a tilde character (~), you can insert a modifier(s) before the parameter number to alter the way the variable is expanded. The d modifier expands to the drive letter and the p modifier expands to the path of the parameter.
Example: Let’s say you have a directory on C: called bat_files, and in that directory is a file called example.bat. In this case, %~dp0 (combining the d and p modifiers) will expand to C:\bat_files.
And a more clear reference from here:
%CmdCmdLine% will return the entire command line as passed to CMD.EXE
%* will return the remainder of the command line starting at the first command line argument (in Windows NT 4, %* also includes all leading spaces)
%~dn will return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC)
%~pn will return the directory of %n if %n is a valid path or file name (no UNC)
%~nn will return the file name only of %n if %n is a valid file name
%~xn will return the file extension only of %n if %n is a valid file name
%~fn will return the fully qualified path of %n if %n is a valid file name or directory

参考
windows命令索引 https://ss64.com/nt/
robvanderwoude’s Scripting Pages http://www.robvanderwoude.com/batchstart.php

0 0
原创粉丝点击