Microsoft Batch - Delete all file inside folder except one or two

Apr 12, 2017 by M. Zemmy Indrapatih

As of this time of writings, i'm stuck to create batch script to remove temporary file from laravel folder. Then after searching i found the working script. Please use if you need it.

This script will delete all file inside subdirectory "storage\framework\sessions\" except ".gitignore" file.

for %%i in (storage\framework\sessions\*) do if not "%%~nxi" == ".gitignore" del /q "%%i"

And this is for delete all file inside subdirectory except more than one file

for %%i in (*.*) do if not "%%i"=="doc1.txt" if not "%%i"=="doc2.txt" del /q "%%i"