Relative path issues with batch files

Source: Internet
Author: User

When registering the control, I usually put a reg.bat in the OCX or DLL directory, register the control when it will be more convenient, but encountered WIN7 such as the requirements of the system, a bit frustrating, always prompt to find the control depends on the other binary files, such as IECS2.DLL dependent IMAP.DLL, in the registration IECS2.DLL The problem that appears as shown in the


The obvious problem is that the path is wrong, but what exactly is the path?


In general, when a batch file handles relative paths, the starting position is the directory where the bat file resides, such as executing a bat file in the D:\XX directory, and all relative directory operations are performed under D:\XX.

However, some commands require administrator privileges, and when you right-click Run as Administrator, the starting position is C:\Windows\system32, which is often not what we want,

One solution is to add a "CD D:\XX" to this directory at the beginning of the batch to modify its current location, but this requires the absolute path to be added to the batch file, and if the bat file is moved to a different location, then a better way is to use "cd/d%~dp0" instead of " CD D:\XX ".


In summary, when writing the bat file, regardless of whether you need to use administrator rights, it is best to add a "cd/d%~dp0" at the beginning of the line to unify the file's starting directory.


So what does this cd/d%~dp0 mean?

%~dp0 "D" is the abbreviation for drive, that is, the driver, disk, "P" is the path abbreviation, that is, the directory CD is to go to this directory, but I think cd/d%~dp0 still better option syntax:    ~0         -Remove any quotation marks ("), expand%0    % ~f0        -Will%0 Expands to a fully qualified pathname ("F" is file, that is, files)    %~d0        -only%0 Extend to a drive letter    %~p0        -only%0 Extend to a path    %~n0        -only%0 Expand to a file name ("n" is the name file name)    %~x0        -only%0 Extended to a file extension    %~s0        -The expanded path contains only short names ("s" for short, shorter)    %~a0        -Will%0 File attributes that are expanded to a file ("A" is attribute, that is, attributes)    %~t0        -Will%0 Extended to File Date/time ("T" times)    %~z0        -Will%0 Expand to file size (size)    %~ $PATH: 0   -Find the directory that is listed in the PATH environment variable and add%0 Expands                  to the first fully qualified name found. If the environment variable name is                  not defined, or if no file is found, the key combination expands to an                  empty string to combine modifiers to get multiple results:    %~dp0       -only%0 Expand to a drive letter and path    %~nx0       -only%0 Extend to a file name and extension    %~fs0       -only%0 Expand to a full pathname with a short name    %~dp$path:0-Find the directory that is listed in the PATH environment variable and%I Expands                  to the first drive letter and path found.    %~ftza0     -Will%0 Expands to a similar output line dir%0 for the current batch file if 0 is replaced with 1 for the first file, and 2 for the 2nd

Examples Show




Relative path issues with batch files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.