[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with function in VBS

Posted on 2013-05-21
6
Medium Priority
?
322 Views
Last Modified: 2013-05-22
The other problem I am having is that the last two functions don't seem to work together.  The last function works (the " " is replaced with "_".  The previous function where the "#" is to be replaced with "_" seems to be skipped over.  

I have attached the file.
0
Comment
Question by:tommy777
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 39185426
Nothing is attached...

~bp
0
 

Author Comment

by:tommy777
ID: 39185430
attaching file
replace-b.vbs
0
 

Author Comment

by:tommy777
ID: 39185453
Actually, I think I corrected it:

Set objFSO = CreateObject("Scripting.FileSystemObject")
nCount = 0
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
set rootFolder = objFSO.GetFolder(currentDirectory)
renameFiles rootfolder
enumFolders rootFolder
wscript.echo nCount & " Files have been renamed."
function enumFolders(folder)
   for each sFolder in folder.subfolders
      renameFiles sFolder
      enumFolders sFolder
   next
end function
function renameFiles(nFolder)
   for each oFile in nFolder.files  
 if instr(oFile.name,"#") > 0 then
         newFileName = replace(oFile.name,"#","_")  
         newFilePath = left(oFile.path,len(oFile.path) - len(oFile.name)) & newFileName
         objFSO.MoveFile oFile.path, newFilePath
end if
next
for each pFile in nFolder.files  
if instr(pFile.name," ") > 0 then
         newFileName = replace(pFile.name," ","_")  
         newFilePath = left(pFile.path,len(pFile.path) - len(pFile.name)) & newFileName
         objFSO.MoveFile pFile.path, newFilePath
nCount = nCount+1      
end if
next
end function
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 39185562
Which I think could be simplified a bit to:

Set objFSO = CreateObject("Scripting.FileSystemObject")
currentDirectory = Left(WScript.ScriptFullName, (Len(WScript.ScriptFullName)) - (Len(WScript.ScriptName)))
Set rootFolder   = objFSO.GetFolder(currentDirectory)

nCount= 0
renameFiles rootfolder
enumFolders rootFolder
Wscript.Echo nCount & " Files have been renamed."

Function enumFolders(folder)
    renameFiles sFolder

    For Each sFolder in folder.subfolders
        enumFolders sFolder
    Next

End Function

Function renameFiles(nFolder)

    For Each oFile in nFolder.files

        If InStr(oFile.name, "#") > 0 Or InStr(oFile.name, " ") > 0 Then
            newFileName = Replace(Replace(oFile.name, "#", "_"), " ", "_")
            objFSO.MoveFile pFile.path, oFile.ParentFolder & "\" & newFileName
            nCount = nCount + 1
        End If

    Next

End Function

Open in new window

~bp
0
 

Author Comment

by:tommy777
ID: 39186147
Error message:

Line 21
Char 5

Object Required 'nFolder'
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39186334
Okay, a bit more cleaning u, this should get you there...

Set objFSO = CreateObject("Scripting.FileSystemObject")
strCurrentDirectory = Left(WScript.ScriptFullName, (Len(WScript.ScriptFullName)) - (Len(WScript.ScriptName)))
Set objRootFolder = objFSO.GetFolder(strCurrentDirectory)

intCount = 0
enumFolders objRootFolder
Wscript.Echo intCount & " Files have been renamed."

Function enumFolders(objFolder)
    renameFiles objFolder

    For Each objSubFolder in objFolder.Subfolders
        enumFolders objSubFolder
    Next

End Function

Function renameFiles(objFolder)

    For Each objFile in objFolder.Files

        If InStr(objFile.Name, "#") > 0 Or InStr(objFile.Name, " ") > 0 Then
            newFileName = Replace(Replace(objFile.Name, "#", "_"), " ", "_")
            objFSO.MoveFile objFile.Path, objFile.ParentFolder & "\" & newFileName
            intCount = intCount + 1
        End If

    Next

End Function

Open in new window

~bp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question