0

I am throwing together a batch file that will echo out a list of all of the directories in a specific path.

I have a few folders in the W:/wamp/www/ directory that start with an underscore _; for example _templates.

I would like the result to exclude any folders that start with an _. I don’t need this effort to be recursive.

So my current directory for w:/wamp/www/ looks like this:

  • _system
  • _templates
  • _assets
  • Folder1
  • Folder2
  • Folder3

Desired echo output is:

  • Folder1
  • Folder2
  • Folder3

I can get a listing with a number count using the following but of course it throws all of the folders back at me. I’d appreciate any assistance. I don’t really need the numbers in this list so if there is a more elegant approach to this I would be grateful for the insight

set acctDir=w:\wamp\www\
set app=setup.exe /cd
set log=w:\wamp\logs\projectlogs.txt


set c=0
For /f %%a in ('dir !acctDir! /B /A /D') do (
    set /a c+=1
    echo     !c!  %%a
    set dir!c!=%%a
    set projectname=%%a
)
2
  • Why not mark them as hidden? They don't look like the sort of names that will appear and disappear dynamically. Simply type attrib +h w:/wamp/www/_*, and the files will then be excluded from directory listings.
    – AFH
    Commented Apr 8, 2015 at 23:23
  • dir has no exclude option but this is relevant stackoverflow.com/questions/15714363/…
    – barlop
    Commented Apr 9, 2015 at 0:59

3 Answers 3

1

You can test for the underscore somewhere inside your FOR loop.

SET FIRSTCHAR=%%a
SET FIRSTCHAR=!FIRSTCHAR:~0,1!
IF NOT "!FIRSTCHAR!"=="_" (do some stuff)

This site has some good tips on string manipulation in CMD. http://www.dostips.com/DtTipsStringManipulation.php

1

If all you need is the listing, the full code could be

dir /a /b /d "w:\wamp\www" | findstr /v /b /c:"_"

Use findstr to filter the list, and retrieve only the lines that do not contain (/v) at the beginning of the line (/b) an underscore

0

If this system is at least Windows 7/Server 2008 R2 then you can run the following command interactively in Powershell:

Get-ChildItem -Exclude _* -Path w:\wamp\www\ | where{$_.mode -like "d*"} | select name

It really is advised to scrap batch all together and move to Powershell. It is the successor of cmd (which will be deprecated in the future) and is way more capable due to it's object orientation.

The command retrieves everything in the www directory that doesn't start with '_', then only passes directories down the pipeline to the select command that returns the directories' name attribute.

Let me know if you need to automate this or if this is sufficient.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .