有些时候,在Oracle的Package程序中在把相关数据写入文件时候并不是有数据才才执行如下语句:
g_file_handle := utl_file.fopen(g_output_file_dir,
l_war_file,
'W');
如果执行了以上代码,那么就生成了一个文件,不管有没有数据写入。
但是确实存在并没有数据需要写入的情况,那么这个文件就是个空文件。对于空文件返回给用户并没有意义,所以需要判断如果文件为空,那就不把此文件返回给用户。
想到的方法:
[@more@]1. 判断文件中的数据行数,如果大于等于1,说明有数据。
2. 判断是否有非空字符
3. 是不是DOS命令有直接的函数来判断呢?
4. 修改源程序,在没有数据需要写入的时候,不执行 fopen
但以上方法中4是可行的,但考虑到一些实际情况,还是要从BAT 文件上动手,这样是最小的改动,带来的风险小。其他方法均没有找到实现方法。
经过研究使用如下方法实现:
我们知道在Windows 下的Dos 下,有FIND 这个命令:
D:>find/?
Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies the text string to find.
[drive:][path]filename
Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
我就想,空文件是什么字符也都没有的,而在我这里有数据的文件里肯定是有空格的。那我是否可以通过FIND 找如果有空格的就是有数据的。
执行以下命令:
查找有空格的数据文件:
D:>find /c " " d:a.txt
---------- D:A.TXT: 15
查找没有数据的文件:
D:>find /c " " d:.txt
---------- D:B.TXT: 0
从以上文件发现,输出中会把查找到的个数以数字的形式显示出来。
那怎么获得这个数呢?
我观察到输出固定是有两个冒号。
那么就把输出内容再写入到一个临时文件temp123.txt中
可以通过以下命令获得数字
FOR /F "tokens=3,3 delims=:" %%i in (temp123.txt) do (set lv_cnt=%%i)
具体在BAT 中的实现如下:
Set output_file=D:A.TXT
Set temp123=D:TEMP123.TXT
FIND /C " " %output_file %> %temp123%
FOR /F "tokens=3,3 delims=:" %%i in (%temp123%) do (set lv_cnt=%%i)
if %lv_cnt% EQU 0 (GOTO 没有数据内容,到要去的地方)
if %lv_cnt% GTR 0 (GOTO 有数据内容,到要去的地方)
---------------
如果你有更加简单的方法,不妨共享一下。^*^
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/45788/viewspace-964706/ ,如需转载,请注明出处,否则将追究法律责任。