Steven
Steven

Reputation: 61

How to loop through a directory and skip folders containing a specific string?

I am currently scanning an entire directory, copying an exe over and running it in each folder. This exe converts certain log files within each folder. However, half the directories don't contain any log files and it seems like a waste to move into each of those folders and run an exe that does nothing. An example of some folder names:

I basically only need to copy the executable into the folders that don't contain EVT in the name. These just contain evtx files that aren't important to me at the moment.

Here is a snippet of my batch file and what it is doing:

set back=%cd%
set current_dir=%cd%
setlocal EnableDelayedExpansion
for /d %%i in ("%current_dir%\*") do (
   copy LogCsv.exe "%%i" >NULL
   cd "%%i"
   LogCsv.exe
   )
cd %back%

Really my first time just using Batch files and I've been exploring answers on here, but couldn't find a solution to this particular problem. How can I loop this directory, excluding any folders containing the "EVT" string in their name?

Thanks for taking time to assist!

Upvotes: 2

Views: 1153

Answers (1)

user7818749
user7818749

Reputation:

Well, you can use findstr /v to exclude certain values from search results.

@echo off
set back=%cd%
set current_dir=%cd%
for /f %%i in ('dir /b /ad %current_dir%\* ^| findstr /v EVT') do (
     copy LogCsv.exe "%%i" >NUL
     cd "%%i"
     LogCsv.exe
     cd %back%
)

Also note, you need to pipe to nul not null

Upvotes: 2

Related Questions