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%%d ate:~-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
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
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
Are you looking to correct this .bat file, or convert it to Python?
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%%d ate:~-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
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
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window