I’m trying to remove all white space and code comments from my .php files by using the -W flag available from the PHP CLI. I’m using a batch file to recursively loop through each php file in my project. The batch command looks like:
FOR /F "tokens=*" %%G IN ('DIR /B /A-D /S *.php') DO "%php-exe-path%/php.exe" -w %%G > %%G
This just produces blank .php files (using the ‘>’ redirector), however using the appending redirector (‘>>’) works fine. I’m not sure if this is an issue with PHP locking the files or some problem with my batch command.
Can anyone help?
Generally: Don’t ever pipe the output of a command that works with a file into the file again. Instead pipe to a temporary file, delete the old one and rename the temporary one to the old name. Kinda like the following:
Side note: Please don’t use
for /fover the output ofdiras that can introduce some subtle but nasty errors. Instead use the proper way of iterating over files which is just plainfor, and recursively with the/rswitch: