Solved

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

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unmap server backup drives 6 27
domain controllers numbers 4 77
windows 7 starter missing password 21 78
Set a time range for screen lockout for Windows 10/7 OS 2 18
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
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…

810 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