REM Create a backup of the Original File for safety's sake. REM End the local Non-Delayed expanion environement needed for the loop explicitly. REM End the local delayed expantion environment while still within the loop. REM Replace don't expand comments continually (FindStr prefix is needed to print blank lines correctly) REM Enable Delayed expantion within the loop in order to process the line contents correctly. REM Note: Delayed expantion is diabled by default, however we're just being 'sure' as the FindStr prefix is needed to print blank lines correctly, so we can t just use "type".įOR /F "Tokens=*" %%A IN ('Findstr /n "^" "%_FilePath%"') DO ( REM Make sure Delayed expantion is not turned on for the loop to interpret FindStr Correctly. REM Set extra replace statement to keep commented matches at two comments depth if the replace string is a comment. REM Example Usage: FART.cmd "C:\Admin\Stack_Exchange\40510701_FART\file.java" "abc() " OFF REM Description: Replaces Text (Argument 2) with alternate Text (Argument 3) in File (Argement 1) I have the sample file I created and used and the example results are shown after the code below. I have set the script to create a backup of the original files just in case I missed some scenario I didn't plan for. The below script should meet your needs using only native windows CMD Interpreter. This really took some doing as it has been at least a year ot two since I used this on anythign as complex as code which gives you all sorts of fun issues. So I spent several long hours through the night working on a version of the script which can replace the comments using native batch only. (It's only in the last 2 to 3 years that I feel confident enough that I will have most of the Powershell I need available to me, so I've begun to use it more Particularly for this type of issue) I have worked mostly within that sphere for the last 10 years or so as I got sick the inconsistencies in VBScript, and needed to keep from using anything not available in the native OS wherever possible. I understand how that is and I've spent a long night and morning working on remembering how I did this last time and writing the script below for you. In a file, there were multiple occurrences and it printed 6 lines where occurrence was found but replaced none of them. Secondly, is there restriction that only first instance can be replaced in fart. This works but also produces some warnings. I used fart.exe -C file.java abc() "\/\/abc() " Unfortunately, none of them are working and I am out of ideas. Any idea on how to proceed with this will be really helpful.įart.exe file.java abc() //abc() fart.exe -C file.java abc() //abc() fart.exe -C file.java "abc() " "//abc() " fart.exe -C file.java abc() ^/^/abc() fart.exe -C file.java abc() \/\/abc() I was trying to use find and replace text (FART) utility to replace text. Thus, I need to add double forward slash. So text (say abc() ) will become commented text (//abc() ). One of the files have text which needs to be commented. I am trying to create batch script that edits few java files in project due to some development environment issues.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |