从一个文件读取到另一个文件时,保持空白行完好无损 [英] Keeping blank lines intact when reading from one file to another

查看:118
本文介绍了从一个文件读取到另一个文件时,保持空白行完好无损的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码(批处理文件)从属性文件逐行读取另一个文件.问题是它正在从源文件中删除所有空白行.为了使空白行可用于目标文件,我应该怎么做?

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%"
)

推荐答案

FOR/F将始终跳过空行,因此必须避免空行.

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

这可以通过使用findstr或find在行前面加上行号来解决.

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%"

切换延迟扩展模式是必要的,因为您需要延迟扩展才能删除行号直到第一个冒号.
但是您需要禁用扩展,才能将%% L转移到line变量中,否则它将破坏感叹号,有时甚至插入符号.

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.

读取文件的set/p技术是另一种方法,在 SO:批处理文件:如何读取文件中进行了介绍.

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

这篇关于从一个文件读取到另一个文件时,保持空白行完好无损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆