In a windows cmd terminal I am using something like dir *.mp3 *.wma *.m4a /s /b > c:\temp\random.tmp to save filenames with paths to the text file c:\temp\random.tmp. It works really well and is fast from inside of vbscript. The problem that I am having is that the text file is saved with a code page of 437 not the windows default. I have tried changing the code page in the command terminal before i redirect the folder contents into the text file and that does not seem to work.
In the windows cmd terminal if I type the text file I see "Beyoncé - Trust In Me.mp3", in vbscript i see "Beyonc, - Trust In Me.mp3", and if i open the text file in windows i see "Beyonc, - Trust In Me.mp3".
If I set the cmd terminal code page to 1252 then the "é" is only replaced with a "," in vbscript. I have tried using adodb with all of its character set and nothing seems to work.
here is my code, can someone help?
'dim MediaExtensions
'MediaExtensions = Array("mp3", "wma", "m4a")
'SourcePath = "C:\Users\Rob\Desktop\Script Project\work in progress\Jukebox\mp3\"
'wscript.echo ListMediaFiles(SourcePath, MediaExtensions)
Function ListMediaFiles(SourcePath, MediaExtensions)
Dim ListFiles, MediaFSO, MediaPath, TempFile, CMDShell, OpenFile, x, objShell 'declare variables"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set MediaFSO = CreateObject("Scripting.FileSystemObject") 'create FileSystemObject MediaFSO
Set CMDShell = CreateObject("WScript.Shell")
strFolder = CMDShell.CurrentDirectory
TempFile = MediaFSO.GetTempName 'Create unique (random) name for the TempFile
MediaPath = MediaFSO.GetSpecialFolder(2) 'the 2 = the system temp folder. in this case it is c:\temp
TempFile = MediaPath & "\" & TempFile 'join the MediaPath and TempFile to create a complete path to file. c:\temp\random.tmp
For each x in MediaExtensions ' create search string from SourcePath and MediaExtensions
FileExtentions = FileExtentions & Chr(34) & lcase(trim(SourcePath)) & "*." & x & Chr(34) & " " 'the x is each element in the MediaExtensions array
Next
ListFiles = "dir " & FileExtentions & " /s /b" 'create search string. using the example above the search string = dir *.mp3 *.wma *.m4a /s /b. We can use the dos dir command to search for more than one extension at the same time. The /s means search all sub folders. The /b is to return only the path and filename.
CMDShell.Run "%comspec% /c " & ListFiles & " >" & TempFile, 0, True 'Gather file names using CMDShell to run "dir *.mp3 *.wma *.m4a /s /b" and saved in "c:\temp\random.tmp."
'CMDShell.Run "%comspec% /k " & ListFiles & " >" & TempFile, 3, True ' use this if you need to trouble shoot the command line, it will not show you the command line but if there is an error your will see it
'chcp 1252&
set CheckFile = MediaFSO.GetFile(TempFile)
If CheckFile.Size > 0 then
'*********************************************
'Dim objStream
'Set objStream = CreateObject("ADODB.Stream")
'objStream.CharSet = "iso-8859-1"
'objStream.Open
'objStream.LoadFromFile(TempFile)
'ListMediaFiles = objStream.ReadText()
'objStream.Close
'msgbox ListMediaFiles
'*********************************************
'cint(objFile.Size / bytesToKb)
Set OpenFile = MediaFSO.OpenTextFile(TempFile,ForReading,False,TristateUseDefault) 'Open as text "c:\temp\random.tmp"
ListMediaFiles = OpenFile.Readall 'read the whole "c:\temp\random.tmp" into memory
OpenFile.Close 'close "c:\temp\random.tmp"
msgbox ListMediaFiles
Else
Msgbox "No Media Found in " & SourcePath
End if
MediaFSO.DeleteFile TempFile 'Delete "c:\temp\random.tmp"
End Function