• Status: Solved
  • Priority: Low
  • Security: Private
  • Views: 88
  • Last Modified:

VBscript & Batch file: list files, directories and datemodified

Hello Experts,

I have a root folder which contains multiple .jar files. Those files are located in subdirectories folders.
I would like to set up a batch or a vbscript in which you report the name of the .jar file in a csv file and a result.csv file should be generated with the following attributes:

Example of result.csv
Name;location;date-modified
Toto.jar;C:\Root\folder1\;18/09/2018
Titi.jar;C:\Root\folder3;25/09/2018

Reference root can be reported directly in the script.


Thank you very much for your help.
0
LD16
Asked:
LD16
  • 6
  • 4
  • 2
2 Solutions
 
Bill PrewCommented:
Here's a small BAT approach.

@echo off
setlocal

set BaseDir=c:\temp
set Filter=*.jar
set Output=result.csv

echo.Name;Location;Date-Modified>"%Output%

for /r "%BaseDir%" %%F in ("%Filter%") do (
    echo %%~nxF;%%~dpF;%%~tF>>"%Output%
)

Open in new window


»bp
0
 
Bill PrewCommented:
And if you really only want the date of last modified, use:

@echo off
setlocal

set BaseDir=c:\temp
set Filter=*.jar
set Output=result.csv

echo.Name;Location;Date-Modified>"%Output%

for /r "%BaseDir%" %%F in ("%Filter%") do (
    for /f "tokens=1" %%a in ("%%~tF") do (
        echo %%~nxF;%%~dpF;%%~a>>"%Output%
    )
)

Open in new window


»bp
0
 
Fabrice LambertFabrice LambertCommented:
Hi,

easy enough with the help of a recursive function.
VBS code below:
Public Sub browseDirectories()
    Const root As String = "c:\myFolder"
    Dim fso As Object       '// Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim fld As Object       '// Scripting.folder
    Set fld = fso.GetFolder(root)
    
    Dim col As Collection
    Set col = browseFolder(fld)
    writeCSV col, "c:\myFolder\result.csv"
    Set col = Nothing
    Set fld = Nothing
    Set fso = Nothing
End Sub

Private Sub writeCSV(ByRef data As Collection, ByVal path As String)
    Const ForAppending = 8
    Dim fso As Object       '// Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim stream As Object    '// Scripting.TextStream
    Set stream = fso.OpenTextFile(path, ForAppending, True)
    stream.WriteLine "Name;location;date-modified"
    Dim item As Variant
    For Each item In data
        stream.WriteLine item
    Next
    stream.Close
    Set stream = Nothing
    Set fso = Nothing
End Sub

Private Function browseFolder(ByRef folder As Object) As Collection
    Dim col As Collection
    Set col = New Collection
    
    Dim file As Object      '// Scripting.file
    For Each file In folder.Files
        If (file.Name Like "*.jar") Then
            col.Add file.Name & "," & file.ParentFolder.path & "," & Format(file.DateLastModified, "dd/mm/yyyy")
        End If
    Next
    
    Dim fld As Object       '// Scripting.folder
    For Each folder In folder.SubFolders
        mergeCollections browseFolder(folder), col
    Next
    Set browseFolder = col
End Function

Private Sub mergeCollections(ByRef src As Collection, ByRef trg As Collection)
    Dim item As Variant
    For Each item In src
        trg.Add item
    Next
End Sub

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LD16Author Commented:
Thank you very much for your help.
@Bill : I test your last version and it works! I just wondering if it is possible to add hours-minutes in datemodified output.
@Fabrice Lambert: I tested your vbscript proposal however I have the following attached error.2018-03-05-13_52_30-Windows-Script-H.png
Thank you very much for your help.
0
 
Bill PrewCommented:
Sure, try this:

@echo off
setlocal

set BaseDir=c:\temp
set Filter=*.jar
set Output=result.csv

echo.Name;Location;Date-Modified>"%Output%

for /r "%BaseDir%" %%F in ("%Filter%") do (
    echo %%~nxF;%%~dpF;%%~tF>>"%Output%
)

Open in new window


»bp
0
 
LD16Author Commented:
Tested and it works! Thank you very much for your help!
0
 
Fabrice LambertFabrice LambertCommented:
I wrote the script with a VBA editor, guess VBS is a little different with constants déclarations.
Remove the data type at the root constant declaration:
Const root  = "c:\myFolder"

Open in new window

0
 
Bill PrewCommented:
And if you also want a VBS approach, here is a simple attempt at that.  It writes to STDOUT, so to get the output in a file just do at a command prompt:

cscript //nologo EE29086923.vbs >result.csv

Const cBaseDir = "c:\temp"
Const cShowExt = "jar"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Wscript.Echo "Name;Location;Date-Modified"

ScanFolder(objFSO.GetFolder(cBaseDir))

Function ScanFolder(objFolder)
    For Each objFile In objFolder.Files
      If Lcase(objFSO.GetExtensionName(objFile)) = Lcase(cShowExt) Then
          Wscript.Echo objFile.Name & ";" & objFile.ParentFolder & ";" & FormatDateTime(objFile.DateLastModified, vbGeneralDate)
      End If
    Next
    For Each objSubFolder in objFolder.SubFolders
        ScanFolder(objSubFolder)
    Next
End Function

Open in new window


»bp
0
 
LD16Author Commented:
Thank you very much for your proposal.
Is there a way to add size file in batch and vbscript approaches?

Thank you very much for your help.
0
 
Bill PrewCommented:
Here they are.

@echo off
setlocal

set BaseDir=c:\temp
set Filter=*.jar
set Output=EE29086923.csv

echo.Name;Location;Size;Date-Modified>"%Output%

for /r "%BaseDir%" %%F in ("%Filter%") do (
    echo %%~nxF;%%~dpF;%%~zF;%%~tF>>"%Output%
)

Open in new window

Const cBaseDir = "c:\temp"
Const cShowExt = "jar"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Wscript.Echo "Name;Location;Size;Date-Modified"

ScanFolder(objFSO.GetFolder(cBaseDir))

Function ScanFolder(objFolder)
    For Each objFile In objFolder.Files
      If Lcase(objFSO.GetExtensionName(objFile)) = Lcase(cShowExt) Then
          Wscript.Echo objFile.Name & ";" & objFile.ParentFolder & ";" & objFile.Size & ";" & FormatDateTime(objFile.DateLastModified, vbGeneralDate)
      End If
    Next
    For Each objSubFolder in objFolder.SubFolders
        ScanFolder(objSubFolder)
    Next
End Function

Open in new window


»bp
0
 
LD16Author Commented:
Thank you very much for your help!!
0
 
Bill PrewCommented:
Welcome.


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now