force delete and rename text file in VB6 - vb6

how to force fully delete , rename and replace text file/log file in vb6. I am performing some actions on a log file and replacing original log file with a new one in the end but the application is still using the original log.

Here is the delete
Sub DeleteAFile(filePath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(filePath)
End Sub
Just mess around with the FileSystemObject class, it has methods to delete, rename, create, etc.

Related

Error Saving File in Document Folder of Library

I am writing an HTA file with VBScript as the scripting language. I have a function where the user is prompted to choose the folder in which they would like to save a document. I didn't write this code myself, but instead borrowed it from here.
Function SelectFolder(myStartFolder)
Dim objFolder, objItem, objShell
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Select Folder to Save New File", 0, myStartFolder)
If IsObject(objFolder) Then SelectFolder = objFolder.Self.Path
End Function
I call this function in another one in order when I make the file and prompt the user to choose where to save it:
Sub Example()
Dim destPath, destFile, objWorkbook
destPath = SelectFolder(libPath)
destPath = destPath & "\Sample Export"
Set destFile = CreateObject("Excel.Application")
Set objWorkbook = destFile.Workbooks.Add()
objWorkbook.SaveAs(destPath)
(code to edit excel file)
End Sub
Example() works fine except when someone chooses to save their document in one of the four default libraries in Windows (Documents, Music, Pictures, Videos). In that case, I receive an error saying "The file could not be accessed" and indicating the error is at the line that says "objWorkbook.SaveAs(destPath)". The error box then gives me the following suggestions:
Make sure the specified folder exists
Make sure the folder that contains the file is not read-only
Make sure the file name does not contain any of the following characters: < > ? { } : | or *
Make sure the file/path name doesn't contain more than 218 characters.
The error occurs when I open the HTA file and click a button calling Example(), which then opens a dialog box to ask the user to choose the file location. When Documents, Music, Pictures, or Videos is chosen, a "Script Error" box pops up with the error message listed above. I am not familiar enough with VBScript to know what the problem is exactly. I would appreciate any suggestions as to what to do.
I don't know exactly what the solution to the above issue was. However, I noticed that the path I pass to SelectFolder had a typo in it. libPath was supposed to be to the %userprofile% from the environment variable. However, I had pluralized "userprofiles" so that it was %userprofiles% instead. This took me to somewhere that looked right but wasn't. I fixed the typo and now BrowseForFolder takes me to C:/Users/*username* like it's supposed to. It turns out that I was being sent to the Desktop location because of the fourth argument in BrowseForFolder, instead of the user profile like I wanted.

Transposing a text file row into columns using VBScripting

I currently have a text file that looks like this
I need to be able to create a VBScript file that will use FileSystemObject to change the rows into columns and output the data into an html table like below
I am having a difficult time finding a method for transposing. I am new to VBscripting, I assume that I can still use the CreateTextFile method for HTML?
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("C:\Users\User.mdm\Desktop\PracticeTest\table.html", True)
MyFile.WriteLine(?)
MyFile.Close

Move files to folders based on first X characters of filename (vbs)

Hello my coding friends.
Sorry to ask this, but I thought it might be quicker to ask if someone has a script like this lying around.
I have about 2000+ files of audio mp3 files logged for a radio station I'm at, and I'd like to put them in to folders according to their recorded log date.
(Yes, I've now fixed the recording to do this correctly from now on, but this is in referent to what I've been doing with it: https://stephenmonro.wordpress.com/2015/05/22/setting-up-an-audio-logger/ )
The files I have are like this: (YYYYMMDD_HH00)
logs\20150424_0300.mp3
logs\20150424_0400.mp3
logs\20150424_0500.mp3
etc.
What I'd like is something like this:
\logs\8 digit date\filename with the same 8 digit date.mp3
Actual
\logs\20150424\20150424_0300.mp3
\logs\20150424\20150424_0400.mp3
\logs\20150424\20150424_0500.mp3
etc.
This is my pseudo code, I've made, but as I'm a little pressed for time and don't have hours to mess around guessing, I just wondered if anyone knew how to do it quickly.
A .VBS file is my prefered language.
Do
Read a filenames first 8 characters {left(8, filename)} (the date)
If not exist, create a folder called that first 8 characters
Move that file into the folder name
Loop (until all files are moved to the right locations)
Your pseudocode looks spot on to me. Assuming every file in your logs folder is consistently named, here's how it could be accomplished using the FileSystemObject library:
Const LOGS_FOLDER = "c:\logs"
Dim objFSO, objFile, strDate, strSub
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(LOGS_FOLDER).Files
strDate = Left(objFile.Name, 8)
strSub = objFSO.BuildPath(LOGS_FOLDER, strDate)
' Create the folder if it doesn't already exist...
If Not objFSO.FolderExists(strSub) Then objFSO.CreateFolder strSub
' Move the file into its proper folder. Use "\" to indicate dest is folder...
objFile.Move strSub & "\"
Next

VB script will not traverse every file in a given folder

I am new to VBscript and am looking to write a simple script that changes a couple cells in a few thousand csv files in a given folder location. I have a good start, and it all seems to be working, except for the fact that when I run the script (from a .bat file that just calls the script) it only changes and moves 3-8 files at a time. The number of files it changes is random, its not like it always changes 5 files or something. I am not sure what is wrong in the code as to why it will not edit and move every single file and only does a couple at a time, here is what I have so far, thanks in advance for any help:
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set colFiles = ObjFSO.GetFolder("C:\Users\xxx\BadCSVs").Files
Set xl = CreateObject("Excel.Application")
For Each objFile in colFiles
If LCase(objFSO.GetExtensionName(objFile)) = "csv" Then
Set wb = xl.Workbooks.Open(objFile)
Set sht = xl.ActiveSheet
If(sht.Cells(1,11) <> "") Then
sht.Cells(1,8) = sht.Cells(1,8) & sht.Cells(1,9)
sht.Cells(1,9) = sht.Cells(1,10)
sht.Cells(1,10) = sht.Cells(1,11)
sht.Cells(1,11) = Null
wb.Save
wb.Close True
Else
'if file is opened up and has only 10 columns of data, then it makes no changes, then closes it.
wb.Close
End If
End If
Next
xl.Quit
Your EVIL global
On Error Resume Next
hides errors. Disable/Remove it and test again.
Your
wb.Close SaveChanges=True
passes the boolean result of comparing SaveChanges (undefined/empty) vs. the boolean literal True. Perhaps you copied VBA code
wb.Close SaveChanges:=True
(mark the colon) that is not legal in VBScript?
And
Set xl = CreateObject("Excel.Application")
should be paired with an
xl.Quit
If you invoke Excel in the loop, terminate it there. I would try to start/quit Excel out of the loop, but you should test that approach carefully.

Error in vb script for .xls location finding

I'm facing a problem while writing the vb script for opening a .xls file that is given below..
Dim XLAPP
Set XLAPP = createobject("excel.application")
XLAPP.Visible =true
XLAPP.Workbook.open"d:\book1.xls"
When I run this script the pop window display an error like this:
The test run cannot continue due to an unrecoverable error.
'd:\book1.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.
Line (1): "Dim XLAPP".
When I write the script XLAPP.Workbook.Add then it adds an excel file but the above given script is not opening the excel file.
The .Open method is within the Workbooks object collection (plural), not the Workbook object (singular)¹.
XLAPP.Workbooks.open "d:\book1.xls"
fwiw, you may have used the Workbooks collection without thinking about it in XLAPP.Workbooks.Add. The .Add method is not a member of the singular Workbook object and I could not get that to run; Run-time error 424: Object required.
¹ Kudos to Matt Donnan and his comment above for supplying the correct response.

Resources