Jump to content

Edit String in text file question


andytan91

Recommended Posts

Hello guys i am new to PHP. Basically what i am doing is an edit audit policy function. Basically the codes below are contained in a batch file. Users will run it in order to check whether he/she meets the requirement. If "Service Pack 2" is present, a pass notice will be given.

 

What i want to do is to give system administrator the privilege to edit the audit policy, the argument to be changed will be located in  findstr /C:"DEFINE ARGUMENT". I have managed to changed the file extension from .bat to .txt in order for php to read the text..now i need php to be able to edit the text in the DEFINE ARGUMENT part. Hence, if the system administrator inputs the desired text in an edit policy form, the text will replace the "Service Pack 2" in the text file. Ideas are welcome..thanks alot!

 

@echo off 
systeminfo | findstr /C:"Service Pack 2" 
if %errorlevel%==0 (
echo Service_Pack_Requirement:Pass  >> C:\final.doc
) else (
echo Service_Pack_Requirement:Fail >> C:\final.doc
)



Link to comment
https://forums.phpfreaks.com/topic/208102-edit-string-in-text-file-question/
Share on other sites

I have managed to changed the file extension from .bat to .txt in order for php to read the text

PHP can read any text based file. There is no need to change the extension.

 

What you'll want to do is first read the batch file

$batch_file = 'filename.bat';
$batch_contents = file_get_contents($batch_file);

Now to change the argument you'd use a small bit of regex

$newArgument = 'NEW POLICY HERE';
$batch_contents = preg_replace('~findstr /C:"([a-z0-9_ -]+)"~i', 'findstr /C:"'.$newArgument.'"', $batch_contents);

Lastly you'd rewrite the new contents to the batch file

file_put_contents($batch_file, $batch_contents);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.