Question

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months. I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same time overwrite that folder and file if they already exist Folder and subfolders to be created (Avaya) C:\Users\My Username\AppData\Roaming\Avaya\ Avaya\one-X Agent\2.5\ File from (Myfile.txt) C:\Myfile.txt to C:\Users\My Username\AppData\Roaming\Avaya\one-X Agent\2.5\

I get “Path not found” error, but If I leave the path till (Avaya) it creates Avaya Folder but not it’s subfolders C:\Users\My Username\AppData\Roaming\Avaya\

Here’s what I have and thank you in advance

Dim fso, vFolder
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
usrName = objWShell.expandEnvironmentStrings("%USERNAME%")
Set fso = CreateObject("Scripting.FileSystemObject")
Set vFolder = fso.CreateFolder("C:\Users\" & usrName & "\AppData\Roaming\Avaya\one-X Agent\2.5\")
CreateFolderDemo = vFolder.Path
Was it helpful?

Solution 2

Some errors:

  1. You declare fso, but you use objFso
  2. You use %USERNAME% but you should consider %APPDATA% instead
  3. You should use OPTION EXPLICIT to detect typos and undefined variables
  4. You should make your code easier to read by dimming one variable at a time
  5. CreateFolder doesn't create the entire tree, so you need to use FolderExists

For example:

Option Explicit
Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim appData
appData = objWShell.expandEnvironmentStrings("%APPDATA%")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(appData + "\Avaya") Then
  objFso.CreateFolder appData + "\Avaya"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent") Then
  objFso.CreateFolder appData + "\Avaya\one-X Agent"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent\2.5") Then
  objFso.CreateFolder appData + "\Avaya\one-X Agent\2.5"
End If

Lastly, it's not clear why your solution needs to be in VBScript. It appears that your requirements are creating folders and copying files, which means, batch files would probably be a far more simpler.

OTHER TIPS

The problem is that CreateFolder does not create intermediate folders. The FSO doesn't have a method that does that. It might be easier to use mkdir like this:

Option Explicit

Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.Run("%COMSPEC% /c mkdir ""%APPDATA%\Avaya\one-X Agent\2.5""",0,true)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top