Link to home
Start Free TrialLog in
Avatar of m0tek
m0tek

asked on

Python Script that Recives a few arguments and uses a few text files for manipulation

Hi ,
I had a batch file i tried to wrote but cant seem to get it right , i have a system that eventually can run Python script files and im trying to create a script / snippet which does the following

short explanation
it runs a report , and checks for a text file with status (for loop)  , if its completed - it goes to the 2nd loop

recieves 2 arguments (sys.argv[1], and [2])
Runs an executable file (subprocess.popen)
outputs the stdout to a log file with the name sys.argv[1].log
does a loop (for) on a the output txt file - and looks for the 2nd token

i.e - for /f "tokens=2" %n IN ('type %1__runjob.log') DO (thats in Batch)
 
the output of sys.argv[1].log is as such (its already outputted like that , just piped into a file)
Job 1234 Submitted
       ____
     (2nd token)

:checkjob
for /f "tokens=4" %x IN ('arik.exe show job %n ^| findstr "status"') DO
if %x==COMPLETE (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %a IN ('type %2_Agentlist.log') do arik.exe view report -s -q c:\%2\%datetime%_%a.csv "%1" "%x" network 0
goto done)
 
ELSE  goto checkjob
Avatar of sjklein42
sjklein42
Flag of United States of America image

Not sure what your actual code looks like, but within a .bat batch file, you need to double your percent signs (%%) for local variables (%a, %n and %x, etc), like this, for example

for /f "skip=6" %%a IN ('type %2_Agentlist.log') do arik.exe view report -s -q c:\%2\%datetime%_%%a.csv "%1" "%%x" network 0
goto done)

Open in new window

Avatar of HonorGod
Are you looking to correct this .bat file, or convert it to Python?
Avatar of m0tek
m0tek

ASKER

either is good
i am pasting the current batch (doesnt work)


::expecting %1 to be the Policy name and %2 the domain
arik.exe run job "Everyone_FC_Shares" "testbatch" > %1__runjob.log
for /f "tokens=2" %n IN ('type %1__runjob.log') DO
:checkjob
for /f "tokens=4" %x IN ('arik.exe show job %n ^| findstr "status"') DO
if %x==COMPLETE (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %a IN ('type %2_Agentlist.log') do arik.exe view report -sThftic -q c:\%2\%datetime%_%a.csv "%1" "%x" network 0
goto done)
 
ELSE  goto checkjob
 
:done
echo done
Can be confusing!  Need to double the percent signs for the "for" loop variables, like this:

::expecting %1 to be the Policy name and %2 the domain
arik.exe run job "Everyone_FC_Shares" "testbatch" > %1__runjob.log
for /f "tokens=2" %%n IN ('type %1__runjob.log') DO 
:checkjob
for /f "tokens=4" %%x IN ('arik.exe show job %%n ^| findstr "status"') DO 
if "%%x"=="COMPLETE" (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %%a IN ('type %2_Agentlist.log') do arik.exe view report -sThftic -q c:\%2\%datetime%_%%a.csv "%1" "%%x" network 0
goto done)
 
ELSE  goto checkjob
 
:done
echo done

Open in new window

Avatar of m0tek

ASKER

still having trouble , can anyone help me convert this to python?
If no Python takers, I'm quite sure the batch file could be made to work.

Please post an actual input file we can use to test it, along with an example or two of where it fails.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of mish33
mish33
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial