-1

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
Rob_4142
  • 3
  • 3
  • FSO doesn't have an "overwrite" option so you should check if the file exists first, if so delete it, then move the other file. Or find another way to move files :) FSO methods and their capabilities are all documented here: [FileSystemObject](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/filesystemobject-object) ... so for instance, you can use a file exists method in conjunction with a delete file method, before you try moving a file to the same location. – topsail Jun 26 '22 at 18:10
  • hey Topsail, is there a way to just alter a file name? All I'm trying to do is trim a leading space from a file name, is there a different way to simply alter file names within a folder (trim leading space)? – Rob_4142 Jun 26 '22 at 18:40
  • If you are using move to rename a file, and the file name already exists, then you can maybe just skip the file? - it already has the correct name. In this case, you could still check if the file exists so you know to skip it. But another possibility is that you might have two files with the same name (except one with a leading space). In which case - I guess you have to decide which one to keep and which one to discard (or otherwise, do nothing - keep both and decide what to do later). – topsail Jun 26 '22 at 18:50

1 Answers1

0

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?

topsail
  • 2,186
  • 3
  • 17
  • 17