Creating folder in network share fails with "bad path" error - vbscript

I have a network share already setup. I am trying to create a VBScript that will create a folder in the share called computername, which is the PC's name. The script will run locally on the PC, access the share and create the folder in the share.
My error is "bad path". I'm guessing I can't just state the network share path?
My script is below:
Dim objShell
Set oWS = WScript.CreateObject("WScript.Shell")
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
computername = oWS.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
If NOT (objFSO.FolderExists("\\ServerPath\Share$" + computername)) Then
objFSO.CreateFolder("\\ServerPath\Share$" + computername)
End If

You're lacking a backslash between the name of the share and the name of the folder you want to create. Also, I'd recommend using a variable for the path, so you don't have to construct it multiple times.
Change this:
If NOT (objFSO.FolderExists("\\ServerPath\Share$" + computername)) Then
objFSO.CreateFolder("\\ServerPath\Share$" + computername)
End If
into this:
path = "\\ServerPath\Share$\" & computername
If NOT objFSO.FolderExists(path) Then
objFSO.CreateFolder(path)
End If

Related

vbscript create text file in server directory

I have vbscript application that run in a server and I tried run that application in my local computer. The problem is I want to create a text file in server directory but I always end up with an error something like "disk is not ready". When I check, it is because in my local computer, there is only 1 partition Drive C: and for some reason the application try to make the textfile in my local directory. And if change the path to drive C: it works and the file is existed in my local directory. So what am I doing wrong? I want the text file is created in server directory.
Here's part of my code :
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("D:\tesfolder\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = Nothing
Set fso = Nothing
please help me
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("\\127.0.0.1\C$\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
The above needs Admin because it uses the Admin only share (present on all computers) c$ and writes to the root of C: drive.
The format is
\\ServerName(orIP)\ShareName\Folder\File.ext
An admin would
\\Computer\D$\tesfolder\SomeText.txt
For a user substitute their share name for D$.

Move files from a special folder to another special folder

I'm writing a script to move an Outlook signature into %APPDATA%\Microsoft\Signatures\ and it's just not going as planned.
I want to instruct the user to put the "signature" folder on their desktop, and then run a script that will move all of the items in the signature folder to the AppData folder.
I've been reading, and it looks like it's not as simple as just putting %userprofile%\Desktop\signatures\* into VBScript or PowerShell code. I don't understand why Windows Explorer knows what to do with that path, but PowerShell/VBScript doesn't know what a special folder is, but whatever the case, my code just isn't working.
Here's an example of what I'm trying to do with VBScript:
Dim desktop
Dim appdata
desktop = object.SpecialFolders("Desktop")
appdata = object.SpecialFolders("APPDATA")
With CreateObject("Scripting.FileSystemObject")
.MoveFile desktop\MET_Signature_Template\*, appdata\Microsoft\Signatures\test\
End With
I get a syntax error, but no direction on why it's wrong. I've tried a few different things I've found on here to no avail.
When in doubt, read the documentation.
Syntax
object.SpecialFolders(objWshSpecialFolders)
Arguments
object
WshShell object.
Change this:
desktop = object.SpecialFolders("Desktop")
appdata = object.SpecialFolders("APPDATA")
into this:
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
appdata = sh.SpecialFolders("APPDATA")
Build source and destination paths using the BuildPath method:
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "MET_Signature_Template")
destination = fso.BuildPath(appdata, "Microsoft\Signatures\test")
fso.MoveFile source & "\*", destination & "\"
In PowerShell you'd do it like this:
$source = "$env:APPDATA\MET_Signature_Template"
$destination = Join-Path [Environment]::GetFolderPath('Desktop') 'Microsoft\Signatures\test'
Copy-Item "$source\*" $destination

.GetAbsolutePathName(".") in vbscript does not get correct path when run by task scheduler

I am trying to run a vb script using task scheduler.
I am using the .GetAbsolutePathName(".") to get the full path of the script.
When I run the script manually, it was able to get the correct path. But when run as scheduled task, it outputs "C:\Windows\System32\ as the path.
I am using the path name to get the full path of an Excel file that I would like to run. The excel file is saved within the same path as the script.
Below is my code:
Set fso = CreateObject("Scripting.FileSystemObject")
scurDir = fso.GetAbsolutePathName(".")
set fso = nothing
Set myxlApplication = CreateObject("Excel.Application")
wscript.echo scurDir & "\OOO Automation Tool.xlsm"
Set myWorkBook = myxlApplication.Workbooks.Open( scurDir & "\OOO Automation Tool.xlsm" )
myxlApplication.Visible = False
myxlApplication.Wait(Now + TimeValue("0:00:10"))
'Run routine
myWorkBook.Application.Run "MOutofOffice.pDetectIdleTime"
'Close application
myxlApplication.Quit
'Release objects
set myxlApplication = nothing
set myWorkBook = nothing
Can you help me get the correct path?
You can try with
With WScript.CreateObject("Scripting.FileSystemObject")
WScript.Echo .GetFile(WScript.ScriptFullName).ParentFolder.Path
End With
You need the path where the script is stored, not the current active directory, what you retrieve with the "." reference.

VBS WScript.Run fails after passing Exists test

In a couple of place in my code I check if the file exists (it does) then I try to Run the file as above, or get the DateLastModified, and get errors about file not found or invalid path. How can the script NOT see a file after confirming it exists?
I'm working up a .vbs script that tries to run an Access .mdb file. The WScript.Run command seems to choke on the filename, but putting a MsgBox() before that call to display the path allows Run to work properly. I don't want to display a popup.
Error is:
The directory name is invalid.
How is this possible and how can I get around it?
Here is code:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
SET ws = WScript.CreateObject("WScript.Shell")
path = Chr(34) & LocalPath & AccessFileName & Chr(34)
if (fso.FileExists(LocalPath & AccessFileName)) THEN
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run path 'This line errors
End If
Try to open your file with shell .InvokeVerb method:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
If CreateObject("Scripting.FileSystemObject").FileExists(LocalPath & AccessFileName) Then
CreateObject("Shell.Application").Namespace(LocalPath).ParseName(AccessFileName).InvokeVerb
End If
UPD: Both ActiveX WScript.Shell and Shell.Application uses native windows shell to perform a file execution.The first one launches new process via WSH core located in wscript.exe, cscript.exe, wshom.ocx, jscript.dll, vbscript.dll, ets, .Run and .Exec methods of WsShell object provides wide control on the launched process, and second one located in Shell32.dll, uses .InvokeVerb method of IShellDispatch object, called without name, runs default verb equals to the windows explorer "open" command.In case of any issues connected to WSH, explorer might still works without any proplems. If it does, that is just a work-around, I can't say what's wrong definetely without close look.
Hello the following code worked for me.
Basically this code gets a folder object and loops through all files in a folder and checks if its the one that you named. This it runs the application.
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = Wscript.CreateObject("Wscript.Shell")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set myFolder = fso.GetFolder(LocalPath)
For each myFile in myFolder.Files
If myFile.Name = AccessFileName Then
'Wscript.Echo myFile.Name
ws.Run myFolder.Path & "\" & myFile.Name
End If
Next
You can give this a shot. You probably do not need the quotes around the path, but I included it as a comment if you want to give it a shot. You just put quotes twice if you need to include a quote character in a string:
Set fso = CreateObject("Scripting.FileSystemObject")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set ws = WScript.CreateObject("WScript.Shell")
' path = """" & LocalPath & AccessFileName & """" <-- probably unnecessary
path = LocalPath & AccessFileName
If (fso.FileExists(path)) Then
Set file = fso.GetFile(path)
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run file.Path 'This line errors
End If
This does not make any sense. Having a MsgBox line is altering the behavior of the program!!!
I feel it is probably some weird invisible character somewhere which is getting activated when you comment the line.
Try retyping the If block without the MsgBox in between.

how to access a network folder using vbscript

I have a folder which is on a network like \\server\contents\tasks and I want to access this folder.
I am getting a "path not found" exception. What am I doing wrong here:
Dim FolderPath
FolderPath = "\\server\contents\tasks"
set FSO = CreateObject("Scripting.FileSyatemObject")
FSO.GetFolder(FolderPath)
Thanks
Edit: I found this post which answers the same thing I am trying to achieve, but the issue is I am getting an error the network share is no longer available. What I have is a local folder as a shared folder and mapped as \\servername\contents\tasks but it gives me the above error.
Edit: I was pointing at the wrong folder.
Now I have another issue trying to open a text file in the network folder. It is able to create a folder at the network path but throwing error while reading a text file in the network folder. Is there something else that needs to be done?
Set FSO = CreateObject("Scripting.FileSystemObject")
strOutputPath = strOutput1 --this is a network path
Set txsOutput = FSO.CreateTextFile(strOutputPath)
Set f = FSO.OpenTextFile(strInput1)
Open the network folder using explorer.exe and pass the location of the folder as a parameter (in this example it's sPath storing the folder path)
Example:
sPath = "\\somedrive.somecompany.ie\software"
Set oShell = CreateObject("WScript.Shell")
oShell.Run "explorer /n," & sPath, 1, False
Terms and conditions: username and password privileges already setup for acccess to the network folder.

Resources