When running a bat script as an administrator in win7/10, get the directory where the current file is located

https://blog.csdn.net/tsvico/article/details/78050884

This command is suitable for the case where bat is double-clicked to open normally, and the administrator opens the flashback.

The following is reproduced, the address is http://blog.chinaunix.net/uid-27000874-id-3224772.html

I know that the bat script of windows is very powerful, but the specific commands have always been rarely touched. Today, when running a script that I wrote before to install the mysql database to the system service on win7, I encountered some problems. I will record it below.

 

Problem: The mysql service installation script fails to run under win7

 

Reason: Under win7, the default directory entered by right-clicking "Run as administrator" is the C:\Windows\System32 directory, so the subsequent cd Demo_V2.0.4.9 command must not enter the correct directory

 

Workaround: use cd /d %~dp0

First of all, the script is as follows, not much explanation for the specific StartDemo.bat command

cd Demo_V2.0.4.9

cd mysql

 

@echo off

set MYSQL_HOME=%cd%

 

net stop "DemoMySql"

call "%MYSQL_HOME%\bin\mysqld.exe" remove  DemoMySql

 

call "%MYSQL_HOME%\bin\mysqld.exe" install DemoMySql --defaults-file="%MYSQL_HOME%\bin\my.ini"

net start "DemoMySql"

 

cd..

cd..

 

This script can be double-clicked to run normally in xp, 2000, 2003 and other systems. When double-clicking to run in win7 system, it will run as a normal user, and the file path obtained at this time is indeed the current path, not C:\Windows\System32. However, when running to uninstall and install DemoMysql system services, ordinary users obviously have insufficient permissions.

So right-click on StartDemo.bat and select "Run as administrator", and then there will be a problem again. Win7 may be out of security concerns. The directory obtained at this time is C:\Windows\System32, so the subsequent execution will be wrong or invalid.

At this time, the script starts to try to add the command cd %cd% to get the current path. The experiment shows that this line of statement is valid in systems such as xp, but it is still invalid in win7. The resulting directory is still C:\Windows\System32.

I checked the Internet and found out that I need to use the cd /d %~dp0 command to get the directory where the script is located. Just add cd /d %~dp0 at the beginning of the script. After running this script on the xp system, it is confirmed that there is no problem. The parameters involved in the command are explained below.

Problem explanation 1: About the /d parameter of cd

Regarding the /d parameter of cd, type cd /? in cmd

You can see the interpretation of the /d parameter as follows:

Using the /D command line switch, in addition to changing the current directory of the drive,

The current drive can also be changed.

 

This sentence does not seem to be easy to understand. I will do an experiment and show it to everyone to understand:

Usually when we open the cmd window in the xp system, it will display

C:\Documents and Settings\Administrator>

If we execute the following command, we find that the directory is still in C:\Documents and Settings\Administrator

C:\Documents and Settings\Administrator>cd d:\tomcat6.0.18

 

C:\Documents and Settings\Administrator>

At this point, if we type d:, it will not only switch to the d drive, but also switch to the directory of d:/tomcat6.0.18

C:\Documents and Settings\Administrator>cd d:\tomcat6.0.18

 

C:\Documents and Settings\Administrator>d:

 

D:\tomcat6.0.18>

下面语句我们就能看到/d参数的作用了。发现加了/d参数之后直接切换到d盘的tomcat6.0.18目录了。

 

 

C:\Documents and Settings\Administrator>cd /d d:\tomcat6.0.18

 

D:\tomcat6.0.18>

 

结论:不加/d参数只能在同一驱动器的目录之间切换,加上/d参数则能在不同驱动器之间的目录之间切换

 

问题解释二:关于%~dp0的批处理命令的详细解释

对此命令并不清楚,以下内容都来自互联网:

%~dp0 “d”为Drive的缩写,即为驱动器,磁盘、“p”为Path缩写,即为路径,目录

cd是转到这个目录,不过我觉得cd /d %~dp0 还好些

 

选项语法:

    ~0         - 删除任何引号("),扩充 %0

    %~f0        - 将 %0 扩充到一个完全合格的路径名(“f”是file,即文件)

    %~d0        - 仅将 %0 扩充到一个驱动器号

    %~p0        - 仅将 %0 扩充到一个路径

    %~n0        - 仅将 %0 扩充到一个文件名(“n”是name 文件名)

    %~x0        - 仅将 %0 扩充到一个文件扩展名

    %~s0        - 扩充的路径只含有短名(“s”为Short,短的)

    %~a0        - 将 %0 扩充到文件的文件属性(“a”为attribute,即属性)

    %~t0        - 将 %0 扩充到文件的日期/时间(“t”time)

    %~z0        - 将 %0 扩充到文件的大小(Size 大小)

    %~$PATH:0   - 查找列在路径环境变量的目录,并将 %0 扩充

                  到找到的第一个完全合格的名称。如果环境变量名

                  未被定义,或者没有找到文件,此组合键会扩充到

                  空字符串

 

可以组合修饰符来得到多重结果:

    %~dp0       - 仅将 %0 扩充到一个驱动器号和路径

    %~nx0       - 仅将 %0 扩充到一个文件名和扩展名

    %~fs0       - 仅将 %0 扩充到一个带有短名的完整路径名

    %~dp$PATH:0 - 查找列在路径环境变量的目录,并将 %I 扩充

                  到找到的第一个驱动器号和路径。

    %~ftza0     - 将 %0 扩充到类似输出线路的 DIR

 

%0为当前批处理文件

如果0换成1为第一个文件,2为第2个

 

****************************************************

 

%0代指批处理文件自身

%~d0 是指批处理所在的盘符

%~dp0 是盘符加路径

 

cd %~dp0 就是进入批处理所在目录了

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324438211&siteId=291194637