'======================================================================================================== '======================================================================================================== ' ### Script: ROT13.vbs ' ### Created: 10/15/05 ' ### Contact: Tim Chovanak, tim@blackpondfarm.com ' ### Description: Encode or Decode ROT13 '======================================================================================================== '======================================================================================================== Option Explicit On Error Resume Next 'Declare variables Dim WshShell,Wshnetwork,WshFile Dim strinputFile Set WshShell = Wscript.CreateObject("Wscript.shell") Set Wshnetwork = wscript.CreateObject("Wscript.Network") Set Wshfile = wscript.CreateObject("Scripting.FileSystemObject") 'Check to ensure we're using cscript If InStr(1, wscript.fullname, "cscript.exe", 1) = 0 Then VerifyCscript 'Parse the command line GetArgs() IF wshFile.FileExists(strInputFile) THEN ROT13(strInputFile) ELSE wscript.echo "Unable to find file " & strInputFile wscript.quit END IF wscript.quit '======================================================================================================== '======================================================================================================== Sub ROT13(strInputfile) On error resume next Dim wshtempfile Dim n,strAscii,strChr,text Dim Temp,Length Set wshtempFile = Wshfile.GetFile(strInputFile) Length = WshTempFile.Size 'Read our input file Set wshtempfile = wshfile.OpenTextFile(strInputFile, 1) temp = wshtempFile.Read(Length) wshtempfile.close For n = 1 to Length strAscii = asc(Mid(temp, n, 1)) If strAscii >= 97 And strAscii <= 109 Then strAscii = strAscii + 13 ElseIf strAscii >= 110 And strAscii <= 122 Then strAscii = strAscii - 13 ElseIf strAscii >= 65 And strAscii <= 77 Then strAscii = strAscii + 13 ElseIf strAscii >= 78 And strAscii <= 90 Then strAscii = strAscii - 13 ELSE 'out of range, do nothing End If strChr = CHR(strAscii) text = text & strChr Next Screenout text END SUB '======================================================================================================== '======================================================================================================== Function GetArgs() On Error Resume Next Dim a,argtext,strArgs For a = 0 To wscript.arguments.count - 1 argtext = argtext & wscript.arguments(a) & " " strArgs = wscript.arguments(a) next If strArgs <> "" Then strInputFile=strArgs Else Call SCRIPTHELP() Exit Function End If If ParseArgument("-?") Then Call SCRIPTHELP() Exit Function End If If ParseArgument("/?") Then Call SCRIPTHELP() Exit Function End If If Err.number <> 0 Then LogError "End of Arguments Function", Err.number Err.Clear End If End Function '======================================================================================================== '======================================================================================================== Function ParseArgument(strtext) Dim a On Error Resume Next For a = 0 To wscript.arguments.count - 1 If wscript.arguments(a) = strtext Then ParseArgument = a + 1 Next End Function '======================================================================================================== '======================================================================================================== Function screenout(text) Dim wsherrorlogfile On Error Resume Next If (Wshfile.fileexists(strOutputFile)) Then 'If our output file exists, open it, write our data, and close Set wsherrorlogfile = Wshfile.OpenTextFile(strOutputFile, 8) wsherrorlogfile.writeline (text) wscript.echo(text) wsherrorlogfile.Close wscript.DisconnectObject wsherrorlogfile Set wsherrorlogfile = Nothing If Err.number <> 0 Then Err.Clear Else 'File doesn't yet exist, create our output file and write our first line of text Set wsherrorlogfile = Wshfile.createtextfile(strOutputFile, 1) wsherrorlogfile.writeline (text) wscript.echo(text) wsherrorlogfile.Close wscript.DisconnectObject wsherrorlogfile Set wsherrorlogfile = Nothing If Err.number <> 0 Then Err.Clear End If End Function '======================================================================================================== '======================================================================================================== Sub ScriptHelp() wscript.echo "" wscript.echo "Usage:" wscript.echo "rot13 inputfile" wscript.echo "" wscript.echo "Example:" wscript.echo "rot13 test.txt" wscript.echo "" wscript.echo "Converts text to or from ROT-13." wscript.quit END Sub '======================================================================================================== '======================================================================================================== Function VerifyCscript() Dim temp REM ###### Sets up Vbscript to always run in command window ###### temp = wshshell.Run("cmd /c ""wscript //h:cscript //nologo //s 1>nul 2>nul""", 0, true) temp = MsgBox ("The script has changed the default output of Windows Scripting Host to the command prompt." &_ vbCrLf & "This is pop up is normal, just re-run the script!", 0, "WSH default changed to cscript.") wscript.quit End Function '======================================================================================================== 'END OF SCRIPT '========================================================================================================