0

What would I need to add, to also ignore file names containing AppData with .exe extensions? I am using this in an SetupComplete.cmd for unattended Windows setup, and have AppDataLocal.exe and AppDataRoaming.exe which are sfx archives and do not want these deleted. I am hoping this can be a wildcard, something like AppData*.exe.

forfiles /p "%WINDIR%\Setup\Files" /s /m *.* /c "cmd /c for %G in (@path) do @if /I [%~xG] neq [.reg] @echo del /F /Q %G" /d

1 Answer 1

1

I see no reason to use FORFILES - it is quite slow.

I would do something like the following:

@echo off
for /f "delims=" %%F in (
  'dir /b /s /a-d "%WINDIR%\Setup\Files"^|findstr /rive "\.reg [\\]AppData[^\\]*\.exe"'
) do del "%%F"

I pipe a recursive DIR /B through FINDSTR, using a pair of regular expressions to exclude file names that end with ".reg" or "\AppData*.exe", ignoring case. The result is iterated via FOR /F so I can delete each file.

10
  • Thankyou @dbenham. I tried running this script, but it deleted everything.
    – Schmitty
    Jul 18, 2015 at 10:34
  • @Schmitty - I can't imagine why it didn't work for you, as I tested and it worked perfectly for me. Are you sure you had .reg and AppData*.exe files to start with? Are you sure you copied the script properly? For testing you could put ECHO before DEL until you get the correct results.
    – dbenham
    Jul 18, 2015 at 11:48
  • Yes, I ran the script from %WINDIR%\Setup\Scripts. And had hkut.reg and AppDataRoaming.exe in %WINDIR%\Setup\Files\winrar, and it deleted everything, except the winrar directory.
    – Schmitty
    Jul 19, 2015 at 15:06
  • @Schmitty - I tested again with the exact setup as you described, and it worked perfectly. I added @echo off, and /a-d to suppress folder names, but that should not have any impact on the end result.
    – dbenham
    Jul 19, 2015 at 16:06
  • is the /a-d switch on dir or del command?
    – Schmitty
    Jul 22, 2015 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .