Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forfiles Batch Script (Escaping @ character)

Tags:

People also ask

How do I escape a character in a batch file?

In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ). That way, a single percent sign will be used as literal within the command line, instead of being further interpreted.

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.

What is ForFiles command?

forfiles is a computer software utility for Microsoft Windows, which selects files and runs a command on them. File selection criteria include name and last modified date. The command specifier supports some special syntax options. It can be used directly on the command-line, or in batch files or other scripts.

Can ForFiles delete folders?

ForFiles command lets you manage or delete folders, complying with some criteria on Windows 10.


I'm working on a batch script that will let me delete files older then a set period using forfiles. For now, I'm aiming at printing the files that will be deleted.

The forfiles invocation I'm using works flawlessly from a cmd.exe shell, but as soon as I embed it into a batch script, it barfs. I suspect that this is due to the @ character not being escaped properly, but I'm not certain.

The command I'm running is:

forfiles /S /P "r:\" /m *.bak /d -10 /c "cmd /c echo @PATH" 

And it results in the following error:

ERROR: Invalid argument/option - '@PATH' Type "FORFILES /?" for usage. 

I've googled all over the place and tried a few different schemes for escaping the @PATH component. Everything from @@PATH, to \"@PATH\" with no results.

Any help would be appreciated!

I should also note that I'm basing a lot of my knowledge of forfiles from here.