batch file

Discussion in 'OT Technology' started by D1G1T4L, May 17, 2007.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    how would i write a batch file to go through subdirectories in a given directory and pick the first one without a file called "blah" in it, and create the file "blah" in that subdirectory
     
    Last edited: May 17, 2007
  2. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    for /r "C:\Given Directory" %i in (blah) do if not exist "%i" echo Contents of the file> "%i"

    Need %%i if you run it in batch
     
  3. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    You are the batch file king, 5Gen. It's kinda scary, actually.
     
  4. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Ain't it? What can I say, I got good at a handful of antiquated things and refuse to learn new things :rofl:
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    sounds like 90% of the IT workforce out there :rofl:
     
  6. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Lol - well I'm not that bad, but when a script is required, I do try and do it in batch first just because a) familiarity b) I don't have to worry about incompatibilities or script errors. If it just won't work (like dipping into a database is required), I switch to something else.
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i think you had ther ight idea except your solution would put the file into every empty dir and i am not sure if the syntax is completly correct?
     
  8. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Oh... sorry thought that's what you wanted. Just need to track whether it has been done then:

    Code:
    set found=0
    for /r "C:\Given Directory" %%i in (blah) do (
         if !found!==0 (
              if not exist "%%i" echo Contents of the file> "%%i"
              set found=1
         )
    )
    set found=
    
    You'll need to either turn on the delayed environment expansion or simply run this from another batch file with this as its only content:

    cmd /v:on /c "C:\Insert 1st Batchfile name here.bat"

    It's not horribly efficient because it enumerates every directory regardless on whether it's done.
     

Share This Page