Batch : Search Directory in every Directory and Subdirectory - windows

I want to delete all directories with a specific name. E.g. "vendor"
D:\Data\Project1\Dir\vendor
D:\Data\Project2\AnotherDir\AnotherDir\vendor
D:\Data\Project3\vendor
This is what I have at this moment. This lists all the folders, and subfolders.
for /d %%a in (*) do dir /ad /on /s /b %%a
Now I wish to put one path in a variable, and check whether the directory is equal to a name (for example "vendor"). But I can't find how.
Can you help me?
Regards,
Demian

This is my working solution if you have RimRaf installed:
#ECHO off
setlocal enableDelayedExpansion
:: Folders where the batch program doesn't need to search.
set skipTheseFolders=Decleir Pinokkio
:: All the folders that need to be deleted
set foldersToDelete=vendor node_modules
for /d /r %%d in (*.*) do (
#ECHO %%d
set folder=%%~nxd
set canI="true"
:: For loop to skip folders
for %%s in (%skipTheseFolders%) do (
:: Need to be implemented
set canI="true"
)
IF !canI! =="true" (
for %%l in (%foldersToDelete%) do (
:: If the folder is one of the folders To Delete
IF "!folder!" == "%%l" (
#echo %%d
cd %%d
cd ..
:: Using Rimraf because when you delete node_modules, normal delete won't work
start /B rimraf !folder!
)
)
)
)

The DIR command has an unfortunate limitation that it cannot recursively list specific folder names. The simplest solution is to list all folders, and use FINDSTR to filter out all but the folders that match.
dir /ad /on /s /b | findstr /iec:"\vendor"
Then you simply iterate the results using FOR /F and add your RD command
for /f "eol=: delims=" %%F in
'dir /ad /on /s /b ^| findstr /iec:"\vendor"'
) do rd /s /q "%%F" 2>nul

You can try this :
#Echo OFF
set "Folder=D:\Data"
set "FolderString=vendor"
Setlocal Enabledelayedexpansion
FOR /f "tokens=*" %%F IN ('dir /b /s /ad %Folder%\ ^| find "%FolderString%"') DO (set var="%%F"
ECHO rd /s /q !var! && rd /s /q !var!)
EndLocal
pause

Related

Delete all files in a folder but skip files that cointain certain string

I'm trying to clean the temp files, but I want skip some files that cointain a certain name. No matter the extension it is.
Tried this way:
#echo off
setlocal EnableDelayedExpansion
for /f "eol=: delims=" %F in ('dir "%windir%/temp" /b /a-d * ') do find "TESTE" %F > nul || del "%F"
pause
Wanted all files that cointains TESTE in name got skipped from deletation.
But my script not even run.
Can someone explain me what is wrong?
It is not absolutely clear whether you want to exclude files from deletion whose names or whose contents include a certain string; anyway, this is for the former:
pushd "%WinDir%\TEMP" && (
for /F "delims= eol=|" %%F in ('
dir /B /A:-D "*.*" ^| findstr /V /I /C:"TESTE"
') do (
ECHO del /F "%%F"
)
popd
)
Once you are satisfied with the output, remove the upper-case ECHO command.
The above script would also not delete files whose extensions contain the given string. But if you want to regard only the base name, you might want to use this code instead:
pushd "%WinDir%\TEMP" && (
for /F "delims= eol=|" %%F in ('
dir /B /A:-D "*.*"
') do (
set "NAME=%%~nF"
setlocal EnableDelayedExpansion
if /I "!NAME:TESTE=!"=="!NAME!" (
endlocal
ECHO del /F "%%F"
) else endlocal
)
popd
)
You should be able to use findstr.exe with its /V and /M options to list your unwanted files.
#SetLocal EnableExtensions
#For /F "Delims=" %%G In (
'%__AppDir__%findstr.exe /VPMLI "TESTE" "%SystemRoot%\TEMP\*" 2^>NUL'
)Do #Del /A/F "%%G"
#Pause
Please note that the \Windows\Temp directory is usually protected, so you may need to run this script 'as administrator'.

How can I make my BATCH file affect subfolders?

I have a BATCH file which deletes every other JPG file. I am looking to make it so that when it is run, it affect all subfolders as well.
I am not a coder by all means, so apologies if this is a rookie question!
Below is my current code:
#echooff
setlocal
for /r %%D in (.) do (
set "del="
for /f %%F in ('dir /b "%%D\*.jpg"') do if defined del (
del "%%D\%%F"
set "del="
) else set "del=1"
)
PAUSE
The /S switch in the dir makes you go through subdirectories, as you can see here:
dir /?
...
/S Displays files in specified directory and all subdirectories.
...
So I'd advise you to replace:
dir /b "%%D\*.jpg"
by:
dir /S /b "%%D\*.jpg"
Good luck
Based upon your clarifications, is this what you're trying to achieve:
#SetLocal EnableExtensions DisableDelayedExpansion
#For /F "EOL=?Delims=" %%G In ('Dir /B/S/A:D 2^>NUL') Do #(Set "_="
For /F "EOL=?Delims=" %%H In ('Dir /B/A:-D/O:N "%%G\*.jpg" 2^>NUL')Do #(
If Not Defined _ (Set "_=T")Else Del /A/F "%%G\%%H"&Set "_="))

.bat file for selectively deleting IE cache for three files

thanks for helping out
I currently face some issues while working on a .bat file that deletes the Internet Explorer 11 cache specifically for this three files:
Analytics.swf
Deal.swf
Pricing.swf
Currently I use the code attached, but it deletes all files in the cache (OS = Windows 10):
#echo off
set DataDir=C:\Users\%USERNAME%\AppData\Local\Microsoft\Intern~1\
del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"
set History=C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\History
del /q /s /f "%History%"
rd /s /q "%History%"
set IETemp=C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Tempor~1
del /q /s /f "%IETemp%"
rd /s /q "%IETemp%"
set Cookies=C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Cookies
del /q /s /f "%Cookies%"
rd /s /q "%Cookies%"
C:\bin\regdelete.exe HKEY_CURRENT_USER "Software\Microsoft\Internet Explorer\TypedURLs"
The following batch script will loop through the 4 folders you provide and search for all three files (see how to loop through arrays in batch) in all subdirectories (dir /b /s /a:-d "%%~f").
If the script is fine for you remove the echo in front of the del command:
#echo off
set "filesDel[0]=Analytics.swf"
set "filesDel[1]=Deal.swf"
set "filesDel[2]=Pricing.swf"
set "dirDel[0]=%LocalAppData%\Microsoft\Intern~1"
set "dirDel[1]=%LocalAppData%\Microsoft\Windows\History"
set "dirDel[2]=%LocalAppData%\Microsoft\Windows\Tempor~1"
set "dirDel[3]=%AppData%\Microsoft\Windows\Cookies"
rem loop over all directories defined in dirDel array
for /f "tokens=2 delims==" %%d in ('set dirDel[') do (
if exist "%%~d" (
pushd "%%~d"
rem loop over all files defined in filedDel array
for /f "tokens=2 delims==" %%f in ('set filesDel[') do (
rem search for file and delete it
for /f "tokens=*" %%g in ('dir /b /s /a:-d "%%~f"') do (
echo del /f "%%~g"
)
)
popd
)
)
Note that I use the quotes in the set command like: set "name=content", this allows spaces in the path name without having the quotes in the variable content itself.
Command reference links from ss64.com:
set
for /f
dir
if
pushd
popd
del
rem

Batch script to delete all folders except of "Starts with"

I have a folder with several subfolders. Structure is like:
C:\foo
C:\foo\web.foo
C:\foo\web.bar
C:\foo\win.foo
C:\foo\win.bar
C:\foo\mobile.foo
C:\foo\mobile.bar
I sometimes wish to delete the folders with its containing files with following batch script:
rmdir C:\foo /s /q
Here it didn't matter that the whole folder C:\foo was deleted completely.
But now I only want to delete only the subfolders of C:\foo with its containing files, which DO NOT start with "web.".
Do you have any good solution for this?
The following should do the trick, note it's a batch file using the current directory:
#echo off
for /F "delims=" %%D in ('dir /B /AD ^| findstr /V "^web."') do (
echo rmdir %%D /s /q
)
If it's okay remove the echo in front of rmdir.
The dir command just list directory names because of /AD and use a simple name output because of /B. To search on the beginning use findstr with /V. For negation use ^. Further the pipe symbol needs to be escaped ^|.
If you want a dynamic batch script that uses arguments you can use the following, call it via batchname.bat "C:\foo" web. (if it's okay remove the echo in front of rmdir.):
#echo off
set INVARGS=0
if [%1] == [] set "INVARGS=1"
if [%2] == [] set "INVARGS=1"
if %INVARGS% == 1 (
echo echo %0 ^<directory^> ^<directory_prefix^>
goto eof
)
set "folder=%1%"
set "prefix=%2%"
pushd "%folder%"
echo List of folders that should be deleted:
for /F "delims=" %%D in ('dir /B /AD ^| findstr /v "^%prefix%"') do (
echo "%cd%\%%D"
)
popd
:choice
echo.
set /P "c=Are you sure you want to continue [Y/N]?"
if /I "%c%" EQU "Y" goto yes
if /I "%c%" EQU "N" goto eof
goto :choice
:yes
echo.
pushd "%folder%"
for /F "delims=" %%D in ('dir /B /AD ^| findstr /v "^%prefix%"') do (
echo rmdir %%D /s /q
)
popd
:eof
This will remove all files begin with web.
#echo off
setlocal EnableDelayedExpansion
for /f %%F in ('dir /b /s') do (
set "name=%%~nF"
if /i not "!name:~0,4!" == "web." (
rm !name!
)
)

Extract folder name from path in batch file

I want to traverse all files within a specific directory and all its subdirectories and then print out the folder name of each file.
I don't know how to get the folder name of each file.
FOR /F "delims=" %%x IN ('dir /B /A /S *') DO (
:: Suppose %%x is 'C:\myfolder\a.txt', the desired output is 'myfolder'
:: %%~nx is not correct
echo ???
)
If you want just the path (without drive, without filename), %%~px is what you need
If you want just the last folder, not the complete path. This is indeed not that trivial:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%x in ('dir /b /a /s *') do (
set "line=%%~dpx"
for /f "delims=" %%y in ("!line:\=.!") do set folder=%%~xy
echo %%~nxx is in: !folder:~1!
)
I think this is what you're looking for:
#echo off
FOR /F "delims=" %%F IN ('dir /B /A /S *') DO (
for %%D in ("%%~dpF\.") do echo %%~nxD
)
pause

Resources