Windows BAT:测试特定文件是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5483960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows BAT : test if a specific file is empty
提问by Réj?me
I would like to check if a specific file is empty in a windows .bat file. Here is my non working script :
我想检查 Windows .bat 文件中的特定文件是否为空。这是我的非工作脚本:
set dir="C:\test"
set file="%dir%\fff.txt"
cd %dir%
if %file%%~zi == 0 exit
ftp -s:"%dir%\ftp.action"
exit
Could you help me debug this please ?
你能帮我调试一下吗?
采纳答案by jeb
Or try it with
或者试试
@echo off
set "dir=C:\temp"
set "file=%dir%\a.txt"
call :CheckEmpty "%file%"
goto :eof
:CheckEmpty
if %~z1 == 0 exit
ftp -s:"%dir%\ftp.action"
goto :eof
The main difference is that I use a function call and use the %~z1, as the modifiers only works for paramters like %1, %2..%9 or for-loop parameters like %%a ...
主要区别在于我使用函数调用并使用 %~z1,因为修饰符仅适用于 %1、%2..%9 之类的参数或 %%a ... 之类的 for 循环参数。
回答by BearCode
batch solution using file compare:
使用文件比较的批处理解决方案:
type nul > blank
fc myfile blank > nul
if errorlevel 1 echo myfile is not empty
回答by Mark Mooibroek
Try this:
尝试这个:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\boot.ini", ForReading)
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close