1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

How to create a batch file to read size of file on specific folder path

Discussion in 'Software Development' started by sky2013, Jul 16, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. sky2013

    sky2013 Thread Starter

    Joined:
    Jul 16, 2013
    Messages:
    5
    Dear All,

    I need to create a batch file which can read the size of file(e.g. 1kb, 2kb...etc) from specific folder and detect 0kb file and convert it to text file (showing 0kb file size) with alarm.

    I am not really sure how to write command for this batch file. Please help :-(
     
  2. peterh40

    peterh40

    Joined:
    Apr 15, 2007
    Messages:
    1,396
    First Name:
    Peter
    Here's an example using Powershell:

    # Test file size
    Param($myfile)
    $file = Get-Item $myfile
    $size = $file.Length
    If ($size -eq 0 ) {
    write-host "File $myfile is size zero!"
    [System.Media.SystemSounds]::Beep.Play()
    }
     
  3. sky2013

    sky2013 Thread Starter

    Joined:
    Jul 16, 2013
    Messages:
    5
    I only can use batch file application... I can't install powershell application because no internet connection.
     
  4. peterh40

    peterh40

    Joined:
    Apr 15, 2007
    Messages:
    1,396
    First Name:
    Peter
    TRy this VBS file, which can be run on any PC:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.GetFile("C:\Myfile.txt")

    If objFile.Size = 0 Then
    Wscript.Echo "The file is empty."
    Wscript.Echo CHR(7)
    End If
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,783
    So you are still on XP because I believe PS is installed by default on Vista and 7.

    You can get a files size one of two ways using a For loop in batch.

    I am on my phone right now but if you read the help for the FOR cmd you should see the modifier that shows the files size.
     
  6. sky2013

    sky2013 Thread Starter

    Joined:
    Jul 16, 2013
    Messages:
    5
    Thanks for the VBS. Is this only applicable if we already have a text document? What i need is, e.g. folder from D:\DVB Bitmap Encoder\MYS\Subs In is converted to .txt file if 0KB file size is detected with an alarm. I try to learn through Youtube but it is still confusing. Sorry :-(
     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,783
    Code:
    for %%G in ("C:\myfile.txt") do IF "%%~zG"=="0" echo ALARM
     
  8. sky2013

    sky2013 Thread Starter

    Joined:
    Jul 16, 2013
    Messages:
    5
    Can you show me a simple program how the script that you provided works? I have tried but i could not get the result. Somehow i have managed to create VBS to find 0kb files in a Directory.Any idea how i can create a batch file which will identify 0 in the text file and immediately trigger alarm to alert people. It also must delete the generated text file using VBS every 10 hours.
     
  9. sky2013

    sky2013 Thread Starter

    Joined:
    Jul 16, 2013
    Messages:
    5
    Any answer for this?
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1103792

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice