Windows BAT : test if a specific file is empty

前端 未结 3 1477
太阳男子
太阳男子 2021-01-05 02:25

I would like to check if a specific file is empty in a windows .bat file. Here is my non working script :

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

c         


        
相关标签:
3条回答
  • 2021-01-05 02:55

    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
    
    0 讨论(0)
  • 2021-01-05 02:56

    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 ...

    0 讨论(0)
  • 2021-01-05 03:01

    batch solution using file compare:

    type nul > blank
    fc myfile blank > nul
    if errorlevel 1 echo myfile is not empty
    
    0 讨论(0)
提交回复
热议问题