[Bat] How to create a batch to get the file name by deleting the file extension with the command! Let's master "Dir", "For sentence" and "% ~".

Windows

Hello!This time I will introduce how to get the file name without the extension in batch.

The DIR command is one of the most popular of the many Windows commands and is often used to list file and folder information.

However, if you do this obediently, it will contain a lot of extra information and you may not be able to achieve your goals.

A lot of extra information will be revealed.As it is, it is difficult to handle in a batch file.


This timeBatch file to get only the file name excluding the file extensionI will introduce a sample of.

Thing you want to do

I want to get a list of file names with the extension removed from a specific folder.

Contents of Temp folder
Sample output on the console

As a sample this time, I created a folder called "Temp" directly under the C drive and put some text files in it.In this Temp folderGet a list of file names with the Dir commandAnd from the acquired dataExtract only the file nameI would like to realize it in the form of.

Script example

As an example to realize the above operation, I created a script as follows.

@echo off setlocal enabledelayedexpansion for / f %% A in ('dir / b c: \ temp \') do set fileName =%% ~ nA & echo! FileName!
endlocal pause

Run DirTarget folder pathI think that other paths can be supported by changing.
* Dir / b Directory path Is a command that outputs the name of the file in that directory.

Information is acquired for each file, so if there is a command you want to execute each timeBlue marker partIt is OK if you describe it in.At that time, please add "!" Instead of "%" on both sides of the used variable.

Also, the trick this time is "How to display the pathIs to devise.

Commentary

Role of Dir

The "Dir" command is a command to get a list of folders and files.When executed by itself, the list of folders and files in the current current folder is output.Therefore, you actually specify the file path to get the list of data in that folder.

Dir [file path]

However, if you execute it as it is, it will be a little troublesome to handle because it contains the date and time like the beginning and extra messages.When executing in a batch file, it is common to extract only the necessary data by using options.

If you add "/ b", only the file name will be output.

Dir / b [file path]
Actually executed

Very often this is done in a batch.

However, if nothing is done, the extension will remain.
To handle this, you need to "process the data line by line".

Role of For statement

for / f %% A in ('[command]') do set [variable name for storage] = %% A

The above command executes a single quotation mark: [command] surrounded by "'" and captures the result in% A. % A is a variable, but it will not work properly unless it is written as %% A in the batch file. The For statement can only be used in a batch file, so it will inevitably be %% A.

Also, this% A seems to be treated as an argument at the time of execution, and its behavior is different from other variables.
Thanks to its characteristics, the "% ~" used this time can be used.

[Command] uses "Dir / b folder path", so it is executed for each file.
In this case, the command below do is executed every time one file in the folder path is read.

Role of setlocal enabledelayedexpansion

"Set local enabled relayed expansion" is a command to enable delayed environment variables.
Delayed environment variables are not explained in detail here, but they are commands for using variables with "!" At both ends.

The contents of variables in the For statement do not change immediately even if they are assigned with the "set" command.
The value of the variable changes only after the For statement ends.Therefore, if you want to change the processing depending on the value assigned in the For statement, it will not work.

In such a case, if you enable this delay environment variable, you can proceed with the process using the variable value changed in the For statement by using "! Variable name!" Instead of "% variable name%". I will.

I hope you can think of it as a kind of magic when using "For".

In the script example, it is saved once in [Variable for saving], but if it is not necessary, you can do as follows.

for / f %% A in ('dir / bc: \ temp \') do echo %% ~ nA

The role of "% ~" to extract desired information from the file path

You can manipulate the obtained file path by adding a specific alphabet after% ~.In this case, the file path obtained by the For statement is obtained in "% A", so it is written as "% ~ # A". * # Is any alphabet
The contents that can be operated and the corresponding alphabets are summarized in a simple table.

Alphabet to addWhat is displayedDisplay example
% ~ d ○Path drive nameC:
% ~ p ○Directory path under the drive\ Temp \
% ~ n ○file nametest
% ~ x ○Extension name. Txt
% ~ f ○All file pathsC: \ Temp \ test.txt

By combining these, you can freely change the file path to be extracted to some extent.

Alphabet to addWhat is displayedDisplay example
% ~ dp ○Path directoryC: \ Temp \
% ~ nx ○File name and extensiontest.txt
% ~ dpnx ○All file paths (same as% ~ f)C: \ Temp \ test.txt
% ~ pcnd ○All file paths (same as% ~ f)
* Even if the order is changed, the file path will be correct.
C: \ Temp \ test.txt

Please note that variable names in ○ can only be specified as numbers or one letter of the alphabet.
And most of the timeCan only be used as an argument to a pre-passed batch file or a variable in a For statement.. * It cannot be said that there is no way to force it ...→ Bonus

Even if you store the path in% VALUE% prepared by yourself and make it in the form of% ~ nVALUE%, it cannot be executed.

Afterword

This time, I introduced how to use "For statement" and "% ~" easily through the method of deleting the file extension and getting the file name with the Dir command.

At the actual site, when outputting a log file etc., it may be output as follows.

echo [log message] >> \\ [server name] \ [log storage location] \% COMPUTERNAME% .txt

With this command, it's like "put the log file with the computer name in the log save destination of the server".Then, you want to get the name of the computer that can output this log.

In such a case, if the file name can be obtained without leaving the extension, the computer name list that was executed as it is will be completed.

Of course, there are many ways to use it other than these cases.
The famous "% ~" is probably "% ~ 0". "% 0" stores the full path where the batch body is located when the batch is executed.Therefore, I think it is a common practice to get the location of the batch file with "% ~ dp0".

Well, there are many uses, so I hope you can make full use of your ideas.

Bonus-How to force an ordinary variable as an argument-

I didn't cover it in the article, but it's a way to convert a normal variable into an argument form.

If you do it easily, if you pass a variable as an argument to the subroutine, you can extract the character string using "% ~" even in the file path you prepared.

@echo off set Test_Path = C: \ Temp \ test.txt call: SUB % Test_Path%
pause exit: SUB echo% ~ n1 exit / b

": SUB" is a subroutine. "% Test_Path%" is passed as the first argument.
The argument passed to the subroutine is stored in "% 1", so by converting it to "% ~ n1", the file name "test" is output.

I hope it helps you.

Comment

Translate »
I copied the title and URL