Scripts VBScript: Soma, Loops, Arquivos e Menus

Computação4,69 KB

Soma de Dois Valores

==================
SOMA
==================
wscript.echo "Primeiro teste em VBScript"
Dim a, b, resultado
a = 10
b = 15
resultado = a + b
MsgBox "O resultado da operação é: " & resultado
a = InputBox("Digite o primeiro valor: ")
b = InputBox("Digite o segundo valor: ")
resultado = CInt(a) + CInt(b)
MsgBox "O resultado da soma é: " & resultado
==================

Contador Simples

==================
Dim x
For x = 1 To 10
    wscript.echo x
Next
==================

Acumulador Horizontal

==================
Dim x, acumulador
acumulador = " "
For x = 1 To 10
    acumulador = acumulador & x & " "
Next
wscript.echo acumulador
==================

Acumulador Vertical

==================
Dim x, acumulador
acumulador = ""
For x = 1 To 10
    acumulador = acumulador & x & vbCrLf
Next
wscript.echo acumulador
==================

Abrir Programa

==================
Dim WshShell
Dim intOpcao
intOpcao = MsgBox("Deseja abrir o Paint?", vbYesNo, "Windows")
If intOpcao = vbYes Then
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run("%systemroot%\system32\mspaint.exe")
End If

Referências de comandos:

  • %systemroot%\system32\control.exe - Abre o Painel de Controle
  • %windir%\notepad.exe - Abre o Bloco de Notas
  • shutdown -s -t 00 - Desliga o computador
  • shell:::{645FF040-5081-101B-9F08-00AA002F951E} - Abre a Lixeira
  • shutdown -r -t 00 - Reinicia o computador
  • %systemroot%\system32\taskmgr.exe - Abre o Gerenciador de Tarefas
  • %systemroot%\system32\mspaint.exe - Abre o Paint
  • %systemroot%\system32\cmd.exe - Abre o Prompt de Comando

Ler Arquivo

==================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set arq = objFSO.OpenTextFile("c:\docs\texto.txt", 1)
Dim linha, i
linha = ""
i = 0
Do While Not arq.AtEndOfStream
    linha = linha & arq.ReadLine & vbCrLf
    i = i + 1
Loop
wscript.echo "Conteúdo do arquivo: " & vbCrLf & linha & "Qtd linhas: " & i & vbCrLf
arq.Close
==================

Abrir Programa com IF

==================
Dim WshShell
Dim intOpcao
intOpcao = InputBox("Digite o número da opção que você deseja: " & vbCrLf & "1) MS-Paint" & vbCrLf & "2) Notepad" & vbCrLf & "3) Prompt de Comando")
If intOpcao = 1 Then
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run("%systemroot%\system32\mspaint.exe")
ElseIf intOpcao = 2 Then
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run("%windir%\notepad.exe")
Else
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run("%systemroot%\system32\cmd.exe")
End If
==================

Tabuada

==================
Dim x, acumulador, n
acumulador = ""
n = InputBox("Digite um número: ")
For x = 1 To 10
    acumulador = acumulador & n & " x " & x & " = " & x * n & vbCrLf
Next
wscript.echo acumulador
==================

Leitura de Arquivos por Opção

==================
Dim a, x
Set objFSO = CreateObject("Scripting.FileSystemObject")
a = InputBox("Digite 1 para operadores e 2 para consultores: ")
If a = 1 Then
    Set arq = objFSO.OpenTextFile("C:\Users\DANIELA\Desktop\UNIP\2º SEM\LPA\script\Operador.txt", 1)
    Dim linha
    linha = ""
    Do While Not arq.AtEndOfStream
    linha = linha & arq.ReadLine & vbCrLf
    x = x + CInt(1)
    Loop
    wscript.echo "Conteúdo do arquivo: " & vbCrLf & linha & vbCrLf & "TOTAL DE " & x & " LINHA(S)"
    arq.Close
Else
    Set arq = objFSO.OpenTextFile("C:\Users\DANIELA\Desktop\UNIP\2º SEM\LPA\script\consultor.txt", 1)
    linha = ""
    Do While Not arq.AtEndOfStream
    linha = linha & arq.ReadLine & vbCrLf
    x = x + CInt(1)
    Loop
    wscript.echo "Conteúdo do arquivo: " & vbCrLf & linha & vbCrLf & "TOTAL DE " & x & " LINHA(S)"
    arq.Close
End If
==================

Baixar

PDF Word HTML