2

I am reading line by line from a properties file to another file using below code(batch file). The problem is It is removing all the blank lines from the source file. What changes I should do in order to make blank lines available to destination file?

FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.SRC"`) DO (
  ECHO %%A>>"%FILE%"
)
Tapas Jena
  • 1,069
  • 4
  • 14
  • 23

2 Answers2

4

FOR /F will always skip empty lines, so you have to avoid empty lines.

This can be solved with prepending the lines by a line number with findstr or find.

Then you only need to remove the line number.

(
  setlocal DisableDelayedExpansion
  for /F "delims=" %%L in ('findstr /n "^" "%FILE%.src"') do (
    set "line=%%L"
    setlocal EnableDelayedExpansion
    set "line=!line:*:=!"
    echo(!line!
    endlocal
  )
) > "%FILE%"

The toggling of the delayed expansion mode is necessary, as you need delayed expansion for removing the line number up to the first colon.
But you need disabled expansion for tranfering the %%L to the line variable, else it would destroy exclamation marks and sometimes carets.

The set/p technic to read a file is a different approach, described at SO:Batch files: How to read a file?

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Is there anything like simply applying some [option] to the for loop will override this default behavior(skipping blank line). – Tapas Jena Feb 21 '14 at 12:55
  • 1
    @TapasJena No, else it wouldn't be necessary to build such a solution – jeb Feb 21 '14 at 13:08
  • ss64 describes the basics, but the advanced issues are sometimes a bit unclear there – jeb Feb 21 '14 at 13:22
  • @jed....I am new to batch scripting and using script from below post. Can you please suggest what changes required in that script to achieve this. http://stackoverflow.com/questions/7518539/using-dos-batch-script-find-a-line-in-a-properties-file-and-replace-text – Tapas Jena Feb 21 '14 at 17:02
  • @TapasJena You should open a new question or edit your question (when it's not to different) – jeb Feb 21 '14 at 17:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48092/discussion-between-tapas-jena-and-jeb) – Tapas Jena Feb 21 '14 at 17:39
0
set SOMEUSELESSVAR=Nonsense

set PRINT=false
set DoPRINT=false
setlocal DisableDelayedExpansion

for /F "delims=" %%a in ('findstr /n "^" "%~f0"') do (
  set "OUTPUT=%%a"
  setlocal EnableDelayedExpansion
  set "OUTPUT=!OUTPUT:ReplaceSomeText=WithSomeOtherTextIfYouNeed!"
  set "OUTPUT=!OUTPUT:*:=!"
  
  if !OUTPUT!==:TEXT_End (
    goto TEXT_End
  )
  if !PRINT!==true (
    echo(!OUTPUT!>>File.txt
    set DoPRINT=true
  )
  if !OUTPUT!==:TEXT_Start (
    set DoPRINT=true
    set PRINT=true
  )
  if !DoPRINT! EQU true (
    endlocal & set PRINT=true& set DoPRINT=true
  ) else (
    endlocal
  )
)

goto TEXT_End

:TEXT_Start
%SOMEUSELESSVAR%
!SOMEUSELESSVAR!
;SOMEUSELESSVAR
REM SOMEUSELESSVAR
^^SOMEUSELESSVAR
^SOMEUSELESSVAR

SOMEUSELESSVAR
:TEXT_End

pause
exit

This will read text in a .bat file from between :TEXT_Start to :TEXT_End and print the text without the labels (:TEXT_Start and :TEXT_End) into a File.txt and then move on with the code after :TEXT_End. It will keep blank lines and variables any how will not be expanded and comments will be printed as they are. If you need the File.txt in a sub-directory or any other directory you can apply the path directly or with some %path% variable. This will work on windows 7 and maybe more or less. To use a other input file to read lines from replace %~f0 with a valid file path and adjust the :TEXT_Start and :TEXT_End labels as you need them. If you need to replace text you can adjust the line ' set "OUTPUT=!OUTPUT:ReplaceSomeText=WithSomeOtherTextIfYouNeed!" ' as you need and add more replacement "filters" in the same way before the line ' set "OUTPUT=!OUTPUT::=!" '. If you use special characters in such replacement filters you may have to escape them by doubleing the special character od by prefixing it with ^.

Edit: Did add filter function to make clear where you can replace text.

Mugolumbu
  • 31
  • 1
  • 6