vbscript with bginfo using objfso - vbscript

May I ask how I can make the username as a variable (ex. %userprofile%):
Set objFile = objFSO.OpenTextFile("c:\users\username\AppData\Roaming\Samplefolder\sampletext.txt", 1)
I suppose it's because it's a string with the " ".
Thanks in advance!

You need to use Shell object to fetch the userprofile. Sample script would be
dim objShell, strPath, objFSO, objFile
Set objShell = WScript.CreateObject("WScript.Shell")
strPath = objShell.ExpandEnvironmentStrings("%UserProfile%")
strPath = objFSO.BuildPath(strPath, "AppData\Roaming\Samplefolder\sampletext.txt")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strPath,1)

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 copy files based on beginning letters

I am trying to get this script to copy all files starting with "XX". Currently it only copies one file.
Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\source")
strDestFolder = "C:\destination\"
For Each objFile In colFiles.Files
If Left(objFile.Name, 2) = "XX" Then
If objNewestFile = "" Then
Set objNewestFile = objFile
Else
If objNewestFile.DateLastModified < objFile.DateLastModified Then
Set objNewestFile = objFile
End If
End If
End If
Next
If Not objNewestFile Is Nothing Then
objFSO.CopyFile objNewestFile.Path,strDestFolder,True
End If
WScript.Echo "Copied."
You can use wildcards * and ? in [source] argument of FSO .CopyFile method.
So the code may look like:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\source\XX*.*", "C:\destination\", True
WScript.Echo "Copied."

list all files in a folder and sub folder without extention

I've come across the following script that I'd really like to use but I would like it not to have the .extention at the end
Dim fso
Dim ObjOutFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjOutFile = fso.CreateTextFile("C:\Users\User\Movies.csv")
ObjOutFile.WriteLine("Type,File Name,Size")
GetFiles("E:\")
ObjOutFile.Close
Function GetFiles(FolderName)
On Error Resume Next
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile
Set ObjFolder = fso.GetFolder(FolderName)
Set ObjFiles = objfolder.Files
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine("File," & ObjFile.Name & "," & objFile.Size & "," & objFile.Type)
Next
Set ObjSubFolders = ObjFolder.SubFolders
For Each ObjFolder In ObjSubFolders
ObjOutFile.WriteLine("Folder," & ObjFolder.Name)
GetFiles(ObjFolder.Path)
Next
End Function
I'm rubbish at this but I would really apperciate the help
Use the .GetBaseName() method of the FileSystemObject. As in:
>> WScript.Echo goFS.GetBaseName("c:\dir\name.ext")
>>
name

The process cannot access the file because it is being used by another process. Code: 80070020, VBScript

When I run my vbscript, it says(In Windows Script Host):
C:\Users\admin\Desktop\Test.vbs
Line: 34
Char:1
Error: The process cannot access the file because it is being used by another process.
Code: 80070020
Source: (null)
How would I be able to fix this? Also here's the script...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFSO, objFolder, objShell, objFile
Dim strDirectory, strFile
strDirectory = "c:\Folder"
strFile = "\Hidden.bat"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If
set objFolder = nothing
set objFile = nothing
Const fsoForAppend = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\Folder\Hidden.bat", fsoForAppend)
objTextStream.WriteLine "attrib ""Folder"" +s +h"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Folder\Hidden.bat"""
Set objShell = Nothing
Without creating any batch file to hide your folder :
Option Explicit
Dim objFSO,objFolder,strDirectory,Command,Result,objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDirectory = "C:\Folder"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
set objFolder = nothing
Command = "Cmd /c Attrib +s +h "& DblQuote(strDirectory) &""
Set objShell = CreateObject("WScript.Shell")
Result = objShell.Run(Command,0,True)
Set objShell = Nothing
'****************************************************************
Function DblQuote(str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
Closes an open TextStream file.
object.Close
From Help.
You need to close it after writing to it before using it.

Resources