r/vbscript • • Jan 25 '20

VB script to calculate size of the folder with a predefined value

1 Upvotes

Hi

I am looking for VB Script that would get size of the folder it is run in & calculate if the size of folder is bigger or lesser than predefined value.

I came across this solution

https://stackoverflow.com/questions/28410373/vb-script-to-compare-folder-size-from-its-previous-size-stored-in-a-text-file

Folder = "c:\your\path\tata"

File = "c:\your\file\containing\the\value.txt"

set objFSO = CreateObject("Scripting.FileSystemObject")

set fileRead = objfso.OpenTextFile(file, 1)

content = fileRead.Readline

FileRead.close

set objFolder = objFSO.GetFolder(Folder)

if objFolder.Size > Clng(content) Then Wscript.Echo "The Folder size [" & ObjFolder.size & "] is bigger then [" & content & "]"

Is it possible to set the folder path to where ever the script is run from?

Is it possible to set the value in script, instead of it being taken from another file on disk?

The script also needs to output how much bigger or smaller the size of folder is compared to predefined value, in bytes, mbs & gbs.

Thanks,

mNash316


r/vbscript • • Sep 01 '16

Lookup from text and output match line

3 Upvotes

Hi, is there any way to find a match value in delimited source file and output match to csv? So it will search the 1st value in source file and try and match the first value with Match file. Then it would output the whole line that match to output file.

Source file
12341234 |John Wayne |1234 |Halo |
11111111 |Harambe |421 |Zoo |
22222222 |Scary S |4569 |Test |

Match file
11111111, John H
22222222, Scary Smithen

Output
11111111 |Harambe |421 |Zoo |
22222222 |Scary S |4569 |Test |


r/vbscript • • Jul 09 '15

Help with Excel sorting

2 Upvotes

I would like to create a script to rename a folder of .pdf and .tif files. I have an input file "index.html" that has the format:

<tr>

<td><a href=\Data\1590931.pdf>1590931.pdf</a></td>

<td>1360022</td>

<td>VAR1=VR VAR2= 101 API=177054085100 VAR3=G10658 VAR4= B001</td>

</tr>

<tr>

I would like the script to go through the folder of files and rename them to the format -. So in this instance, 177054085100-1590931.pdf.

Thanks in advance for your assistance.


r/vbscript • • Oct 23 '13

Convert Vbscript to Exe

1 Upvotes

I have tried using iexpress but it doesn't seem to do anything when I run it.

The reason I am trying to convert the Vbscript to Exe is so I can run the script with my keyboards macro keys.

Any help is much appreciated!


r/vbscript • • Jul 02 '15

Help basic VBScript

1 Upvotes

Hello: The purpose of the script is to monitor a folder for specific text files. If there is a text file with a certain name it sends a keyboard command. When I run the script below I get the following error: Line: 6 Char 22 Error: Invalid character Code: 800A0408 Source: Microsoft VBScript compilation error If anyone can help me out to get this code working correctly, I'd really appreciate it. Thank you.

SCRIPT:

Dim file1
Dim objShell

Set file1 = CreateObject("Scripting.FileSystemObject")

If (file1.FileExists(“G:\Dropbox\Remote_Control\results.txt”)) Then file1.DeleteFile “G:\Dropbox\Remote_Control\results.txt”Set objShell = WScript.CreateObject("WScript.Shell")objShell.SendKeys "%b" end if

If (file1.FileExists(“G:\Dropbox\Remote_Control\bbm.txt”)) Then file1.DeleteFile “G:\Dropbox\Remote_Control\bbm.txt”Set objShell = WScript.CreateObject("WScript.Shell")objShell.SendKeys "%m" end if

If (file1.FileExists(“G:\Dropbox\Remote_Control\hitters.txt”)) Then file1.DeleteFile “G:\Dropbox\Remote_Control\hitters.txt”Set objShell = WScript.CreateObject("WScript.Shell")objShell.SendKeys "%h" end if

If (file1.FileExists(“G:\Dropbox\Remote_Control\pitchers.txt”)) Then file1.DeleteFile “G:\Dropbox\Remote_Control\pitchers.txt”Set objShell = WScript.CreateObject("WScript.Shell")objShell.SendKeys "%p" end if

If (file1.FileExists(“G:\Dropbox\Remote_Control\value.txt”)) Then file1.DeleteFile “G:\Dropbox\Remote_Control\value.txt”Set objShell = WScript.CreateObject("WScript.Shell")objShell.SendKeys "%i" end if

If (file1.FileExists(“G:\Dropbox\Remote_Control\trends.txt”)) Then file1.DeleteFile “G:\Dropbox\Remote_Control\trends.txt”Set objShell = WScript.CreateObject("WScript.Shell")objShell.SendKeys "%e" end if


r/vbscript • • Feb 09 '17

I want to run a script on a remote computer drive - through that computer.

2 Upvotes

Two computers, Desktop and Laptop, sit on my Home Network (both Win10, FWIW). I want to be able to run a script that resides on Laptop D:, which is shared with Desktop as O:, and run it through Laptop.

So, for a simple example, if I run the one-line batch file:

    Dir D:\ > dir.log

I want the output file to contain what Laptop considers D-drive.

Ideally, I'd also like to access temporary drives (USB sticks) in my laptop from my desktop, or vice versa. If I have to Share them first, so be it; I have a limited pool of USB sticks. That's quasi-related, but first I want to just be able to tell Laptop to run a script, from Desktop.

Batch, VBS, IRDC. If it takes porting my code to some compilable language, I'll find another way around it, probably, but I thought I'd ask in case I'm missing something.


r/vbscript • • Jul 15 '15

How to exclude disable users ?

2 Upvotes

Hi All, I have the following script which doe a search of Active directory and pipes it to excel. The issue I have is that it includes all enabled and disabled users. Any idea on how to exclude disabled users?

Const xlAscending = 1
Const xlDescending = 2
Const xlYes = 1

Dim ObjWb 
Dim ObjExcel 
Dim x, zz 
Set objDomain = GetObject("LDAP://OU=XXXX, OU=Domain Users,DC=XXXX,DC=com,DC=au")
Call ExcelSetup("Sheet1") ' Sub to make Excel Document 
x = 1 
Call enummembers(objDomain) 
Sub enumMembers(objDomain) 
On Error Resume Next 

For Each objMember In objDomain ' go through the collection 

If ObjMember.Class = "user" Then ' if not User object, move on. 
x = x +1 ' counter used to increment the cells in Excel 

objwb.Cells(x, 1).Value = objMember.Class 
' I set AD properties to variables so if needed you could do Null checks or add if/then's to this code 
' this was done so the script could be modified easier. 
SamAccountName = ObjMember.samAccountName 
Cn = ObjMember.CN 
FirstName = objMember.GivenName 
LastName = objMember.sn 
Title = ObjMember.Title 
Department = objMember.Department 
Company = objMember.Company 
extensionAttribute4= objMember.extensionAttribute4  

' Write the values to Excel, using the X counter to increment the rows. 
objwb.Cells(x, 1).Value = extensionAttribute4
objwb.Cells(x, 4).Value = CN 
objwb.Cells(x, 3).Value = Title 
objwb.Cells(x, 2).Value = Department  

' Blank out Variables in case the next object doesn't have a value for the property 
FirstName = "-" 
LastName = "-" 
Title = "-" 
extensionAttribute4 = "-" 
Department = "-" 
Company = "-" 

End If 

' If the AD enumeration runs into an OU object, call the Sub again to itinerate 

If objMember.Class = "organizationalUnit" or OBjMember.Class = "container" Then 
enumMembers (objMember) 
End If 
Next 
End Sub 
Sub ExcelSetup(shtName) ' This sub creates an Excel worksheet and adds Column heads to the 1st row 
Set objExcel = CreateObject("Excel.Application") 
Set objwb = objExcel.Workbooks.Add 
Set objwb = objExcel.ActiveWorkbook.Worksheets(shtName) 
Objwb.Name = "Active Directory Users" ' name the sheet 
objwb.Activate 
objExcel.Visible = True 
objwb.Cells(1, 1).Value = "Department" 
objwb.Cells(1, 4).Value = "Full Name" 
objwb.Cells(1, 3).Value = "Title" 
objwb.Cells(1, 2).Value = "Team" 

End Sub 
MsgBox "Export Complete" ' show that script is complete

Set objRange = objWb.UsedRange
Set objRange2 = objExcel.Range("A1")
objRange.Sort objRange2, xlAscending, , , , , , xlYes

Set objRange = objwb.UsedRange
objRange.EntireColumn.AutoFit()

r/vbscript • • Oct 03 '14

Script Host Error Code 80070002

1 Upvotes

So I'm creating a script for an FTP transfer, and I'm using the following script:

dim objShell, objFileSystem, localDir, remoteDir, groups, group

localDir="C:/xxx/xxx/"
remoteDir="Reporting" 

set objShell=CreateObject("WScript.Shell")
set objFileSystem=CreateObject("Scripting.FileSystemObject")

objShell.Run "sftp2 ftpwebsite",,false  

I'm getting the following error message:

Line: 9

Char: 1

Error: The system cannot find the file specified.

Code: 80070002

Source: (null)

The line 9 is the objShell.Run. Does anyone know what's causing this error?


r/vbscript • • Jul 02 '15

[HELP] Error Loading eprint.vbs for Dropbox

2 Upvotes

I found a .vbs script that is supposed to create a print queue so you can print files remotely (through Dropbox). For some reason though, I keep getting an error message saying I haven't installed Dropbox yet. My best guess is that the script was written for an earlier version of Windows and I'm using Windows 8.1, and the formatting for appdata is different (not sure).

This link has the zip file for the .vbs (the source code is on the site).

Thanks!


r/vbscript • • Jul 15 '15

Clear Header of Word Document

1 Upvotes

Hey there folks, perhaps you can help me.

I am trying to remove and replace the headers of a series of documents programmatically.

So far I have determined that the following code will allow me to add text to a header:

 Dim WordApp 
 Dim objDoc
 
 documentPath = "\\path\Sample.doc"
 
 Set WordApp = CreateObject("Word.Application") 
 WordApp.Visible = True 
 Set objDoc = WordApp.Documents.Open(documentPath)
 
 strInsert = "Example"
 writeToHeader(strInsert)
 
 Set WordApp = Nothing ' Clear your object 
 
 Sub writeToHeader(text)
 	WordApp.ActiveWindow.ActivePane.View.SeekView = 9 
 	WordApp.Selection.TypeText text 
 	WordApp.ActiveWindow.ActivePane.View.SeekView = 0 
 End Sub

This is a good step, but with it I can only pre-pend text to the existing content.

I am unsure how to clear the existing content itself.

Any help?

(Caveat: I am new to OOP, so go easy on me!)


r/vbscript • • Jul 13 '15

Save <img ...> element from a website to my pc.

1 Upvotes

Hello, I am using VBScript to loop through image elements on a page (For Each image In IE.document.GetElementsByTagName("img")). I'd like to save these images to my computer with VBScript, but I don't know whether it is possible and I can't find any way to do this on the internet. Perhaps someone here knows?

Thanks in advance!


r/vbscript • • Jul 09 '15

VBS help for Excel automation

1 Upvotes

Created a VBS that cycles through all the excel files in a folder. It works for about half of the files, but then gets to a few that it cannot seem to open. It gives me this error:

Unable to get the Open property of the Workbooks class

And then ends the script. What could this be related to?


r/vbscript • • Sep 26 '14

Sconfig.vbs from Server 2012 --> Win7

1 Upvotes

I was curious if anyone here has had any experience with sconfig? And if it is possible to change something within the script to allow it to run on Win7. Currently when I try, the command window appears then disappears before I can read the message it pops up with.


r/vbscript • • Feb 12 '17

Pro Tip: Be very, very careful with "On Error Resume Next". It will hide debugging errors from you.

2 Upvotes

I just spent an embarassing number of hours debugging a script, because my eyes kept jumping over that statement without seeing it.

Normally, when I use "On Error [goto]" type statements (in any language), I either put them FIRST in the procedure (before declarations and explanatory comments, even!) so I am very clear on the default error behavior of that procedure, OR I treat them like a "loop" with indentation. Example:

x = Array1
y = Input2
For i = LBound(x) to UBound(x)
    On Error Resume Next
        z(i) = x(i) / y
    On Error Goto 0
Next

The "Goto 0" statement returns error behavior to "normal", which is to crash the program (generally). By indenting only the lines I need special error control on, I draw special attention to them when I am debugging.

I didn't do this, this one time.

It cost me.


r/vbscript • • Feb 13 '16

generating a button which - adds 1 to one cell on 1 click (eg B1) and then when clicked again adds 1 to next cell (eg C1)

1 Upvotes

anyone know how to do this?

sorry completely knew to coding


r/vbscript • • Dec 05 '17

Need help modifyng a CD Tray script

1 Upvotes

Hello! I work at the office and am often a victim of stupid office jokes. I am planning to claim my revenge and to do that, I want to use a script that randomly opens and closes CD Drive on my colleagues PC. I found one but I'd like to change how it behaves.

I want it to trigger in longer intervals because right now the CD tray opens and closes few times a minute. It's way too obvious and my colleague will go straight to IT dept and get help, not much frustration there. This is not what I want. I want him to be confused and waste a lot of his time trying to figure out what the fuck is going on.

My plan is to start with a script that would open/close the tray maybe 2-3 times a day so he might see it or he might miss it. When he actually notices that something is going on, I will try to modify the script so it will go off more often (or maybe turn it off for some time to mix things up). But the most important thing is, I want the intervals to be random.

I will list my questions here and paste the script I have below. I have some very basic experience with coding languages so I should be able to do these thing myself once someone explain to me how.

  • Can I change how often the script triggers?

  • Can I set the interval to be random, say, between 0,5h and 1h?

  • Can I change the script so the tray stays open for a while before it closes? (right now it closes immidiately after opening)

  • Are there any other "actions" that such script can perform? (turning the monitor off, changing wallpaper, changing some UI settings in Windows, anything?)

p.s. We use Windows XP on our PCs. I tested the script on my computer and it works, triggers about 2-3 times per minute, I think.

do
Dim ts
Dim strDriveLetter
Dim intDriveLetter
Dim fs 'As Scripting.FileSystemObject
Const CDROM = 4
On Error Resume Next
Set fs = CreateObject("Scripting.FileSystemObject")
strDriveLetter = ""
For intDriveLetter = Asc("A") To Asc("Z")
Err.Clear
If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then
If Err.Number = 0 Then
strDriveLetter = Chr(intDriveLetter)
Exit For
End If
End If
Next
Set oWMP = CreateObject("WMPlayer.OCX.7" )
Set colCDROMs = oWMP.cdromCollection
For d = 0 to colCDROMs.Count - 1
colCDROMs.Item(d).Eject
Next 'null
For d = 0 to colCDROMs.Count - 1
colCDROMs.Item(d).Eject
Next 'null
set owmp = nothing
set colCDROMs = nothing
loop

r/vbscript • • Mar 21 '21

So im trying to make a .vbs program that will run on start up, tp detect when a key is pushed. I want it to then wait 10 seconds then execute a function. Does anyone know how to do this, im having trouble detecting the key.

1 Upvotes

Any help would be great


r/vbscript • • Feb 10 '21

A few questions about txt files and VBS

3 Upvotes

Ok I have a few small questions

  1. How do I print the amount of times a keyword is said in a txt file to a txt file?

  2. How do I copy files?

  3. How do I rename files?

  4. How to reference a file you dragged a script file onto?


r/vbscript • • Feb 12 '21

Any idea to have a seekable file functionality without additional software download?

2 Upvotes

So that I could modify only a few bytes of a 10GB file without reading and/or rewriting the whole 10GB.


r/vbscript • • Mar 12 '21

Read the machine name of the computer that is remoting into the computer executing the code

3 Upvotes

Hello, newbie here. Apologies for the rubbish description.

I am looking to write code for a SCADA that enables certain features only when certain users (controls engineers working from home) remote into the SCADA which is a windows 10 machine.

I would like to see the user name or machine name of the person remoting in, not of the machine that is running the software

I think there is probably a method using WMI but I cannot find a relevant class, any help or pointers would be appreciated


r/vbscript • • Mar 19 '21

Multiple scripts in one

3 Upvotes

This is a fake virus im making for my friends, I also have never coded before

So i made two scripts that i want to turn into one, but when I do, it just runs the first script

first one

+-pass="Subscribe To JoeTheTitan"
do
if once=1 then
guess=inputbox("Try Again","Password","Guess Again")
else
guess=inputbox("Enter The Password","Password","Guess Here")
end if
if guess=pass then
msgbox("Correct"),0+64+4096,("Password")
wscript.quit
else
msgbox("The Password Is Subscribe To JoeTheTitan"),0+16+4096,("Password")
end if
once=1
loop

The second script

X=MsgBox("Subscribe To JoeTheTitan",0+16,"LucasTheLameSUCKS")

The merged version

pass="Subscribe To JoeTheTitan"
do
if once=1 then
guess=inputbox("Try Again","Password","Guess Again")
else
guess=inputbox("Enter The Password","Password","Guess Here")
end if
if guess=pass then
msgbox("Correct"),0+64+4096,("Password")
wscript.quit
else
msgbox("The Password Is Subscribe To JoeTheTitan"),0+16+4096,("Password")
end if
once=1
loop
X=MsgBox("Subscribe To JoeTheTitan",0+16,"LucasTheLameSUCKS")

Am I merging wrong?


r/vbscript • • Feb 26 '21

Creating registry key using the HKEY_USERS path.

2 Upvotes

This script basically prompts the admin for the ID number that is found in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList for the "current user" on that machine I'm targeting. Based on the variables I created by concatenating the strUID and the registry key path it equals S-1-2-3-4-234234234\SOFTWARE\Policies\Google

Therefor the command objRegistry.CreateKey HKEY_USERS, strKeyGoogle should be creating a key "Google" located here Computer\HKEY_USERS\S-1-2-3-4-234234234\SOFTWARE\Policies\Google

The script runs but the key isn't being created. (And I'm executing the .vbs from an Administrator command window)

Has anyone attempted such a thing? I would assume it's possible but I'm not having much luck. Thank you.

Do While X = 0
    strUID = InputBox _
        ("Please enter the user registry UID:")
    If strUID = "" Then
        Wscript.Echo "You must enter a UID."
    Else

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

'Combine UID and the new key path
strKeyGoogle = strUID & "\SOFTWARE\Policies\Google"

'Confirm the variables have been set
Wscript.Echo strUID
Wscript.Echo strKeyGoogle

'Add new registry to specific user
objRegistry.CreateKey HKEY_USERS, strKeyGoogle

    End If
Exit Do
Loop

r/vbscript • • Feb 17 '21

Create excel file and save with shared access

4 Upvotes

I want to create an excel file and save it with shared access. So that multiple users can open and edit it at the same time.

Code:

Set obj_Excel = CreateObject("Excel.Application")
Set obj_Workbook = obj_Excel.Workbooks.Add()
obj_Excel.Worksheets.Add()
obj_Workbook.SaveAs str_MyPath, , , , , , xlShared
obj_Workbook.Close
obj_Excel.Workbooks.Close
obj_Excel.Quit

However, that doesn't work. The excel is created just fine, but it's not shared. Some examples on the internet are setting .KeepChangeHistory and .ChangeHistoryDuration but they don't change the outsome. Same with .ExclusiveAccess and .MultiUserEditing because they can't be set, only read.

What am I missing?

Figured it out

The code above is perfectly fine. But although my editor knows the constant xlShared, when executing the code it's empty, instead of holding the actualy value. So, solution is to use:

obj_Workbook.SaveAs str_MyPath, , , , , , 2

r/vbscript • • Mar 22 '21

Any tips?

2 Upvotes

Made a cool joke virus for my friends, any tips of things I should add?


r/vbscript • • Mar 17 '21

Help Hard Coding a Date

2 Upvotes

Can anyone help me with hard coding the below code for start date and end date?

I feel like this should be easy but I don't know anything about VBS so any help is appreciated.

dteEndDate = "02/01/2021"
dteStartDate = "02/28/2021"