excecuting vbscript(?) code: automatically open new files in a directory [closed] - windows

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
im new to vbs-,Powershell-, ... coding
i need to build something that checks for new files in a specific directory and opens all newly added files automatically.
i've found this post which seems to fit my needs perfectly: https://devblogs.microsoft.com/scripting/how-can-i-automatically-open-new-files-added-to-a-folder/
I tweaked the code to my situation like this:
Set objShell = CreateObject(“Wscript.Shell”)
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
(“SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE ” _
& “Targetinstance ISA ‘CIM_DirectoryContainsFile’ and ” _
& “TargetInstance.GroupComponent= ” _
& “‘Win32_Directory.Name=””T:\\\\Reporting\\\\AutoOpen””‘”)
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
strNewFile = objLatestEvent.TargetInstance.PartComponent
arrNewFile = Split(strNewFile, “=”)
strFileName = arrNewFile(1)
strFileName = Replace(strFileName, “\\”, “\”)
strFileName = Replace(strFileName, Chr(34), “”)
objShell.Run(“excel.exe ” & strFileName)
Loop
Bummer that the above post contains no details on how to actually use the code or what programming language it is :D
I assumed this snipped is for vbscript. When i put it in a .vbs file and execute it, i get the error-message "invalid character, line 1, character 29, compiling error, ..." -> this character is the first quotation mark in line 1.
So i also tried powershell, but it seems like the code doesnt fit there either.
... So what kind of code is this after all?
And how can i put it in an executable file?
Thank you so much in advance.

The slanted or smart quotes “.” you are using are invalid in vbscript code.
You should replace all of them with regular double quotes "
Applications such as MS Word tend to convert regular quotes with these slanted ones, so that might be where you got them from.

Related

Send multimedia keys using VB6 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I know that pause/play key(from keyboard)'s keyCode is 179, but how can I programically press those multimedia keys from VB6? There are some questions about it but they're for C#/C++/VB.Net
You can use the Chr function to convert your keycode to a string that SendKeys can use:
SendKeys Chr(179)
You might run into a Permission Denied error next, which you can work around by using a WScript.Shell object by using this subroutine:
Private Sub SendKeysViaWshShell(p_iKeyCode As Integer, Optional p_fWait As Boolean = False)
Dim objWshShell As Object
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.SendKeys Chr(p_iKeyCode), p_fWait
Set objWshShell = Nothing
End Sub

Getting actual powershell return value in vb6?

Alright, so I have been trying to do this for a while, and I have come to the realization that this isn't really something that is often asked, and with vb6 getting phased out more and more, there seems to be less help than I would like regarding the language.
The title doesn't say it all actually, as I am looking to do something very specific. I need to execute a shell command (that I know how to do), however, after I execute it, I want to be able to save the return value of that command as a string. For example, if the command is ipconfig, I want the entire return value of that, all the text I would see in powershell after executing that command, saved to a string in my program.
As far as I know, I need to "import" a few things, because I have to use WshShell, which I don't know where to get. So that's part of the question, what classes do I have to add and how, or if there is a way to do it without adding classes then even better. In addition, I have heard a lot about the use of CreatePipe and such regarding similar problems, but I don't know how to use it.
Basically, what I'm saying is that I am quite uneducated regarding the subject, and any insight would be much appreciated, and thanks to all who reply.
There are many ways. Using VBScript's WSHShell.Exec is the easiest.
This is VBScript but VBScript can be pasted into VB (not vice versa though).
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("ipconfig")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
MsgBox oExec.StdOut.ReadAll
Slightly modified from help.
This is how to ping from VBS/VB
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
'msgbox colItems
For Each objItem in colItems
msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
Next

read from exe files in vb6 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to read from exe files so i find this code
Dim strData As String
Dim lngFind As Long
Open txtAdrsOfFile For Binary As #1
strData = Space$(FileLen(txtAdrsOfFile))
Get #1, , strData
Close #1
lngFind = InStr(1, strData, txtFindString)
If strData = txtFindString Then
MsgBox "FIND"
Else
MsgBox "NOT FIND"
End If
its need a command buttton and 2 text boxs
but its not working
if there be a way to use that code if you guys can plz help me with doing this for multitipe strings i mean we can read more than 1 form a file
tnx
Firstly instr() returns an integer of the position of the find string inside the search string
It will not alter the two strings you pass it.
So instead of checking
If strData = txtFindString Then
You should check
If lngFind > 0 then
MsgBox "FIND"
Else
MsgBox "NOT FIND"
End If
This is because StrData is still going to equal what the contents of your file was before you call instr()
Also as pointed out if you searching for text and aren't worried about matching case you should call instr() with the text flag
lngFind = InStr(1, strData, txtFindString,1)

Copy a Cell Below A Cell with a Constant Value (see screenshot) - excel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The picture will make it all make sense. But I'm just trying to copy the text from below the "Author information:" Cell to another cell location. This isn't difficult for a few because it's just a simple copy and paste but I need to search the entire worksheet and extract all of the info from the first cell below "Author information:" At this point there are thousands of entries...
Screenshot Dropbox link: https://www.dropbox.com/s/nbyg8lkay3c5c9s/Screenshot%202013-12-27%2016.32.38.png
Any help would be fantastic!
For ease, try the following.
In cell C2 type this formula =IF(A1="Author Information:",A2,"")
Copy this formula all the way down
copy the results in column C to another sheet and paste-special with values only
Sort that sheet to get rid of blanks
That should be a quick and dirty way to get it done.
Try this code:
Sub copyAuthorInfo()
Dim src As Worksheet, tgt As Worksheet
Dim irows As Long
Dim rrows As Long
Set src = ThisWorkbook.Sheets("Sheet1") ' this is the sheet with the original data
Set tgt = ThisWorkbook.Sheets("Sheet2") ' author info will be copied into this sheet
irows = src.UsedRange.Rows.Count ' count the number of used rows in sheet 1
rrows = 1 'row counter for the target sheet
For i = 1 To irows
If Cells(i, 1).Value = "Author information:" Then 'make sure to adjust the search text
' copying columns A to C. Change the 3 in the following line to copy more columns.
Range(Cells(i + 1, 1), Cells(i + 1, 3)).Copy tgt.Range("A" & rrows)
rrows = rrows + 1
End If
Next
End Sub
To run the code, you need to open the Visual Basic Editor (VBE) with Alt-F11 or your preferred method. Use Insert > Module and paste the code. You can run the code by hitting F5 while the cursor is somewhere in the code.
You have tagged with OSX. Does that mean you use a Mac? You also tagged with Excel 2007 and 2010. The code will run fine in these, but not in Excel 2008 for Mac, because it does not support VBA.

Why doesn't 'shell' work in VBscript in VS6?

In a macro for Visual Studio 6, I wanted to run an external program, so I typed:
shell("p4 open " + ActiveDocument.FullName)
Which gave me a type mismatch runtime error. What I ended up having to type was this:
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
strResult = wshShell.Run("p4 open " + ActiveDocument.FullName)
What is going on here? Is that nonsense really necessary or have I missed something?
As lassevk pointed out, VBScript is not Visual Basic.
I believe the only built in object in VBScript is the WScript object.
WScript.Echo "Hello, World!"
From the docs
The WScript object is the root object of the Windows Script Host
object model hierarchy. It never needs to be instantiated before invoking its
properties and methods, and it is always available from any script file.
Everything else must be created via the CreateObject call. Some of those objects are listed here.
The Shell object is one of the other objects that you need to create if you want to call methods on it.
One caveat, is that RegExp is sort of built in, in that you can instantiate a RegExp object like so in VBScript:
Dim r as New RegExp
VBScript isn't Visual Basic.
Give this a try:
Shell "p4 open" & ActiveDocument.FullName
VB6 uses & to concatenate strings rather than +, and you'll want to make sure the file name is encased in quotes in case of spaces. Try it like this:
Shell "p4 open """ & ActiveDocument.FullName & """"

Resources