Luis Diaz
asked on
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-modifie d
Toto.jar;C:\Root\folder1\; 18/09/2018
Titi.jar;C:\Root\folder3;2 5/09/2018
Reference root can be reported directly in the script.
Thank you very much for your help.
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-modifie
Toto.jar;C:\Root\folder1\;
Titi.jar;C:\Root\folder3;2
Reference root can be reported directly in the script.
Thank you very much for your help.
And if you really only want the date of last modified, use:
»bp
@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%
)
)
»bp
Hi,
easy enough with the help of a recursive function.
VBS code below:
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
ASKER
Sure, try this:
»bp
@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%
)
»bp
ASKER
Tested and it works! Thank you very much for your help!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
»bp
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
»bp
ASKER
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.
Is there a way to add size file in batch and vbscript approaches?
Thank you very much for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much for your help!!
Welcome.
»bp
»bp
Open in new window
»bp