Solved

Using a script, how do yo you delete a text file if it is blank?

Posted on 2014-09-12
5
421 Views
Last Modified: 2014-09-18
I want a script to check a file, and if it is blank delete it, and if it's not blank just leave it.

if exist sample.txt  ......delete sample.txt

I need to change the script so that if the file is blank, or 0kb then and only then should it be deleted.
0
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40320108
Do you only want a Batch script? If not, here is a powershell script

Get-ChildItem c:\scripts\*.* -include *.txt, | where {$_.length -eq 0} | del
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40320109
Command line:
for %R in (filename.txt) do if %~zR equ 0 del filename.txt

Open in new window


Bat file
for %%R in (filename.bat) do if %%~zR equ 0 del filename.txt

Open in new window

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40320132
Here is a VBScript also

Dim oFSO
Dim oFile
Dim ts
strFilePath = "<insert path to file here>"
Set oFSO = CreateObject("Scripting.FileSystemObject")
if oFSO.FileExists(strFilePath) then
   Set oFile = oFSO.GetFile(strFilePath)
   strLine = ""
   linecount = 0
   blnShouldDelete = True
   Set ts = oFile.OpenAsTextStream(1, -2)
   Do While ts.AtEndOfStream <> True
      linecount = linecount + 1
      strLine = ts.ReadLine

      if Len(strLine) > 0 then
         blnShouldDelete = False
      end if 
   Loop
   ts.Close

   if (linecount = 0 Or linecount = 1) And blnShouldDelete = True then
       oFile.Delete
   end if
end if
set oFSO = Nothing

Open in new window

0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40320420
Here's how I would code it in a BAT file:

REM Delete file if it exists, and is empty (0 bytes length)
if exist "sample.txt" (
  for %%A in ("sample.txt") do (
    if %%~zA EQU 0 (
      del "%%~A">NUL
    )
  )
)

Open in new window

~bp
0
 

Author Closing Comment

by:100questions
ID: 40330344
Used the bat file version. Thank you.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
Configuring Remote Assistance for use with SCCM
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question