0
FOR /F "usebackq" %%i IN (`hostname`) DO SET MYVAR=%%i
SET subkey1=%random%%random%%random%%random%%random%%random%
SET subkey1=%subkey1:0=a%
SET subkey1=%subkey1:1=b%
SET subkey1=%subkey1:2=c%
wmic computersystem where caption="%MYVAR%" rename %subkey1%

This is block of code run in Windows BATCH file, I'd like to adapt it for Python app but I can't think of a way to do so.

I've used:

os.system('cmd xxxx')

to run single line of code, but I don't think this will solve my issue with big block of code that can't be run separately.

crNh
  • 45
  • 8
  • create a script `script.cmd` with all the logic you need and run the script from python – balderman Jul 14 '21 at 08:49
  • Put the Windows CMD block into a batch file (`script.bat`) and run it via `os.system(cmd /C call "script.bat")`… – aschipfl Jul 14 '21 at 08:49
  • That's possiblity, however if I want to run it on a different PC - do I need to get script file with my app or it's gonna get compiled all together, or how does this work? Though, thanks for the idea! – crNh Jul 14 '21 at 08:51
  • @crNh see my answer. You dont have to copy the script to other machines. – balderman Jul 14 '21 at 08:58
  • That is not a block of code run from Windows [[tag:cmd]], or more technically, it will not work if run from it. That is the content of a [[tag:batch-file]], and there is a difference. Your question specifically mentions batch file, so please clarify by editing your assigned tags accordingly. – Compo Jul 14 '21 at 10:24
  • Does this answer your question? [Execute batch file using Python script](https://stackoverflow.com/questions/42295036/execute-batch-file-using-python-script) – bad_coder Jul 14 '21 at 14:04

1 Answers1

1

below is a way to do it (creating the script on the fly)

import os

script = '''FOR /F "usebackq" %%i IN (`hostname`) DO SET MYVAR=%%i
        SET subkey1=%random%%random%%random%%random%%random%%random%
        SET subkey1=%subkey1:0=a%
        SET subkey1=%subkey1:1=b%
        SET subkey1=%subkey1:2=c%
        wmic computersystem where caption="%MYVAR%" rename %subkey1%'''
with open('script.cmd', 'w') as f:
    f.write(script)
os.system('script.cmd')
balderman
  • 22,927
  • 7
  • 34
  • 52