Skip to main content

Posts

Showing posts with the label vbscript

Loop thru custom Textboxes name in VBA or VB.Net

Having a custom name in Textboxes, Listbox, ComboBox in VBA or VB.Net is a good strategy. So, it will be easy to debug the code or analyze the data.

If just relying on the default names is also good but it will get messy and it will be hard to control when there’s quite a few of textboxes, and list boxes. And the next person who will maintain the code will find it easier to debug or check the program when proper custom name is assigned to the controls. And that next person might be yourself. So, be gentle and be explicit in giving comments and assigning names to controls.

It doesn’t hurt to put a clear comment or an overview of what the piece of code does or what’s the input that the control is accepting. Because greediness breeds greediness, and it will bite back.

Anyway, to loop to all the custom or default names is quite easy.


Example, if there are 30 or more textboxes in a form. It can be done by a simple for loop like:

For x = 1 to 30

GetTboxvalue =    Me.Controls(“Textbox” & …

PowerShell get folder capacity

Get the folder capacity using PowerShell via the old-school method.
Old-school since we will utilize the native “dir” command, just like the good old days of DOS.
PowerShell dir command output is not the same with the native windows system32 dir command line just like in DOS system.
The output will be in bytes since it is from the command line but of course, if you are command line junkie it will be easy to read even if the capacity is written in bytes.
First, fire up notepad write the dir command plus the full folder path of the specified folder in which you like to monitor or want to know the capacity.
 dir “c:\users\dmusic\music folder 001”
The path is enclosed in quotes since the folder has spaces and save the notepad as a batch file with “.bat” extension.
Open PowerShell command line window or PowerShell ISE and type the following command:
#change the path to where the batch file was saved
$folder_base_cap = d:\read_folder_cap.bat 
$ folder_base_cap | Select-String "bytes&qu…

Sort numbers, digits or figures in Excel 2016

To sort numbers, digits, or figures in Excel 2016 is quite straight forward.
There are two ways to do it via the Home tab or Data tab.
What is found on the Home tab is sorting of alphabets or A-Z option which can easily be seen beside the auto-sum function which is on the right most tab of the ribbon by default.
Screen shot below shows the sorting functionality found in Home tab.


The other way to sort by numbers, digits or figures in Excel 2016 is to click on “Data” click on “Sort” then an option will appear to sort by smallest to largest or largest to smallest.
A screen shot below shows where to find the function.


That’s it hopes it helps to make thing easier.


================================ Free Android Apps:
Click on links below to find out more:

Excel Keyboard shortcuts guide https://play.google.com/store/apps/details?id=chrisjoms.myexcelapplicationguide
Linux Android App cheat sheet: https://play.google.com/store/apps/details?id=com.LinuxMobileKit Multiplication Table for early learner…

PowerShell disable USB ports

Disabling USB ports using PowerShell by modifying registry values.
How to user PowerShell to modify an existing value in Windows Registry?
PowerShell code below modifies the registry value and disable USB ports in Windows.
The code must be run in an elevated mode to modify the registry.
Here's the code:
Modify the registry key value using PowerShell:
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR\" -Name "start" -Value 4
To enable the USB ports, change the value to 3:
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR\" -Name "start" -Value 3
Disabling USB ports is quite annoying of course, but if security is a concern then I guess it is justifiable especially computers that can easily be accessed by unwanted guests or individuals.

It will also protect the network or computers from getting malware's or viruses that is transferred or copied via USB storage.



Cheers! Till next time.


==============…

PowerShell validate list of email addresses

Validate list of email addresses in a text file and check which email is a valid email and which one has the incorrect format.
PowerShell code below can easily check or validate incorrect email, output will show true if valid email and false if the email is not valid.
To check list of valid emails PowerShell utilizes regex. This is useful to check typo errors when sending out mass email or a list of emails which has not yet been verified.

But of course, the code will not be able to check whether the emails are active or not.

#===============================
$reader = [System.IO.File]::OpenText("c:\all_emails.txt") #get-content can also be used
while($null -ne ($line = $reader.ReadLine())) {
    #$line     $regx="[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
    [regex]::Match($line, $regx, "IgnoreCase ")  |  format-Table success, value -AutoSize }

#========…

How to escape in PowerShell

In bash script the backslash is use as escape sequence and even in Java programming.

But what's the equivalent of bash escape sequence in PowerShell?

Grave accent or back tick, which is found at the top left corner of the keybord below the "esc" key on some keyboard, This character "`" is the escape in PowerShell.

For example:

Write-host "This line is above `r`n and  `r`n this line is below"

Output is:
This line is above
 and
 this line is below

r is equal to carriage return character putting a backtick before r, `r tells PowerShell that r is not a character but a carriage return
n is a new line character putting a backtick before n, `n tells PowerShell that n is a new line character.

Another example:

$ sign in PowerShell is to indicate a variable

But what if you need to display a dollar sign as a character and not as a special character to declare a variable. Then we need to escape the dollar sign by prefixing a backtick before the character.

Exampl…

Excel VBA load all text files to listbox

Get all text files in a specified path to a  userform listbox using VBA.

The code below will read the directory for all text files and its filename will be loaded to listbox.

=====================

Dim fs, f, fc, f1

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder("C:\text_folder")

    Set fc = f.Files

    For Each f1 In fc

        If InStr(1, f1.Name, ".txt") Then

           'add all filenames to listbox
           UserForm1.ListBox1.AddItem f1.Name

        End If

    Next

=====================

Happy coding!


Cheers..till next time.



================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?id=com.LinuxMobileKit
Multiplication Table for early learners https://play.google.com/store/apps/details?id=com.TableMultiplication
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuf…

PowerShell search text file contents

PowerShell code below will search recursively all text file on the path specified. If the pattern matches on the text file, the contents will be copied and all matching patterns will be consolidated on a single file.
#=============================
Get-ChildItem -Path "d:\all_text_files \" -recurse | ForEach-Object {
  @'
= = =>>>The file name - {0}
{1}
- - - - - - -
`r`n
'@ -f $_.Name, (Get-Content $_.FullName )
}  | Select-String -pattern "router" | Out-File 'd:\d_output\output.txt'
#=============================
Code explanation:
Get-ChildItem -Path "d:\txt notes\" -recurse | ForEach-Object  >>> Get all the items recursively on the path specified and for each object process each object
  @'  '@ >>> PowerShell string format that any text or string between the @ symbol will be process exactly as it is displayed, it’s like what you see is what you get.
{0} {1} >>> are place holders, in which the value w…

Use PowerShell to list installed programs

Do you need to check list of programs installed on remote computers?

PowerShell will come in handy to check list of installed software on remote computers.

Open PowerShell ISE or the PowerShell command line.

Make sure the PowerShell settings is configured to run PowerShell scripts.

Here's the PowerShell code snippet.

#for 64 bit
 #Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |  Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

#for 32 bit
 Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.publisher -notmatch "^Microsoft"} | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

To get the list of non-Microsoft software installed on the computer:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.publisher -notmatch "^Microsoft"} | Select-Object DisplayName, DisplayVersion, Publish…

PowerShell create active directory user

Create bulk users for Active Directory in PowerShell with the help of a text file or a CSV file can be done easily. But the code below is to create a single user account. The code below can easily be tweak by using for loop command and reading an external file where the data will be imported to PowerShell variables.
When I was creating this script, I encountered an error that says:
new-aduser attribute value was not in the acceptable range
Basically, the error is good and is useful as well. Since it already gives a hint that one of the value is not acceptable.
And on this case, it was the Country variable, I was writing the whole word “Philippines” when I changed it to PH and the error went away.
Here’s the code: #------Code Start--------  $Domain="@gui.local" $Firstname="Yorem" $Lastname="Goas" $Email="yorem.goas@gui.com" $Password="Wh@t!s1t0k" $Username=$Firstname +"."+ $Lastname $UPN=$Username+$Domain $DisplayName= "$Firstname…

PowerShell remove line feed

Line feed is useful in some ways like formatting lines so it will be easy to read if there’s a line in between the other line.
But there are also instances that line feed might be annoying or simply we need to get rid of it since it’s not desired on the output.
There instances that Out-string parameter in PowerShell will append a line feed on its output.
Or if you take the input or output from other sources but want to remove the line feed, PowerShell can remove the line feed without any hassle.
Code below is using PowerShell to remove line feed from an input, the input could either be a text file or a string pipe to another variable.
$string_with_line_feed -replace "`n|`r"
`n -  backtick with n refers to the line feed character `r – backtick with r refers to the carriage return
Or the code above can be written as follows:
$string_with_line_feed -replace "`n|"
So, basically line feed is replace with nothing. But for readability and to avoid confusion it’s better to …

Powershell move selected files

Powershell script below will sort files base on its last write time, it will select the files and move the selected files to another folder.

This script will remove the first 3 old files base on the lastwritetime property, quite useful to archive files or grouping large files into multiple folders.
#=====Code Snippet Start
$xfiles= Get-ChildItem d:\the_log_folder |  Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true} | Select-Object -first 3 | select fullname | ft -HideTableHeaders
write $xfiles >  d:\move_files_listing.txt
ForEach ($movefiles in (Get-Content "d:\move_files_listing.txt"))
{     
  If ($movefiles[1].Length –gt 0) {
      move-item $movefiles d:\the_archive_folder
      write $movefiles
     }
}
#=====Code Snippet Ends

Code explanation:
--# Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true # this will sort the files beginning from the file with the  last write time or basically the old files base on its write time.
--# …