I know that %0
contains the full path of the batch script, e.g. c:pathtomyfileabc.bat
I would path
to be equal to c:pathtomyfile
This article covers one of the simplest things one can do in Windows. Learn how to find file path in Windows 10 for your every day usage. Windows 10 is a mixture of the simple and the powerful. Oh, did I mention beauty somewhere? If not then yes, beauty as well. These 3 words were. Aug 21, 2017 How to Find a File's Path on Windows. This wikiHow teaches you how to find the full path to a file using Windows Search, File Explorer, or the Run command window. Create a bat file in some convenient directory then you could copy+paste the short path from that path. You could just run command.com and keep doing cd commands to your current directory too. In Windows batch scripts,%s1 expands path parameters to short names. Create this batch file: @ECHO OFF echo%s1.
How could I achieve that ?
Daniel Earwicker6 Answers
%~dp0
will be the directory. Here's some documentation on all of the path modifiers. Fun stuff :-)
To remove the final backslash, you can use the :n,m
substring syntax, like so:
I don't believe there's a way to combine the %0
syntax with the :~n,m
syntax, unfortunately.
%~dp0
may be a relative path.To convert it to a full path, try something like this:
You can use following script to get the path without trailing '
You can use %~dp0
, d means the drive only, p means the path only, 0 is the argument for the full filename of the batch file.
For example if the file path was C:UsersOliverDesktopexample.bat then the argument would equal C:UsersOliverDesktop, also you can use the command set cpath=%~dp0 && set cpath=%cpath:~0,-1%
and use the %cpath%
variable to remove the trailing slash.
That would be the %CD%
variable.
%CD%
returns the current directory the batch script is in.
I am working on a Windows 7 machine and I have ended up using the lines below to get the absolute folder path for my bash script.
I got to this solution after looking at http://www.linuxjournal.com/content/bash-parameter-expansion.
Andy KorneyevNot the answer you're looking for? Browse other questions tagged windowsbatch-file or ask your own question.
-->Retrieves the full path and file name of the specified file.
To perform this operation as a transacted operation, use theGetFullPathNameTransacted function.
For more information about file and path names, seeFile Names, Paths, and Namespaces.
Syntax
Parameters
Windows Find File Path
lpFileName
The name of the file.
This parameter can be a short (the 8.3 form) or long file name. This string can also be a share or volumename.
In the ANSI version of this function, the name is limited to MAX_PATH characters.To extend this limit to 32,767 wide characters, call the Unicode version of the function (GetFullPathNameW), and prepend'?' to the path. For more information, seeNaming a File.
nBufferLength
The size of the buffer to receive the null-terminated string for the drive and path, inTCHARs.
lpBuffer
A pointer to a buffer that receives the null-terminated string for the drive and path.
lpFilePart
A pointer to a buffer that receives the address (within lpBuffer) of the finalfile name component in the path.
This parameter can be NULL.
If lpBufferrefers to a directory and not a file, lpFilePart receives zero.
Return Value
If the function succeeds, the return value is the length, in TCHARs, of thestring copied to lpBuffer, not including the terminating null character.
If the lpBuffer buffer is too small to contain the path, the return value is thesize, in TCHARs, of the buffer that is required to hold the path and theterminating null character.
If the function fails for any other reason, the return value is zero. To get extended error information, callGetLastError.
Remarks
Find File Path Windows 10
GetFullPathName merges the name of the current driveand directory with a specified file name to determine the full path and file name of a specified file. It alsocalculates the address of the file name portion of the full path and file name.
This function does not verify that the resulting path and file name are valid, or that they see an existingfile on the associated volume.
Note that the lpFilePart parameter does notrequire string buffer space, but only enough for a single address. This is because it simply returns an addresswithin the buffer that already exists for lpBuffer.
Share and volume names arevalid input for lpFileName. For example, the following list identities the returned pathand file names if test-2 is a remote computer and U: is a network mapped drive whose current directory is the root of the volume:
- If you specify 'test-2q$lh' the path returned is 'test-2q$lh'
- If you specify '?UNCtest-2q$lh' the path returned is '?UNCtest-2q$lh'
- If you specify 'U:' the path returned is the current directory on the 'U:' drive
If the return value is greater than or equal to the value specified innBufferLength, you can call the function again with a buffer that is large enough tohold the path. For an example of this case in addition to using zero-length buffer for dynamic allocation, see theExample Code section.
In Windows 8 and Windows Server 2012, this function is supported by the following technologies.
Technology | Supported |
---|---|
Server Message Block (SMB) 3.0 protocol | Yes |
SMB 3.0 Transparent Failover (TFO) | Yes |
SMB 3.0 with Scale-out File Shares (SO) | Yes |
Cluster Shared Volume File System (CsvFS) | Yes |
Resilient File System (ReFS) | Yes |
Examples
The following C++ example shows a basic use ofGetFullPathName,GetLongPathName, andGetShortPathName. For another example using dynamicallocation, see GetShortPathName.
Requirements
Minimum supported client | Windows XP [desktop apps | UWP apps] |
Minimum supported server | Windows Server 2003 [desktop apps | UWP apps] |
Target Platform | Windows |
Header | fileapi.h (include Windows.h) |
Library | Kernel32.lib |
DLL | Kernel32.dll |