Solved

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

Posted on 2014-09-12
5
419 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 54

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

734 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