CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Help with DOS batch script - nested script calling

    hi all, it was suggested that I post this here:

    I'm trying to have a main batch file recurse through its subdirectories and call any batch files it encounters there. My problem is limiting the scope each batch to its own subdirectory. It's not really DOS but the Windows command prompt.

    say you have these files
    c:\temp\batch.bat

    c:\temp\folder1\batch_deletes_txt.bat
    c:\temp\folder1\file1.txt
    c:\temp\folder1\file2.doc

    c:\temp\folder2\batch_deletes_doc.bat
    c:\temp\folder2\file3.txt
    c:\temp\folder2\file4.doc

    So, batch.bat should call batch_deletes_txt.bat which deletes file1.txt only (not file3.txt), and batch.bat should also call batch_deletes_doc.bat which deletes file4.doc only (not file2.doc).

    My contents:

    batch.bat:
    for /r %%X in (*.bat) do if NOT %%X == %0 %%X
    REM ^ the "if not" is to prevent infinite loops.

    batch_deletes_txt.bat:
    for /r %%X in (*.txt) do del "%%X"

    batch_deletes_doc.bat:
    for /r %%X in (*.doc) do del "%%X"

    This doesn't work because the scope of each script is c:\temp, ie. not local. How do I fix this?

    (My real goal is not deleting files, this is just an example)

  2. #2
    Join Date
    Aug 2005
    Location
    Milwaukee, WI
    Posts
    55

    Re: Help with DOS batch script - nested script calling

    I'm no batch script expert, but I *do* know that cd is the command used to change directories. Have you tried using that?

    Have you considered perl? You could even write it inline on the command line if you'd like: perl -e "perl commands;"
    Meddle not in the affairs of dragons, human, for thou art crunchy and taste good with ketchup.

  3. #3
    Join Date
    Jan 2009
    Posts
    3

    Re: Help with DOS batch script - nested script calling

    I did think about using cd, but the question is how do you ask the first batch to cd into folders dynamically?

    I'm only vaguely familiar with perl. An ordinary batch file would be preferred in this case...
    Last edited by anon0; February 2nd, 2009 at 09:11 PM.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help with DOS batch script - nested script calling

    Use Powershell 2.0 CTP. It's a replacement for the DOS command language with its commandlets. You can use For-Each to loop thru folders dynamically
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jan 2009
    Posts
    3

    Re: Help with DOS batch script - nested script calling

    Quote Originally Posted by dglienna View Post
    Use Powershell 2.0 CTP. It's a replacement for the DOS command language with its commandlets. You can use For-Each to loop thru folders dynamically
    thanks, I've never used it. Could someone explain how to do this in Powershell?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured