Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with script

Posted on 2006-11-29
2
Medium Priority
?
162 Views
Last Modified: 2010-04-30
I initially wrote a script that returns # of files, folders and folder size in a specific folder based on an input directory.  I am trying to modify the script to read from a source file, and it is not working.  Additionally, on directories with less than 100KB, it is not returning size.  Can someone help me de-bug it please?  Code is below.

Thanks,

IPT

*******************

Option Explicit
On Error Resume Next

Dim objfso, boolExcel, objXL, cellcount, nTotal, FolderCount, drive, fso, fl, folder, fsize, TotalSize, openfile, fstring_err

set objfso = createobject("scripting.filesystemobject")

if objfso.FileExists("C:\Program Files\Microsoft Office\Office11\excel.exe") then
      boolExcel = true
      Set objXL = WScript.CreateObject("Excel.Application")
end if

if boolExcel then
      objXL.Visible = TRUE
      objXL.WorkBooks.Add

      objXL.Columns(1).ColumnWidth = 30
      objXL.Columns(2).ColumnWidth = 30
      objXL.Columns(3).ColumnWidth = 30
      objXL.Columns(4).ColumnWidth = 30
      
      objXL.Cells(1, 1).Value = "Folder Name"
      objXL.Cells(1, 2).Value = "Number of Files"
      objXL.Cells(1, 3).Value = "Number of Folders"
      objXL.Cells(1, 4).Value = "Size of Root Folder"

      cellcount = 4
else
      set output = objfso.CreateTextFile("filecheck.txt",true)
      output.writeline "Folder Name,File Count,Size"
end if

Set fso = CreateObject("Scripting.FileSystemObject")
Set fstring_err = fso.CreateTextFile("c:\errorlog.txt", True)
Set getfilepath = fso.GetFile("c:\checkfilesize.txt")
Set openfile = getfilepath.OpenAsTextStream(1, -2)
        Do While Not openfile.AtEndOfStream
                  strfilepath = ""
                  strfilepath = openfile.ReadLine
                              For Each strfilepath in getfilepath
                                    Set fl = fso.GetFolder(strfilepath)                        
                                          GetFileCount fl
                                          objXL.Cells(cellcount,1) = fl.path
                                          objXL.Cells(cellcount,2) = nTotal
                                          objXL.Cells(cellcount,3) = FolderCount
                                          objXL.Cells(cellcount,4) = fsize
                                          cellcount = cellcount + 1
                              Next
        Loop
            msgbox "Process completed!"
      
openfile.Close
fstring_err.Close
Set fso = Nothing
Set wsh = Nothing

Function GetFileCount(fl)
Dim nextFolder, folders, Files, SubFolders, SubFolder, firstpass, nextf
err.clear
nextFolder = fl.Path
Set Files = fl.Files
Set folders = fl.subfolders
nTotal = nTotal + Files.Count
FolderCount = FolderCount + folders.Count
Set SubFolders = fl.SubFolders
For Each SubFolder In SubFolders
      nextf = nextf & folders.Count
    nextFolder = nextFolder & GetFileCount(SubFolder)
            fsize = round(fl.size /1024)
            if len(fsize) >= 4 then
                  fsize = Round(fsize /1024) & " MB"
            else
                  fsize = Round(fsize) & " KB"
            end if      
      if err.number <> 0 then
            msgBox SubFolder & " - " & err.description
      end if            
Next
End Function
0
Comment
Question by:iptrader
[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
2 Comments
 
LVL 12

Accepted Solution

by:
jkaios earned 2000 total points
ID: 18041436
Try the new function below (SizeToString) to convert the filesize.

For Each SubFolder In SubFolders
    nextf = nextf & folders.Count
    nextFolder = nextFolder & GetFileCount(SubFolder)
          fsize = SizeToString(fl.size)                                 '<---- TRY IT HERE
     if err.number <> 0 then
          msgBox SubFolder & " - " & err.description
     end if          
Next


'================================
Function SizeToString(dBytes)

 Dim sWork  'String
 Dim vWork  'Variant
 Dim lWork   'Long
 Dim dTemp 'Double
 
 sWork = FormatNumber(dBytes)
 vWork = Split(sWork, ",")
 lWork = UBound(vWork)
 
 Select Case lWork
   Case 0      '// Hundred
     sWork = " bytes"
     vWork = dBytes
   Case 1      '// Thousand
     dTemp = dBytes / 1024
     sWork = " KB"
     vWork = Round(dTemp, 2)
   Case 2      '// Million
     dTemp = dBytes / (1024 ^ 2)
     sWork = " MB"
     vWork = Round(dTemp, 2)
   Case 3      '// Billion
     dTemp = dBytes / (1024 ^ 3)
     sWork = " GB"
     vWork = Round(dTemp, 2)
   Case 4      '// Trillion
     dTemp = dBytes / (1024 ^ 4)
     sWork = " TB"
     vWork = Round(dTemp, 2)
 End Select
 
 If InStr(vWork, ".") Then
   SizeToString = FormatNumber(vWork, 1) & sWork
 Else
   SizeToString = vWork & sWork
 End If
 
End Function
'================================
0
 

Author Comment

by:iptrader
ID: 18041520
jkaios, it is still not returning size for anything under 100 KB.  Sorry.

IPT
0

Featured Post

Independent Software Vendors: 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 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