locked
IF Not Exist wildcard for directories> RRS feed

  • Question

  • So this works if the wildcard is a file:

     

    if not exist "path\to\file\*.*" goto next

     

    But it doesn't work for directories, does anyone know an easy way to accomplish that using a wildcard fir directories, ie if no directories exist in /path/to/file goto next

    Friday, July 15, 2011 8:31 PM

Answers

  • Another approach:

    dir /b /ad "path\to\file" | findstr /r "^.*$" > nul || goto next

    If there are no folders under path\to\file it will not produce any output, findstr will fail to find any match and goto will be used, otherwise - line will be skipped. :)

    • Proposed as answer by jrv Friday, July 15, 2011 9:23 PM
    • Marked as answer by the200mg Friday, July 15, 2011 9:40 PM
    Friday, July 15, 2011 8:56 PM

All replies

  • Here is one of several methods:

    @echo off
    set path=d:\Test Folder
    set Folder=
    for /d %%a in ("%path%\*.*") do set Folder=%%a
    if "%Folder%"=="" echo No folders found in %path%

    • Proposed as answer by Bigteddy Friday, July 15, 2011 8:59 PM
    Friday, July 15, 2011 8:43 PM
  • You can try Test-Path to see if a folder directory exists.  It returns a boolean value.  Try the following example:

    if (Test-Path path\to\file\)
    {
      #Directory exists<br/>}
    else
    {
      #go to next
    }
    
    


    I don't think there is a way to insert a wildcard in place of a path.

     

    • Proposed as answer by mzachen Friday, July 15, 2011 9:11 PM
    Friday, July 15, 2011 8:48 PM
  • Another approach:

    dir /b /ad "path\to\file" | findstr /r "^.*$" > nul || goto next

    If there are no folders under path\to\file it will not produce any output, findstr will fail to find any match and goto will be used, otherwise - line will be skipped. :)

    • Proposed as answer by jrv Friday, July 15, 2011 9:23 PM
    • Marked as answer by the200mg Friday, July 15, 2011 9:40 PM
    Friday, July 15, 2011 8:56 PM
  • thank you very much for the replies, i am going to go with Bartek's solution, thanks again
    Friday, July 15, 2011 9:06 PM