I have a pretty small VB script here that just trims a leading space off of file names for a given folder. The problem is when it runs it throws an error message if that same file name already exists. Is there a way to tell the script to overwrite a file if the file name already exists? Syntax below:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolderName = "\\CORP***\***Share\Common\RFI Validation Outputs"
Set objFolder = objFSO.GetFolder(strFolderName)
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFSO.MoveFile strFolderName + "\" + objFile.Name , strFolderName + "\" + LTrim(objFile.Name)
Next
My basic suggestions are along these lines (my VBS is rusty to check this for basic errors please!):
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolderName = "\\CORP***\***Share\Common\RFI Validation Outputs"
Set objFolder = objFSO.GetFolder(strFolderName)
Set colFiles = objFolder.Files
currName = ""
cleanName = ""
fullCleanPath = ""
For Each objFile in colFiles
currName = objFile.Name
cleanName = LTrim(objFile.Name)
fullCleanPath = strFolderName + "\" + LTrim(objFile.Name)
if currName <> cleanName and not objFSO.FileExists(fullCleanPath) then
objFSO.MoveFile objFile.Path , fullCleanPath
else
'//Houston, We have a problem
end if
Next
However, I would encourage adding some way to do a "dry run" - just a list of the proposed changes with some kind of output to a console or file, plus warnings of the files that already exist and possibly just to make sure it runs through with out errors (or otherwise - well, BACKUP first!)
As I explained, you will have to decide what to do if you have two files with the same name (except that one has that leading space or spaces) ... overwrite? skip? delete one? decide later?