Forfiles Batch Script (Escaping @ character)

后端 未结 8 955
闹比i
闹比i 2020-12-15 05:35

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.

8条回答
  •  鱼传尺愫
    2020-12-15 06:29

    This an old question but I've got a different answer... in case anyone needs it.

    When using 'forfiles', the path (written after /p) CAN be between quotation marks. However, it must not end with a slash.

    If you want to run 'forfiles' for the root directory of a drive:
    forfiles /p "C:" /c "cmd /c echo @file"

    If you want to process files in a different directory...
    forfiles /p "C:\Program Files" /c "cmd /c echo @file"

    In other words, the safest approach is:

    • Always use quotation marks (because folders with spaces, like 'Program Files', will still work)
    • Always omit the last trailing slash

    forfiles /p "C:\Path\Without\Trailing\Slash"

提交回复
热议问题