'Delete long name and long path file using cmd del

I would like to create a simple script to delete tmp files. However, not all files are deleted, those with the long name show an error. There are two errors:

  1. The filename or extension is too long
  2. When deleting, the path splits into two and an error appears Could Not Find ...

I use simple commands:

forfiles -p "patch" -s -m *.tmp -d -2 /c "cmd /c for %A in (@file) do @echo %~sA" >> patch\files_to_delete.txt

and to delete files:

for /f %i in (files_to_delete.txt) do del /f /a %i

sample path and filename:

X:\XXXX\XXXXXXXXX\XXXX\XXXXXXXXX\XX\XXXXX\XXXXXXX\XXXXXXX\XXXXX\XXXXXXXX\Microsoft.XXXXXXXXXXXXX_XXXXXXXXXXXX\AC#!001\MicrosoftXXXX\Cache\JQQOUHRJ\XXXXXXXXX[00007l4w2XZUfV000002cG]pb=!1m5!1m4!1i8!2i290!3i198!XXX28!2m2!1e1!3i845!3m9!2sen!3sukXXXX105!12m1!1e4!12XXXXX7!12m1!1e3!4e0!XXXXXX!5f2!23i1358902[1].XXXXXXXXX.tmp

When deleting this file an error is shown:

Could Not Find X:\XXXX\XXXXXXXXX\XXXX\XXXXXXXXX\XX\XXXXX\XXXXXXX\XXXXXXX\XXXXX\XXXXXXXX\Microsoft.XXXXXXXXXXXXX_XXXXXXXXXXXX\AC#!001\MicrosoftXXXX\Cache\JQQOUHRJ\XXXXXXXXX[00007l4w2XZUfV000002cG]pb

Could Not Find C:\WINDOWS\system32!1m5!1m4!1i8!2i290!3i198!XXX28!2m2!1e1!3i845!3m9!2sen!3sukXXXX105!12m1!1e4!12m1!1e47!12m1!1e3!4e0!XXXXXX!5f2!23i1358902[1].XXXXXXXXX.tmp

when i use apostrophes i get a message The filename or extension is too long.

any idea?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source