|
Answer» I use following code to list all the folders and subfolders from a given folder: dir /s /b /o:N /a:d > folderlist.txt , but I need only those, which contains at least one file.So you mean, EXCLUDE all folders with a zero number of files? Is a folder with a subfolder, but no files, empty or not?
Yes. If has a subfolder, but no files it is empty, but subfolder should be CHECKED too.sounds LIKE a job for powershell. something like this may get you started; powershell -command "Get-ChildItem 'C:\mydocs' -Recurse -Directory | ? {[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -gt 0} | ? {$_.FullName}" it will not show folders with a file count of zero. Output seems correct! Which flag should I use please to hide folder details?if you prefer to stick with your batch solution, try this:
Code: [Select]echo off for /f %%i in ('dir <YourFolderName> /ad /s /b') do ( for /f %%j in ('dir %%i /w ^| find /i "file(s)"') do ( if %%j GTR 0 echo %%i >> <YourFileName> ) )
If you prefer a Powershell solution, this may come in HANDY:
Code: [Select]Get-ChildItem <YourFolderName> -Recurse -Directory | Where-Object { $_.GetFiles().Count -gt 0 } | Select-Object Fullname -ExpandProperty Fullname | Out-File -FilePath <YourFileName> Be sure to change appropriately.
Good luck. Thanks for your help!
|