Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What that %%~ do in a Windows batch file?

Tags:

batch-file

In a Windows batch file that I inherited and have to edit, there's this line (and I'm simplifying for readability):

FOR %%m in (*.XML) DO IF EXIST D:\DATA\%%~m COPY D:\DATA\%%~m subdir

Which copies XML files in D:\Data to the subdir subdirectory of the current folder.

My question is what does %%~m to that %%m wouldn't do?

like image 806
Amarundo Avatar asked Oct 24 '25 15:10

Amarundo


1 Answers

The question has been answered in the comments from @Stephan, @rojo, @Magoo.

Summarised here so the question is marked as answered:

The ~ character in %%~m removes the surrounding quotes from the variable m.

If it is a file name that contains spaces you can put the quotes back around the whole path name, for example:

if exist "D:\Data\%%~m" copy "D:\Data\%%~m" subdir

You can learn about these substitutions from for /?. The variable substitution can contain other operations, for example %%~nm and %%~tm to exatract the file name or datestamp.

like image 196
Brian Tompsett - 汤莱恩 Avatar answered Oct 26 '25 05:10

Brian Tompsett - 汤莱恩



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!