Windows Get File Path

  1. Windows Find File Path
  2. Find File Path Windows 10
Active7 months ago

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 Earwicker
97.4k31 gold badges184 silver badges262 bronze badges
Misha MoroshkoMisha Moroshko
65.8k184 gold badges433 silver badges662 bronze badges

6 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.

Dean HardingDean HardingWindows Get File Path
61.9k8 gold badges125 silver badges170 bronze badges

%~dp0 may be a relative path.To convert it to a full path, try something like this:

dss539
5,3192 gold badges26 silver badges61 bronze badges
ArnaudArnaud

You can use following script to get the path without trailing '

duanduan

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.

HayzHayz

That would be the %CD% variable.

%CD% returns the current directory the batch script is in.

RuelRuel
12.6k5 gold badges31 silver badges46 bronze badges

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.

GetAndy Korneyev
23.9k15 gold badges58 silver badges62 bronze badges
JonasJonas

Not 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.

Note The GetFullPathName function is not recommended for multithreaded applications or shared library code. For more information, see the Remarks section.

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.

Tip Starting in Windows 10, version 1607, for the unicode version of this function (GetFullPathNameW), you can opt-in to remove the MAX_PATH character limitation without prepending '?'. See the 'Maximum Path Limitation' section of Naming Files, Paths, and Namespaces for details.

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.

Windows get file path cmd

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
GetFullPathName does not convert the specified file name, lpFileName. If the specified file name exists, you can use GetLongPathName or GetShortPathName to convert to long or short path names, respectively.

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.

Note Although the return value in this case is a length that includes the terminating null character, the return value on success does not include the terminating null character in the count.
Multithreaded applications and shared library code should not use the GetFullPathName function and should avoid using relative path names. The current directory state written by the SetCurrentDirectory function is stored as a global variable in each process, therefore multithreaded applications cannot reliably use this value without possible data corruption from other threads that may also be reading or setting this value. This limitation also applies to the SetCurrentDirectory and GetCurrentDirectory functions. The exception being when the application is guaranteed to be running in a single thread, for example parsing file names from the command line argument string in the main thread prior to creating any additional threads. Using relative path names in multithreaded applications or shared library code can yield unpredictable results and is not supported.

In Windows 8 and Windows Server 2012, this function is supported by the following technologies.

TechnologySupported
Server Message Block (SMB) 3.0 protocolYes
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 clientWindows XP [desktop apps | UWP apps]
Minimum supported serverWindows Server 2003 [desktop apps | UWP apps]
Target PlatformWindows
Headerfileapi.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also