Amarundo
Amarundo

Reputation: 2397

What that %%~ do in a Windows 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?

Upvotes: 3

Views: 1276

Answers (1)

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.

Upvotes: 2

Related Questions