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.
LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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 PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you very much for your help!!
0
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.