Solved

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

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

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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now