davil Posted June 8, 2010 Share Posted June 8, 2010 I used to always be able to write to windows text files and create newlines with a simple \r\n but now the text files are being written and \r\n appears in the textfile instead of a newline. I've also tried just \r or just \n but to no avail. I need to edit the services file on multiple XP machines here and add a line, but only if it hasn't been added before. My Code is far from finished but I want to be able to log the process to a text file. also the services file that I'm trying to append to is also adding the \r\n instead of a newline. Here's my code: <?php $windir=getenv('WINDIR'); $dcserver=getenv('LOGONSERVER'); $hostname=getenv('COMPUTERNAME'); $logfile="$dcserver\\NETLOGON\\sap_update_2010\\logs\\$hostname.txt"; $svcfile="$windir\\system32\drivers\\etc\\services"; $logfileHandle = fopen($logfile, 'w'); //OPEN LOG FILE FOR OUTPUT $inputfileHandle = fopen($svcfile, 'r'); //OPEN SERVICES FILE FOR READING (WILL WRITE TO THIS LATER) fwrite($logfileHandle, 'Log File Start... \r\n Test Line 2'); // MORE LOGGING TO COME LATER BUT FOR THE MOMENT I CAN'T GET NEWLINE TO WORK //LOAD IN SERVICES FILE FOR CHECKING $number_of_lines=count(file($svcfile)); //set a variable for amount of lines in services file if ($number_of_lines>10){ //If the file is normal length & no issues $data1 = fread($inputfileHandle, filesize($svcfile)); //read all the lines into a large variable fclose($inputfileHandle); //once read into an variable, we can now close the file handle $array1 = explode("\r\n", $data1); // Explode all Data into an array - line by line for ($a=0;$a<= $number_of_lines-1;$a++){ //step through line by line searching for the sapmsPR2 string if (strstr($array1[$a],'sapmsPR2')){ $done=1; } } if (!$done){ // If the line hasn't already been added, we need to add this line to the file $addline='\nsapmsPR2 3600/tcp #SAP PORT - ROLLED OUT BY SCRIPT'; // This is the line to be added to services file $outp1 = fopen($svcfile, 'a'); //OPEN SERVICES FILE FOR OUTPUT [append] fwrite($outp1,$addline); // WRITE THE LINE TO THE FILE [at the end] fclose($outp1); // CLOSE WHEN FINISHED //ALSO WE NEED TO COPY THE SAPLOGON.INI file to the machine if (!copy($inifile, $newinifile)) { fwrite($logfileHandle, 'Error - failed to copy SAPLOGON.INI across...\n'); }else{fwrite($logfileHandle, 'SAPLOGON.INI copied successfully to Windows folder...\n');} } } //Finish up by closing the log file fclose($logfileHandle); ?> Oh yes, I suppose an important thing to note here is that I'm trying to compile a simple windows CLI exe. to do this I've tried the following with no luck: bamcompile [http://www.bambalam.se/bamcompile/], PHC [http://wiki.swiftlytilting.com/Phc-win#Download] But I've a feeling it's not the compilers because even my WAMP setup is doing the same thing now, whereas before \r\n was fine. I know when reading in files and 'exploding' the data into an array - \r\n works fine, do I need to output to a file differently? Another stupid Question - Is it because I upgraded my own machine to Windows 7 since I last tried something like this - does Win7 treat files differently? I tried to figure out how to do this in binary but I've a feeling it might be easier than all that. If anybody can help me out I'd really appreciate it because I'm on a deadline and don't want to have to re-write this in nasty Autohotkey script or freebasic. and I'm no good at any other decent scripting languages. P.S. just in case anybody wonders, I've been running this EXE as an administrator on my win7 machine so as to give access to services file, but will wrap it in proper admin credentials later for running on the XP machines. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 8, 2010 Share Posted June 8, 2010 \r\n does not work with ' use " instead. Quote Link to comment Share on other sites More sharing options...
davil Posted June 8, 2010 Author Share Posted June 8, 2010 Perfect!! I new it would be something simple. Thanks very much ignace!! Quote Link to comment Share on other sites More sharing options...
davil Posted June 8, 2010 Author Share Posted June 8, 2010 Also I've just noticed that reading the file into a string and then changing it to an array was an unnecessary step - anybody wanting to do similar code to the above should use file() function rather than fread() and use the foreach function to step through the array rather than a 'for' up to the counted number of lines in the file - the script should be far more efficient that way. I was just throwing that one together and learned quite a bit about my bad coding practices so far. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.