1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: A .bat that reads python files?

Discussion in 'Software Development' started by Helloher, Apr 1, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    Does anyone know if a batch file could be created to read python files? What I would like to do is have it read a group of python files in a folder and create a .txt with a piece of information from each one, that is found in the same place on each one. if this is posibble, it would be appreciated.
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Examples of what is in the python code and what your intended output is would probably help.
     
  3. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    The files vary in complexity, but the barebones file resembles this:

    from __future__ import generators
    import plus
    import Arenas
    import random
    import Hazards
    import math
    class Box(Arenas.SuperArena):
    "Twiiiiiiiiiiiiiiiisted"
    name = "Twiiiiiiiiiiiiiisted Arena"
    preview = "twisted/twi_preview.bmp"
    game_types = ['DEATHMATCH', 'BATTLE ROYAL', 'TEAM MATCH']
    extent = (-15, 15, 15, -15)
    def __init__(self):
    Arenas.SuperArena.__init__(self, "Arenas/twisted/twisted.gmf")
    #plus.Arena.__init__(self, "")
    plus.setBackColor(.36, .537, .788)

    degrad = 0.01745
    self.AddStaticCamera("Battle View", (19.5, 15, 19.5), (50*degrad,225*degrad), 40*degrad)
    self.AddStaticCamera("High Flipper View", (-19.5, 45, -19.5), (48*degrad,45*degrad), 84*degrad)
    self.AddStaticCamera("Birds Eye View", (0, 45, 0), (90*degrad,0), 50*degrad)
    self.AddWatchCamera("Combat Cam", (-12, 8, 12), (16, 36, 65*degrad, 30*degrad))
    self.AddWatchCamera("Aerial Cam", (-19.5, 35, -19.5), (50, 60, 45*degrad, 60*degrad))
    self.AddWatchCamera("Ground Cam", (8, -5, -8), (15, 40, 75*degrad, 35*degrad))
    self.AddWatchCamera("Spectator Cam", (13, 15, 13), (6, 18, 45*degrad, 45*degrad))

    self.players = ()
    def AddShadowReceivers(self):
    self.SetShadowSource(5.897, 19.159, 5.899)

    def Activate(self, on):
    if on: self.players = plus.getPlayers()

    Arenas.SuperArena.Activate(self, on)
    def Tick(self):
    for each in self.players:
    if plus.getLocation(each)[1] < -10: # Adjust this parameter to floor height(s).
    plus.eliminatePlayer(each)
    return Arenas.SuperArena.Tick(self)

    Arenas.register(Box)

    All I want to get is the names from each file printed in a .txt, preferably in alphabetical order, although this isn't required.
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I don't know what you mean by names so you are going to have to be very specific as to what your output would be from this input.
     
  5. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    Where it says name = "Twiiiiiiiiiisted Arena"

    I want the name printed in the .txt and nothing else.
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I am kind of wondering why you can't write this in python if you know how to code python. This is easy enough to do in a batch file though. Will write it up when I am on my lunch break.
     
  7. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    I didn't code this. I simply customised something else. Thanks btw.
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I think this code should work barring any anomalies with your other files.
    Code:
    @echo off
    setlocal enabledelayedexpansion
    
    :: Output file
    Set _oFile=Names.txt
    
    :: Directory with python files
    Set _Path=E:\batch files\findstr
    
    :: Used to search and output your files
    PushD "%_Path%"
    FOR /F "tokens=*" %%I IN ('dir /b /a-d *.py') DO (
    	For /F "usebackq tokens=1* delims== " %%a In ("%%I") Do (
    		echo.%%a|findstr /B /I /C:"name">nul
    		If !errorlevel!==0 echo.%%~b>>%_oFile% 
    	)
    )
    Popd
     
  9. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    Ok, thanks, I'll give it a try.
     
  10. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    When I open it, it says "The device is not ready".
     
  11. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    I found my rather idiotic error. Thanks.
     
  12. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    See those two lines of code. The first one is a comment to tell you to set your path to the files.

    :: Directory with python files
    Set _Path=E:\batch files\findstr
     
  13. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    Yeah, I noticed right after I posted. Thanks so much btw.
     
  14. Helloher

    Helloher Thread Starter

    Joined:
    Mar 23, 2010
    Messages:
    18
    Hello again. Is there any way to not list specific things? I.E., if 'Practice Arena', or 'Event Results' are found, the file would automatically not list them. However, there is a name called 'The Practice Arena' which I would like to keep. I don't know if this is possible. Thanks.
     
  15. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Code:
    @echo off
    setlocal enabledelayedexpansion
    
    :: Output file
    Set _oFile=Names.txt
    
    :: Directory with python files
    Set _Path=C:\Users\MaryJo\batch\FindArenaNames
    
    :: Used to search and output your files
    PushD "%_Path%"
    FOR /F "tokens=*" %%I IN ('dir /b /a-d *.py') DO (
    	For /F "tokens=1* delims== " %%a In ('Type "%%I"^|findstr /B /I /C:"name"') Do (
    	echo.%%~b|findstr /B /I /V /C:"Practice Arena" /C:"Event Results">nul
    	If !errorlevel!==0 echo.%%~b>>%_oFile%
    	)
    )
    PopD
    Special thanks to TheOutCaste.
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/914015

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice