How to get filename only without path in windows command line?



When working with files in a Windows command line, it is often necessary to extract only the filename from the full path. This is accomplished through the use of a few different techniques, depending on the specific situation.

The most commonly used method to extract the filename from the full path is to use the FOR command. This command allows you to parse the full path by specifying the particular characters that you want to extract. For example, if you wanted to extract the filename from the full path “C:\\My Documents\\MyFile.txt”, you would use the following command:

FOR /f \tokens=1* delims=\ %A IN (\C:\\My Documents\\MyFile.txt\) DO @echo %B

This command will parse the full path and output only the filename, in this case “MyFile.txt”. This technique is useful when you need to extract filenames from multiple paths in a batch file, as it can be used in a looping construct.

Another approach to extracting filenames from the full path is to use the DIR command. This command will list the contents of a directory, and you can specify the directory that you want to list. For example, if you wanted to list the contents of “C:\\My Documents”, you would use the following command:

DIR \C:\\My Documents\

This command will list the contents of the specified directory, and the output will include the filenames. This technique is useful when you need to extract filenames from a single directory, as it is much easier to parse the output of the DIR command than it is to parse the full paths.

Finally, a slightly more complicated technique involves using the Windows Script Host (WSH). This is a scripting language built into Windows, and it can be used to manipulate files and directories. Specifically, you can use the FileSystemObject object to extract filenames from a full path. The following example shows how to do this:

Set objFSO = CreateObject(\Scripting.FileSystemObject\)

Set objFile = objFSO.GetFile(\C:\\My Documents\\MyFile.txt\)

WScript.Echo objFile.Name

This script will parse the full path and output only the filename, in this case “MyFile.txt”. This technique is useful when you need to extract filenames from a single path, as it is much easier to use a script than it is to use the FOR command.

In summary, there are several different techniques for extracting filenames from a full path in Windows. The FOR command is the most commonly used technique, as it allows you to parse multiple full paths in a batch file. The DIR command is useful when you need to extract filenames from a single directory, and the Windows Script Host can be used to extract filenames from a single path.

Inquire Now

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Find your next full or part-time role here

ScaleDesk can either help you find your next full time gig or a part time side gig where you can create extra income!

Onboard with us once
Skip HR screening and go to the final interview with with only your resume and a video interview you never have to redo
Get paid electronically every month for the hours you work
We will be your reference even if you work for us once

IT Teams: Use ScaleDesk to augment your team

Schedule Demo