I am trying to write a batch function where i need exclude some folders while searching in directory
Main Folder
--ProjA Folder
--A Folder
--A.csproj
--Aa Folder
-Aa.csproj
-- B Folder
--B.csproj
-- C Folder
--c.csproj
When i tried with this function,its giving me project file names in all nested subfolders.
Set MainDir="C:usersMain"
cd %MainDir%
for /f "delims=" %%x in ('dir /s /b *.csproj') do if exist %%x (
echo %%~dpx
)
But I need to exclude B folder related B.csproj files. Please let me know how can we apply filter for this particular subfolder
3
[Theoretical only]
Set "MainDir=C:usersMain"
cd %MainDir%
for /f "delims=" %%e in ('dir /s /b *.csproj^|find /i /v "ProjA FolderB Folder"') do if exist "%%e" (
echo %%~dpe
)
Notes:
Use set "var=value"
for setting string values – this avoids problems caused by trailing spaces. Don’t assign "
or a terminal backslash or Space. Build pathnames from the elements – counterintuitively, it is likely to make the process easier. If the syntax set var="value"
is used, then the quotes become part of the value assigned.
Changed metavariable
from %%x
to %%e
since x
is also a metavariable modifier
(see for /?
from the prompt) which can lead to hard-to-spot problems.
Quoted metavariable in if exist
so that filenames including spaces should be handled correctly.
Use ^|
(escaped pipe) to filter output of dir
using find
to report filenames which do not (/v
) contain the case-insensitive (/i
) string "ProjA FolderB Folder"
.
Your code should report the drive and path for each file encountered that matches the filemask *.csproj