Windows BAT:测试特定文件是否为空

wsxa1bj1  于 2023-03-31  发布在  Windows
关注(0)|答案(4)|浏览(429)

我想检查一个特定的文件是否是空的windows .bat文件.这里是我的非工作脚本:

set dir="C:\test"
set file="%dir%\fff.txt"

cd %dir%
if %file%%~zi == 0 exit
ftp -s:"%dir%\ftp.action"
exit

你能帮我调试一下吗?

kxxlusnw

kxxlusnw1#

或者试试用

@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

主要的区别是我使用了一个函数调用,并使用了%~z1,因为修饰符只适用于像%1,%2 ..%9这样的参数或像%%a...这样的for-loop参数。

bf1o4zei

bf1o4zei2#

使用文件比较的批处理解决方案:

type nul > blank
fc myfile blank > nul
if errorlevel 1 echo myfile is not empty
kcwpcxri

kcwpcxri3#

for /F %%A in ("myfile") do If %%~zA equ 0 echo myfile is empty
avkwfej4

avkwfej44#

试试这个:

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

相关问题