Solved

Help with function in VBS

Posted on 2013-05-21
6
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 55

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

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 55

Accepted Solution

by:
Bill Prew earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

688 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