I have two classic asp pages in which I had to call a batch script. The first one is working:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%'Roda o arquivo BAT para chamada do script powershell
Set wshell = CreateObject("WScript.Shell")
wshell.Run "\\w102xnk172\c$\inetpub\wwwroot\DCT_NEW\App\copy_items.bat"
Set wshell = Nothing
'Cria o arquivo .txt de bloqueio, que o script powershell excluirá em sua finalização.
Dim fs,tfile
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set tfile = fs.CreateTextFile("\\w102xnk172\c$\inetpub\wwwroot\DCT_NEW\DB\running_script.txt")
tfile.Close
Set tfile = Nothing
Set fs = Nothing
'Quando não existir mais o arquivo, a interface continua.
filename = "running_script.txt"
path_db = "\\w102xnk172\c$\inetpub\wwwroot\DCT_NEW\DB\"
path_file = "\\w102xnk172\c$\inetpub\wwwroot\DCT_NEW\DB\running_script.txt"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
While fs.FileExists(path_file) = True
Wend
%>
This code runs my script and creates a "running_script.txt", then checks if it still exits, so no code runs until it ends. My script, at it's end, delete that file. This is working fine, and the other on is just as simple:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
page = REQUEST.QUERYSTRING("page")
If page = "ofs" Then
Set wshell = CreateObject("WScript.Shell")
wshell.Run "C:/www/gppintranet/scripts/ofs.bat"
Set wshell = Nothing
End If
response.Redirect("/gppintranet/view/home.asp")
%>
But for some reason it does not call my script. I'm automatically redirected to the home page. I've debugged, it is getting into my If statement and the script is running properly.
I'm running this application in IIS 10.0.15063.0, if it is of use.
What is wrong? I see no difference between them.
EDIT: I have run the non-working script at the working enviroment, and it didn't work there as well. I've checked and both IIS are at the same version and with the same settings. The working script is:
#echo off
start "" http://w102xkj172/DCT_MASTER/update_dct_p2p.asp
timeout /T 5
echo Waiting for P2P DCT's information update...
:verify_locked
if exist \\w102xkj172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT_MASTER\p2p_running.txt (
goto :verify_locked
) else (
echo Copying files...
robocopy /IS \\w102xkj172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT\DB\ \\w102xnk172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT_LINE_P\DB\ DCT.mdb
robocopy /IS \\w102xkj172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT\DB\ \\w102xnk172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT_LINE\DB\ DCT.mdb
robocopy /IS \\w102xkj172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT\DB\ \\w102xnk172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT_LINE_1\DB\ DCT.mdb
robocopy /IS \\w102xkj172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT\DB\ \\w102xnk172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT_LINE_3\DB\ DCT.mdb
robocopy /IS \\w102xkj172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT\DB\ \\w102xnk172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT_LINE_4\DB\ DCT.mdb
)
exit
And the non working is:
#echo off
START "" http://example.aspx
Related
This has been bugging me for while as I feel I have few pieces of the puzzle but I cant put them all together
So my goal is to be able to search all .pdfs in a given location for a keyword or phrase within the content of the files, not the filename, and then use the results of the search to populate an excel spreadsheet.
Before we start, I know that this easy to do using the Acrobat Pro API, but my company are not going to pay for licences for everyone so that this one macro will work.
The windows file explorer search accepts advanced query syntax and will search inside the contents of files assuming that the correct ifilters are enabled. E.g. if you have a word document called doc1.docx and the text inside the document reads "blahblahblah", and you search for "blah" doc1.docx will appear as the result.
As far as I know, this cannot be acheived using the FileSystemObject, but if someone could confirm either way that would be really useful?
I have a simple code that opens an explorer window and searches for a string within the contents of all files in the given location. Once the search has completed I have an explorer window with all the files required listed. How do I take this list and populate an excel with the filenames of these files?
dim eSearch As String
eSearch = "explorer " & Chr$(34) & "search-ms://query=System.Generic.String:" & [search term here] & "&crumb=location:" & [Directory Here] & Chr$(34)
Call Shell (eSearch)
Assuming the location is indexed you can access the catalog directly with ADO (add a reference to Microsoft ActiveX Data Objects 2.x):
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
cn.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows'"
sql = "SELECT System.ItemNameDisplay, System.ItemPathDisplay FROM SystemIndex WHERE SCOPE='file:C:\look\here' AND System.Kind <> 'folder' AND CONTAINS(System.FileName, '""*.PDF""') AND CONTAINS ('""find this text""')"
rs.Open sql, cn, adOpenForwardOnly, adLockReadOnly
If Not rs.EOF Then
Do While Not rs.EOF
Debug.Print "File: "; rs.Collect(0)
Debug.Print "Path: "; rs.Collect(1)
rs.MoveNext
Loop
End If
Try using the next function, please:
Function GetFilteredFiles(foldPath As String) As Collection
'If using a reference to `Microsoft Internet Controls (ShDocVW.dll)_____________________
'uncomment the next 2 lines and comment the following three (without any reference part)
'Dim ExpWin As SHDocVw.ShellWindows, CurrWin As SHDocVw.InternetExplorer
'Set ExpWin = New SHDocVw.ShellWindows
'_______________________________________________________________________________________
'Without any reference:_____________________________________
Dim ExpWin As Object, CurrWin As Object, objshell As Object
Set objshell = CreateObject("Shell.Application")
Set ExpWin = objshell.Windows
'___________________________________________________________
Dim Result As New Collection, oFolderItems As Object, i As Long
Dim CurrSelFile As String
For Each CurrWin In ExpWin
If Not CurrWin.Document Is Nothing Then
If Not CurrWin.Document.FocusedItem Is Nothing Then
If left(CurrWin.Document.FocusedItem.Path, _
InStrRev(CurrWin.Document.FocusedItem.Path, "\")) = foldPath Then
Set oFolderItems = CurrWin.Document.folder.Items
For i = 0 To oFolderItems.count
On Error Resume Next
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Else
Result.Add oFolderItems.item(CLng(i)).Name
On Error GoTo 0
End If
Next
End If
End If
End If
Next CurrWin
Set GetFilteredFiles = Result
End Function
Like it is, the function works without any reference...
The above function must be called after you executed the search query in your existing code. It can be called in the next (testing) way:
Sub testGetFilteredFiles()
Dim C As Collection, El As Variant
Set C = GetFilteredFiles("C:\Teste VBA Excel\")'use here the folder path you used for searching
For Each El In C
Debug.Print El
Next
End Sub
The above solution iterates between all IExplorer windows and return what is visible there (after filtering) for the folder you initially used to search.
You can manually test it, searching for something in a specific folder and then call the function with that specific folder path as argument ("\" backslash at the end...).
I've forgotten everything I ever knew about VBA, but recently stumbled across an easy way to execute Explorer searches using the Shell.Application COM object. My code is PowerShell, but the COM objects & methods are what's critical. Surely someone here can translate.
This has what I think are several advantages:
The query text is identical to what you wouold type in the Search Bar in Explorer, e.g.'Ext:pdf Content:compressor'
It's easily launched from code and results are easily extracted with code, but SearchResults window is available for visual inspection/review.
With looping & pauses, you can execute a series of searches in the same window.
I think this ability has been sitting there forever, but the MS documentation of the Document object & FilterView method make no mention of how they apply to File Explorer.
I hope others find this useful.
$FolderToSearch = 'c:\Path\To\Folder'
$SearchBoxText = 'ext:pdf Content:compressor'
$Shell = New-Object -ComObject shell.application
### Get handles of currenlty open Explorer Windows
$CurrentWindows = ( $Shell.Windows() | Where FullName -match 'explorer.exe$' ).HWND
$WinCount = $Shell.Windows().Count
$Shell.Open( $FolderToSearch )
Do { Sleep -m 50 } Until ( $Shell.Windows().Count -gt $WinCount )
$WindowToSerch = ( $Shell.Windows() | Where FullName -match 'explorer.exe$' ) | Where { $_.HWND -notIn $CurrentWindows }
$WindowToSearch.Document.FilterView( $SearchBoxText )
Do { Sleep -m 50 } Until ( $WindowToSearch.ReadyState -eq 4 )
### Fully-qualified name:
$FoundFiles = ( $WindowToSearch.Document.Folder.Items() ).Path
### or just the filename:
$FoundFiles = ( $WindowToSearch.Document.Folder.Items() ).Name
### $FoundFIles is an array of strings containing the names.
### The Excel portion I leave to you! :D
I have a vbscript that opens a workbook and runs a procedure in the module1 of the workbook, then closes the workbook. Works great. But here's the thing. The path to the workbook is hard coded, like this:
Set xlBook = xlApp.Workbooks.Open("E:\FolderName\WorkbookName.xlsm", 0, True)
I don't mind requiring the user to have the script and the workbook in the same folder, and requiring that they not change the name of the workbook file, but I'd like to just substitute the hard coded path with something dynamic, like:
Set xlBook = xlApp.Workbooks.Open(VBSPath & "\WorkbookName.xlsm", 0, True)
So the path is whatever is the path of the script.
Is that possible? Any other ideas?
tod
VBScript provides various information about the current script and host process via properties of the WScript object, among them:
ScriptFullName: The full path to the current script.
ScriptName: The filename of the current script.
You can derive the script folder from the ScriptFullName property in a number of ways, e.g.
By using the GetParentFolder method:
Set fso = CreateObject("Scripting.FileSystemObject")
dir = fso.GetParentFolderName(WScript.ScriptFullName)
By using the Len and Left functions:
dir = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName)-Len(WScript.ScriptName))
By using the InStrRev and Left functions:
dir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
By using the Split and Join functions:
a = Split(WScript.ScriptFullName, "\")
a(UBound(a)) = ""
dir = Join(a, "\")
By using the Replace function:
dir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
Note that this approach might prove problematic if (for whatever reason) the script name appears somewhere else in the full name of the script as well (e.g. C:\script.vbs\script.vbs).
You can try like that :
VBSPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) - (Len(WScript.ScriptName) + 1)))
msgbox VBSPath
I figured out one solution, very similar to what has been posted here.
I created a WScript.Shell object:
Set WshShell = CreateObject("WScript.Shell")
Then I used the CurrentDirectory as the path of the workbook:
Set xlBook = xlApp.Workbooks.Open(WshShell.CurrentDirectory & "\WorkbookName.xlsm", 0, True)
Works!
A vbs file has been written that calls an Excel macro. However, when this vbs file is scheduled using Jenkins, the macro is not executed, though the console shows a success message.
Please suggest if there is some other way for scheduling Excel macro using Jenkins. vbs file code is this:
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Demo\Migration\Compare_Tool_For_Demo.xlsm", 0, True)
xlApp.Application.Run "Compare_Tool_For_Demo.xlsm!OpenSupport2Tool"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Jenkins build command :-
call "C:\Demo\Migration\Start_Report_Tool_For_Demo.bat
Macro operations are executed perfectly when the vbs file is executed manually. However whenever executed from Jenkins, it does not work.(Console shows "SUCCESS" however)
This sounds like a permissioning issue. I have no idea what 'Jenkins' is, but I think you should make sure it has Administrator rights.
Found the solution .
write a powershell file with contents -:
**
$macroBook = ".\Excel.xlsm"
$procedureName = "Macro"
$macroBookFullPath = (ls $macroBook).FullName
$macroProcName = (ls $macroBook).Name + "!" + $procedureName
$excelApp = New-Object -com "Excel.Application"
try {
$excelApp.DisplayAlerts = $false
$excelApp.Workbooks.Open($macroBookFullPath)
$excelApp.Run($macroProcName)
} finally {
$excelApp.DisplayAlerts = $false
$excelApp.Quit()
}
**
You would need powershell plugin for Jenkins . Follow the steps
1. Add build step "Windows Powershell"
2. Write this command - "mkdir C:\Windows\system32\config\systemprofile\Desktop"
3. Execute the powershell file (.ps1) by using "./" as a powershell step ("./Powershell_Test.ps1")
Then go your merry way.
I want to switch between two audio devices connected to my computer (Windows 7 32 bit). I had a look at question, and found nircmd.
Now, I'm able to create two VBS files to switch between the two devices. I was wondering if I could find out what the current active/default sound device is, then I could put everything in one file itself.
My current code is -
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 0, True)
Set WshShell = Nothing
The other file has "Headphones" instead of "Speakers".
This is how I solved it currently. This sucks big time, I know. It doesn't find the current active device, instead stores the information in a text file (which has to already exist! With the current device name in it!). Yes, it's horrible. But considering my knowledge of VBScript and current knowledge of the Windows Registry, both of which are very near zero, this is the best I could come up with!
I'm posting this here, because of a lack of a simple answer anywhere else. If anybody has any better solutions, without using any other programs, I'll be very grateful to know.
Also, if anybody wants to use this code, please change the file paths and names to suit yours.
Dim objFso, objFileHandle, strDisplayString
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set readObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 1)
strDisplayString = readObjFileHandle.ReadLine()
readObjFileHandle.Close
Set writeObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 2, "True")
If StrComp(strDisplayString, "Headphones", vbTextCompare) = 0 Then
'MsgBox "Headphones - switching to Speakers"
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 2, True)
Set WshShell = Nothing
writeObjFileHandle.Write("Speakers")
Else
'MsgBox "Speakers - switching to Headphones"
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Headphones""", 2, True)
Set WshShell = Nothing
writeObjFileHandle.Write("Headphones")
End If
writeObjFileHandle.Close
Background
I'm trying to automate the creation of Virtual Directories based on the location of an existing Virtual Directory and its sub-directories.
Example:
C:\WebSites\Parent\NewVirtualDirectories
Where Parent is a Virtual Directory and NewVirtualDirectories contains any automated Virtual Directories.
Problem
Using the following Code:
Option Explicit
Dim args, strComputer, strVdirName, strVdirPath, objVdir, objIIS, objWebSite
Set args = WScript.Arguments
strComputer = "localhost"
strVdirName = args(1)
strVdirPath = args(0)
Set objIIS = GetObject("IIS://" & strComputer & "/W3SVC/1")
Set objWebSite = objIIS.GetObject("IISWebVirtualDir","Root/Parent")
Set objVdir = objWebSite.Create("IISWebVirtualDir",strVdirName)
objVdir.AccessRead = True
objVdir.Path = strVdirPath
objVdir.AppCreate (True)
objVdir.SetInfo
WScript.Quit
I can create children under Parent, but they show up Directly under the parent. I need them to be in the Sub Folder.
I get: http://localhost/Parent/NewSite
I want: http://localhost/Parent/NewVirtualDirectories/NewSite
I've tried
Set objWebSite = objIIS.GetObject("IISWebVirtualDir","Root/Parent/NewVirtualDirectories")
but NewVirtualDirectories is not a Virtual Directory (an I don't want it to be) so I get an error. I can get the desired effect when I do this manually in IIS manager, but I can't figure out how to automate it.
Any help would be greatly appreciated.
EDIT
For those who are facing similar problems, I found a great resource for VBScript-ing
http://www.cruto.com/resources/vbscript/vbscript-examples/vbscript-sitemap.asp
After doing a lot more digging (trial and error) I was able to figure it out.
By referencing the existing folder as a IISWebDirectory, I was able to select it and then create an application without creating a virtual directory.
Option Explicit
Dim args, strComputer, strVdirName, strVdirPath, objVdir, objIIS, objWebSite
Set args = WScript.Arguments
strComputer = "localhost"
strVdirName = args(1)
strVdirPath = args(0)
Set objIIS = GetObject("IIS://" & strComputer & "/W3SVC/1")
Set objVdir = objIIS.GetObject("IISWebDirectory","Root/Parent/NewVirtualDirectories/" + strVdirName)
objVdir.AccessRead = True
objVdir.AccessScript = True
objVdir.AppFriendlyName = strVdirName
objVdir.AppCreate (True)
objVdir.SetInfo
WScript.Quit