0

Hey i want to delete all files inside a folder except for few files, now these files i want to keep are normal folders & some of them are just blank files.

my files that i want to be kept

heres my current code, it keeps 1 folder only but i want to keep all 3 of the folders + my 2 blank files.

pushd "C:\Folder2" || exit /B 1
for /D %%D in ("*") do (
    if /I not "%%~nxD"=="Important Folder1" rd /S /Q "%%~D"
)
for %%F in ("*") do (
    del "%%~F"
)
popd

[Edit /] (from comment section)

I tried, like this:

pushd "%LOCALAPPDATA%\Google\ChromeVoter\User Data\Default\" || exit /B 1
for /D %%D in ("*") do (
    if /I not "%%~nxD"=="Important Folder1" rd /S /Q "%%~D"
    if /I not "%%~nxD"=="Important Folder2" rd /S /Q "%%~D"
    if /I not "%%~nxD"=="Important Folder3" rd /S /Q "%%~D"
)
for %%F in ("*") do (
    del "%%~F"
)
popd

It didn't work.

Compo
  • 36,585
  • 5
  • 27
  • 39
jasonmur
  • 19
  • 4
  • At what point did you not consider using `if /I not "%%~nxD"=="Important Folder2"` and `if /I not "%%~nxD"=="Important Folder3"`? And is there a particular reason why you have not used the same methodology for your files, in your second loop? – Compo Oct 02 '20 at 09:37
  • like this? https://pastebin.com/HPYD9DKs didnt work. @Compo – jasonmur Oct 02 '20 at 09:44
  • I have added that code, posted on a third party web site, to your question area, please do the same yourself in future. Also 'didn't work', is not an appropriate explanation, please provide more debugging information. – Compo Oct 02 '20 at 10:06
  • 1
    What happens if you do it like this: `If /I Not "%%~nxD" == "Important Folder1" If /I Not "%%~nxD" == "Important Folder2" If /I Not "%%~nxD" == "Important Folder3" RD /S /Q "%%~D"`? – Compo Oct 02 '20 at 10:17
  • This code looks [familiar](https://stackoverflow.com/a/39292406) to me. Anyway, your logic is wrong, a folder that does not match one (negative) condition still matches the others, so `Important Folder2` becomes removed by the line `if /I not "%%~nxD"=="Important Folder1" rd /S /Q "%%~D"`, for instance… – aschipfl Oct 02 '20 at 14:28
  • I recommend to use `findstr` as filter like: `for /F "eol=| delims=" %%I in ('dir "%LOCALAPPDATA%\Google\ChromeVoter\User Data\Default\*" /AD /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /V /X /C:"Important Folder1" /C:"Important Folder2" /C:"Important Folder3"') do rd /Q /S "%LOCALAPPDATA%\Google\ChromeVoter\User Data\Default\%%I"` Open a [command prompt](https://www.howtogeek.com/235101/), run `dir /?`, `findstr /?`, `for /?` and `rd /?` and read each output help carefully and completely. Let me know if you are interested in an answer with a full explanation for this command line. – Mofi Oct 02 '20 at 14:41
  • The second `for` loop can be replaced by the single command `del /A /F /Q "%LOCALAPPDATA%\Google\ChromeVoter\User Data\Default\*"`. Run in command prompt window `del /?` for help on this command. The option `/A` overrides the implicit default `/A-H` to delete also files with hidden attribute set which would be ignored on not using `/A` at all. – Mofi Oct 02 '20 at 14:45

0 Answers0