Solved

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

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

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

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

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

821 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