TechUser360
TechUser360

Reputation: 27

How do I exclude something in a .txt from showing up with For?

I made the following script for displaying all the local users that don't have a nominative folder in the destination.

I'd like to create an exlusion text file for the users that I don't want to display.

I'm note really sure how I can achieve this.

Here is my code:

@echo off

set BackupDest=D:\backup

echo Destination folder missing for the following user(s):
for /D %%I in ("%HomeDrive%\users\*") do if not exist "%BackupDest%\%%~nI\" (
echo %%~nI
)

Upvotes: 1

Views: 48

Answers (1)

michael_heath
michael_heath

Reputation: 5372

@echo off
setlocal
set "BackupDest=D:\backup"

if not exist "%~dp0exclude_user.txt" > "%~dp0exclude_user.txt" echo Public

echo Destination folder missing for the following user(s) :

for /f "tokens=*" %%I in (
    'dir /a:d-h /b "%HomeDrive%\users\*" ^| findstr /b /e /i /l /v /g:"%~dp0exclude_user.txt"'
) do if not exist "%BackupDest%\%%~nxI\" (
    echo %%~nxI
)

The user named folders to exclude can be added to exclude_user.txt. The file is found in the script directory e.g. %~dp0. If file does not exist, it will be created with a line containing Public, which perhaps you do not want to backup.

The arguments of findstr are currently set for a literal exact match that is case-insensitive.

dir will find folders that are not hidden so that special named folders such as Default are not output.

Upvotes: 1

Related Questions