I
I
Ivan Ilyasov2017-09-07 01:56:19
css
Ivan Ilyasov, 2017-09-07 01:56:19

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

2 answer(s)
B
bbquite, 2019-05-19
@WebDev921

Disable word wrapping with white-space:nowrap
AND overflow:hidden

R
res2001, 2017-09-07
@IvanIlyasov

@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
)

Save the code in a .bat file, initialize the variables correctly: pathdir and filename.
And practice on the "cats".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question