Answer the question
In order to leave comments, you need to log in
How to delete all files and subfolders except for a given file while keeping its path (subfolders)?
Searched. I didn't find what I needed. You need a batch file or a script.
Situation:
There is a folder, let's say "folder" in the folder there are still a bunch of folders with different names, and in each of these folders there is a folder called "special-folder" and in this folder there is a file "file.php"
Task:
Delete all files and folders that do not have a file and subfolder "/special-folder/file.php".
That is, the path, for example, is:
C:/folder/kakayato-podpapka/special-folder/file.php - save. The "kakayato-podpapka" folder should contain nothing but the "special-folder" folder, and the "special-folder" folder should contain nothing but the "file.php" file.
C:/folder/ another-podpapka /special-folder/file.php - save.
another-file.php - delete everything
C:/folder/kakayato-podpapka/ another-folder /file.php - delete everything
Answer the question
In order to leave comments, you need to log in
@echo off
set "pathdir=c:\script\test\del"
set "filename=special-folder\file.php"
for /f "tokens=* delims=" %%b in ('dir /a:-d /b /s "%pathdir%\*" 2^>nul ^| findstr /v /c:"%filename%" 2^>nul') do (
del /q /f "%%~b" 2>nul
)
for /f "tokens=* delims=" %%a in ('dir /a:d /b /s "%pathdir%" 2^>nul') do (
dir /a:-d /b /s "%%~a\*" 1>nul 2>&1 || rd /s /q "%%~a" 2>nul
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question