batch file - bat: Listing empty directories, if/else statements within for statements? -
i'm trying write script lists every file name in specified folder, , notifies user if folder empty. far i've got:
for /r "o:\mail\5-friday" %%d in (*.pdf) ( dir /a /b "%%~fd" 2>nul | findstr "^" >nul && echo %%~nd || echo empty: friday ) but i've no idea put if, else operators.
and there way specify folder based on user input without rewriting every function each folder? instead of:
if /i {%ans%}=={thursday} (goto :thursday) if /i {%ans%}=={friday} (goto :friday) :thursday <do stuff> :friday <do same stuff thursday, different directory> etc, write 1 function variables in place of paths, assign directory variable, , add/remove folders in code necessary?
to address first part of question, "where put if, else operators"... notation of
command | findstr >nul && echo success || echo fail ... shorthand for
command | findstr >nul if errorlevel 1 ( echo fail ) else ( echo success ) the magic happens in conditional execution operators, && , ||. if findstr exits status zero, match found. therefore, execute stuff after &&. otherwise, status non-zero, no match found, execute stuff after ||. see how works?
for second part, here's typical way prompt user provide entry based on finite number of choices.
@echo off setlocal :begin set /p "day=what day? " %%i in (monday tuesday wednesday thursday friday) ( if /i "%day%" equ "%%i" goto %%i ) goto begin :monday call :getdirs "o:\mail\1-monday" goto :eof :tuesday call :getdirs "o:\mail\2-tuesday" goto :eof :wednesday call :getdirs "o:\mail\3-wednesday" goto :eof :thursday call :getdirs "o:\mail\4-thursday" goto :eof :friday call :getdirs "o:\mail\5-friday" goto :eof :getdirs <path> setlocal enabledelayedexpansion /f "delims=" %%i in ('dir /b /s /ad "%~1"') ( dir /b "%%i" 2>nul | findstr "^" >nul || echo %%i has no files ) goto :eof or, hacksier, i'll weren't expecting possible. i'll have script open folder selection dialog allow user select directory scan. it's batch / jscript hybrid script.
if wish, can set root of folder browser shellspecialconstants folder changing last argument in next-to-the-last line. using value of 0x11 makes root system's drives. no value or value of 0x00 makes root "desktop". or leave script as-is set root "o:\mail".
@if (@a==@b) @end /* :: fchooser2.bat :: batch portion @echo off setlocal set initialdir="o:\mail" /f "delims=" %%i in ('cscript /nologo /e:jscript "%~f0" "%initialdir%"') ( call :getdirs "%%i" ) exit /b :getdirs <path> setlocal enabledelayedexpansion /f "delims=" %%i in ('dir /b /s /ad "%~1"') ( dir /b "%%i" 2>nul | findstr "^" >nul || ( rem put code handle empty directories. echo %%i has no files ) ) goto :eof :: jscript portion */ var shl = new activexobject("shell.application"); var hint = 'double-click folder expand, and\nchoose folder scan empty directories'; var folder = shl.browseforfolder(0, hint, 0, wsh.arguments(0)); wsh.echo(folder ? folder.self.path : ''); edit since apparently browseforfolder accepts absolute directory, there's no benefit using powershell / c#. hybrid batch / powershell / c# script shall henceforth retired revision history.
this fun!
Comments
Post a Comment