Wrong Syntax error for date in vbscript - vbscript

What's wrong with this code?
if(Year(rs.Fields(created_date)) == Year(Now) AND Month(rs.Fields(created_date)) == Month(Now) AND Day(rs.Fields(created_date)) >= Day(Now) +3) {
It says, wrong syntax. How will I make this correct?

The operator is And, not &&:
>> If True && False Then
>> WScript.Echo "&& is not VBScript"
>> End If
>>
Error Number: 1002
Error Description: Syntax error
>> If True And True Then
>> WScript.Echo "&& is And"
>> End If
>>
&& is And
On second look: VBScript is not Javascript (==, {}).

Related

How to test for function return false?

Can someone tell me what
if(!IsNumeric(aNumber))
{
do something
}
is in valid VBScript? I've tried
!IsNumeric(aNumber)
already.
The logical negation operator is called Not in VBScript:
>> b = 1 = 1
>> WScript.Echo TypeName(b), CStr(b), CStr(Not b)
>>
Boolean True False
>>
2nd sample:
>> For Each e In Split("1 a 2")
>> If Not IsNumeric(e) Then
>> WScript.Echo e, "not a number"
>> Else
>> WScript.Echo e, "a numerical string"
>> End If
>> Next
>>
1 a numerical string
a not a number
2 a numerical string
>>

VBScript To Remove Number from filename

I'm looking for a VBscript which scan a folder for files and remove number from filenames. for example if we have a file named "target1990.txt" then It should be "target.txt"
Can anyone help please?
Use a RegExp that looks repeatedly/globally for a sequence of digits (\d+) and replace all matches with "" (nix):
>> set r = New RegExp
>> r.Global = True
>> r.Pattern = "\d+"
>> s = "target1990.txt"
>> WScript.Echo s, r.Replace(s, "")
>>
target1990.txt target.txt
further sample (same regexp):
>> s = "t1ar33get19s90.txt"
>> WScript.Echo s, r.Replace(s, "")
>>
t1ar33get19s90.txt targets.txt

VBScript Error code 800A0409, Unterminated string constant, on line 1

I'm getting Error code 800A0409, Unterminated string constant, on line 1, 54 with the code below.
Option Explicit
Dim ObjProgressMsg
Dim fso,objText,strVstup,strVystup,f,dtmVyt,dtmF,dDiff,fName,fExt,fShort,dtmAkt,tx,msgText
Dim strMessage,strWindowTitle,strTemp,wshShell,objTempMessage,strTempVBS
Set fso = CreateObject("Scripting.FileSystemObject")
Set objText = fso.GetFile("l:\bat\posledni.den")
strVstup = "l:\filefolder\"
strVystup = "l:\backup"
dtmVyt = objText.DateLastModified
msgText = "Some text about copying and renaming" & VbCrLf & "files, please wait..."
ProgressMsg msgText
For Each f In fso.GetFolder(strVstup).Files
dtmF = f.DateLastModified
dDiff = DateDiff("s", dtmF, dtmVyt)
If dDiff < 0 Then
ProgressMsg ""
WScript.Echo f
End If
Next
WScript.Echo "Some text about the task being finished."
Function ProgressMsg( strMessage )
' Written by Denis St-Pierre
' Displays a progress message box that the originating script can kill in both 2k and XP
' If StrMessage is blank, take down previous progress message box
' Using 4096 in Msgbox below makes the progress message float on top of things
' CAVEAT: You must have Dim ObjProgressMsg at the top of your script for this to work as described
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strTEMP = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
If strMessage = "" Then
' Disable Error Checking in case objProgressMsg doesn't exists yet
On Error Resume Next
' Kill ProgressMsg
objProgressMsg.Terminate( )
' Re-enable Error Checking
On Error Goto 0
Exit Function
End If
strTempVBS = strTEMP + "\" & "Message.vbs" 'Control File for reboot
' Create Message.vbs, True=overwrite
Set objTempMessage = fso.CreateTextFile( strTempVBS, True )
objTempMessage.WriteLine( "MsgBox""" & strMessage & """, 4096, """ & "a_sp_rano" & """" )
objTempMessage.Close
' Disable Error Checking in case objProgressMsg doesn't exists yet
On Error Resume Next
' Kills the Previous ProgressMsg
objProgressMsg.Terminate( )
' Re-enable Error Checking
On Error Goto 0
' Trigger objProgressMsg and keep an object on it
Set objProgressMsg = WshShell.Exec( "%windir%\system32\wscript.exe " & strTempVBS )
End Function
The script should show a msgbox while searching for files newer than last modified date of posledni.den file. Then once it finds a file it should close msgbox and echo the file it found.
It works just fine if I change this:
msgText = "Some text about copying and renaming" & VbCrLf & "files, please wait..."
to this:
msgText = "Some text about copying and renaming" & "files, please wait..."
Removal of VbCrLf seems to fix that error, just no line break is obviously happening. I can't figure out why it's behaving like that, what am I doing wrong. Every kind of insight on the problem would be much appreciated.
Thank you in advance. :)
The error occurs in the execution of the generated .vbs. What you do is:
>> msg1 = "A" & vbCrLf & "B"
>> code = "MsgBox """ & msg1 & """"
>> WScript.Echo code
>>
MsgBox "A
B"
>> Execute code
>>
Error Number: 1033
Error Description: Unterminated string constant
What you should do:
>> msg1 = """A"" & vbCrLf & ""B"""
>> WScript.Echo msg1
>>
"A" & vbCrLf & "B"
>> code = "MsgBox " & msg1 & ", 4096"
>> WScript.Echo code
>>
MsgBox "A" & vbCrLf & "B", 4096
>> Execute code
>>
>> <-- no news are good news; message displayed

VBScript cut off end of filename after space but keep extension

I've been searching online. Is there a way to cut off the space and the rest of the filename but leave the extension with VBScript.
Say I have a filename like this:
filename this is a file.txt
Could VBScript cut off the space and everything afterwards but leave the extension like this:
filename.txt
Sure, you can do some surgery with the string functions available in vbscript.
dim s
dim s2
s = "filename this is a file.txt"
s2 = Left(s, Instr(s, " ")-1) & Right(s, Len(s) - InstrRev(s, ".") + 1)
msgbox s2
There are several ways to achieve what you want:
Using a regular expression:
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "^(\S*).*(\..*?)$"
Set f = fso.GetFile("filename this is a file.txt")
f.Name = re.Replace(f.Name, "$1$2")
Using Split:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("filename this is a file.txt")
f.Name = Split(fso.GetBaseName(f))(0) & "." & fso.GetExtensionName(f)
Using string functions: see answer provided by KekuSemau.
Use a RegExp to cut the first 'word' and the extension from your input:
>> Set r = New RegExp
>> r.Pattern = "^(\w+)(.*)(\..*)$"
>> For Each s In Array("filename this is a file.txt", "a.txt", "1a nix ...txt")
>> WScript.Echo s, """" & r.Replace(s, "$1$3") & """"
>> Next
>>
filename this is a file.txt "filename.txt"
a.txt "a.txt"
1a nix ...txt "1a.txt"
If you insist on staying with String ops, use Mid() instead of Right():
>> s = "filename this is a file.txt"
>> a = Left(s, InStr(s, " ") - 1)
>> b = Mid(s, InStrRev(s, "."))
>> WScript.Echo a, b, a & b
>>
filename .txt filename.txt

Is it possible to use AutoHotKey to run PSLoggedOn and edit the output in the stream and return the result?

I am using AutoHotKey to build a simple GUI tool that uses the Sysinternals tool PSLoggedOn. First if I use
run psloggedon.exe -l -x \\computername I don't get any output at all.
So I tried run %ComSpec% /C psloggedon.exe -l -x 1> %Temp%\psloggedon.txt and that gives me the output in domain\username format for each user logged in.
Like my first example I would rather just run psloggedon and get the output instead of first opening a command prompt; is that possible somehow?
Either way I want to take the output and avoid writing it to a file, but instead edit the output from the output stream to remove the "domain\" part and then just return the username. How can this be done without any third-party software?
This answer to another question on Serverfault shows a way to do it by reading the StdoutRead.
I too was looking at a way to avoid using a file but have decided it's actually the simplest option.
Code copied from user60738
#include <Constants.au3>
Dim $line, $line2, $file, $icount, $reg, $reg2, $reg3
$file = FileOpen("pclist.txt", 0)
While 1
$line = FileReadLine($file)
If #error Then ExitLoop
$reg3 = ""
$reg2 = ""
$reg = ""
If Ping($line, 200) Then
$reg = #ComSpec & ' /C "' & #ScriptDir & "\pstools\psloggedon.exe -l -x \\" & $line & '"'
$reg = Run($reg, "", #SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
While 1
$reg2 = StdoutRead($reg)
If #error Then ExitLoop
If $reg2 Then
$reg3 &= $reg2
EndIf
WEnd
If StringInStr($reg3, "Error") Then
$reg = "Error"
ElseIf StringInStr($reg3,"No one is logged") Then
$reg = "No one is logged on locally."
Else
$reg = StringTrimLeft($reg3, StringInStr($reg3, Chr(13), "", 3))
$reg = StringTrimLeft($reg, StringInStr($reg, "\"))
$reg = StringTrimRight($reg, StringLen($reg) - StringInStr($reg, Chr(13)))
$reg = StringStripWS($reg, 8)
EndIf
$icount += 1
$line2 &= $icount & #TAB & $line & #TAB & $reg & #CRLF
EndIf
TrayTip("Psloggedon", $icount & " " & $line & " User: " & $reg, 10)
WEnd
FileClose($file)
ConsoleWrite($line2)
FileDelete("C:\output.txt")
FileWrite("C:\output.txt", $line2)
ShellExecute("C:\output.txt")

Resources