

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