How do include sub-folders in my file conversion script?

Viewed 120

I have modified a vbscript and batch file that allows me to convert HTML files to xlsx files in the current directory as follows

Script:

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Please specify the source and the destination files"
    Wscript.Quit
End If

xlsx_format = 51

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)

oBook.SaveAs dest_file, xlsx_format

oBook.Close False
oExcel.Quit

Batch file:

FOR /f "delims=" %%i IN ('DIR *.HTM* /b') DO to-xlsx.vbs "%%i" "%%~ni.xlsx"
del *.HTM /q

By including /s in the batch file I am able to convert files in the subfolders but they are still saved to the parent directory and I cant quite work out how to change that?

src_file is C:\Converter\Subfolder\FileName and dest_file is C:\Converter\FileName for a file in the subfolder

1 Answers

You will need to change

FOR /f "delims=" %%i IN ('DIR *.HTM* /b') DO to-xlsx.vbs "%%i" "%%~ni.xlsx"

to

FOR /R %%i IN (*.HTM* ) DO to-xlsx.vbs "%%i" "%%~ni.xlsx"

for /r means Recursive, so you are asking CMD to loop every file and his subdirectories, but only that they contain an .htm* file.

Related