Trying to code for the first time, but failed with file location - vbscript

The problem is that I tried to make a file on somebody else's desktop but I don't know what to write in the users.
I tried "C:\Users\" & strUser & "\desktop\Test"
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test"
error: Can't find the file location.

Use the special folder Shell function to return the path to the desktop.
set objShell = Wscript.CreateObject("Wscript.Shell")
strDesktopPath = objShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder strDesktopPath & "\Test"

I think your code will work if you simply add a \ at the end of your path:
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test\"

Related

backup only some files using vbs [duplicate]

This question already has answers here:
Copy a file from one folder to another using vbscripting
(5 answers)
Closed 2 years ago.
I want to make an automatic backup of my excel files using vbscript.
It works to copy the entire folder but I want to copy only the xlsx files.
Here is the code until now:
Dim objFSO, objFolder, evrFiles
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set evrFiles = objFolder.Files
For Each evrFile in evrFiles
If InStr(1, evrFile.Name, ".xlsx", vbBinaryCompare) > 0 Then
objFSO.CopyFile "C:\Users\Home\Desktop\vbs\" & evrFile.Name, "E:\test2"
End If
Next
WScript.Quit
It throws error on line 5 char 1 "Object required: " "
Any ideas?
LE: I have also tried:
Dim objFSO, objFolder
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\" & strUser & "\Desktop\vbs")
Set evrFiles = objFolder.Files
For Each evrFile in evrFiles
If InStr(1, evrFile.Name, ".xlsx", vbBinaryCompare) > 0 Then
objFSO.CopyFile "C:\Users\" & strUser & "\Desktop\vbs\" & evrFile.Name, "E:\test2"
End If
Next
WScript.Quit
But this one gives me "Permission denied on line 9 char 3"
This one works(to copy the entire folder) but I want only the excel files.
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
objFSO.CopyFolder"C:\Users\" & strUser & "\Desktop\vbs","E:\test2"
You dont need to iterrate each file, you could do something like:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
objFSO.CopyFile "C:\Users\" & strUser & "\Desktop\vbs\*.xlsx", "E:\test2\"
You may also want to set the overwrite flag when doing the copy, it there will be existing files already in the destination folder.
objFSO.CopyFile "C:\Users\" & strUser & "\Desktop\vbs\*.xlsx","E:\test2", True

Create multiple folders by merging two VBS codes

'I have 2 scripts but could not combine them
'create multiple folders script:
Dim objFSO, objFolder, strDirectory, i
strDirectory = "C:\Users\test\Desktop\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
i = 1 ''
While i < 150
Set objFolder = objFSO.CreateFolder(strDirectory & i)
i = i+1
''WScript.Quit ''
Wend
'desktop path script
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
I want the code to automatically find the desktop path and then create the folders, some one help me please ?
To get the desktop folder path string and create a sub directory you can do this:
Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Desktop")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(strPath + "\NewFolder") Then
objFso.CreateFolder strPath + "\NewFolder"

vbscript to delete files & folders of remote computer

First thing I am not an expert in writing VBScripts.
I have a requirement of deleting files & folders of remote systems with just 1 click. I was trying to build below VBScript but somehow it’s not working. I request any of your help to correct the same or with a new script that help me to fulfill the requirement. Any help in this regard is greatly appreciated, Thanks in Advance.
With the below:
C:\Test - is the directory from where I would like to delete the files & subfolders
C:\computerList.txt – is the text file contains all remote systems IP Address.
Const strPath = "C:\Test"
Set computerList = objfso.OpenTextFile ("C:\computerList.txt", 1)
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call Search (strPath)
WScript.Echo"Done."
Sub Search(str)
Do While Not computerList.AtEndOfStream
strComputer = computerList.ReadLine
Dim objFolder, objSubFolder, objFile
Set objFolder = objFSO.GetFolder("\\" & strComputer & "\" & str)
For Each objFile In objFolder.Files
If objFile.DateLastModified < (Now() - 0) Then
objFile.Delete(True)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Search(objSubFolder.Path)
' Files have been deleted, now see if
' the folder is empty.
If (objSubFolder.Files.Count = 0) Then
objSubFolder.Delete True
End If
Next
loop
End Sub
Regards,
Balaram Reddy
Your first problem is that you have the line order incorrect:
Set computerList = objfso.OpenTextFile ("C:\computerList.txt", 1)
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Should be
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set computerList = objfso.OpenTextFile ("C:\computerList.txt", 1)
You are using objfso before declaring it
When using a UNC path, you will need to use the folder's remote share name. If you have admin privileges on the remote pc use:
Const strPath = "c$\Test"

VBscript , create directory in FTP

I would like to create a directory in FTP, the name of the directory must be as my computer name,
here is my code,
Dim FoldertoCreate, filesys, newfolder, Ob
Set Ob = Wscript.CreateObject("Wscript.Network" )
FoldertoCreate = "ftp://user:password#ftpserver/url-path/" & ob.ComputerName
Set filesys = CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(FoldertoCreate) Then
Set newfolder = filesys.CreateFolder(FoldertoCreate)
End If
This code doesn't work however when i replace ftp://user:password#ftpserver/url-path with any local directory like D:/ , it works :S
how to make it work for my ftp too
The FileSystemObject does not support FTP. The Shell Automation Object does, but it doesn't seem to like the NewFolder method. That leaves us with automating the FTP.exe command using an unattended FTP session. It might look something like this.
strUser = "myusername"
strPass = "mypassword"
strHost = "ftp.myhost.com"
Const ForWriting = 2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("session.txt", ForWriting, vbTrue)
With objFile
.WriteLine "OPEN " & strHost
.WriteLine "USER " & strUser
.WriteLine strPass
.WriteLine "mkdir sometestdirectory"
.Close
End With
strFTP = "%systemroot%\System32\ftp.exe -s:session.txt"
Set WshShell = CreateObject("WScript.Shell")
strFTP = WshShell.ExpandEnvironmentStrings(strFTP)
WshShell.Run strFTP,, vbTrue
objFso.DeleteFile "session.txt", vbTrue

VBScript current directory + sub directory?

I am trying to get the path of a file that is within a sub-directory of the current directory in VBScript. The following does not seem to work?
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
FileToCopy = currentDirectory & "\test\user.js"
Here is the entire code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "unproxy\user.js")
''# get AppdataPath
Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
AppdataPath = WshSysEnv("APPDATA")
FoxProfilePath = AppdataPath & "\Mozilla\Firefox\Profiles\"
'"# is firefox and user.js present?
if oFSO.FolderExists(FoxProfilePath) AND oFSO.FileExists(FileToCopy) Then
''# copy user.js in all profilefolders to get around those random profile names =)
For Each ProfileFolder In oFSO.GetFolder(FoxProfilePath).Subfolders
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
Next
End If
'"# clean up
Set oFSO = Nothing
Set WshShell = Nothing
Set WshSysEnv = Nothing
I recommend using FileSystemObject when dealing with file paths:
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "test\user.js")
Edit: The problem is in this line of your script:
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
Since FileToCopy contains a full file name, when you concatenate it with ProfileFolder you get an invalid file name, like this:
C:\Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\mlreq6kv.default\D:\unproxy\user.js
Change this line to the one below, and your script should work fine. (Note: the trailing path separator at the end of ProfileFolder is required to indicate that the profile folder, e.g. mlreq6kv.default, is indeed a folder and not a file.)
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\", True
You can get the current directory with :
Set WSHShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory

Resources