Search code examples
batch-fileescaping

Ignore percent sign in batch file


I have a batch file which moves files from one folder to another. The batch file is generated by another process.

Some of the files I need to move have the string "%20" in them:

move /y "\\myserver\myfolder\file%20name.txt" "\\myserver\otherfolder"

This fails as it tries to find a file with the name:

\\myserver\myfolder\file0name.txt

Is there any way to ignore %? I'm not able to alter the file generated to escape this, such as by doubling percent signs (%%), escaping with / or ^ (caret), etc.


Solution

  • You should be able to use a caret (^) to escape a percent sign.

    Editor's note: The link is dead now; either way: It is % itself that escapes %, but only in batch files, not at the command prompt; ^ never escapes %, but at the command prompt it can be used indirectly to prevent variable expansion, in unquoted strings only.

    The reason %2 is disappearing is that the batch file is substituting the second argument passed in, and your seem to not have a second argument. One way to work around that would be to actually try foo.bat ^%1 ^%2... so that when a %2 is encountered in a command, it is actually substituted with a literal %2.