I want to execute a Code which is inside text file - batch-file

I have a Code which passes dates arguments to a visual basic code which is inside excel file.Code inside the Date.txt file is pasted below:
`Set objExcel = CreateObject("Excel.Application")`
'With objExcel'
'.Workbooks.Open "C:\emailfetch\FetchEmails.xlsm"'
'.Visible = True'
'.Run "FolderTraverse",cLng(DateSerial(2014,3,1)),cLng(DateSerial(2014,3,4))'
'.ActiveWorkbook.Close True'
'.Quit'
'End With'
I need to execute the above code from execute.bat file. Execute.bat file consists 2 other files .Codes need to executed
In Steps
1.Date.txt
2.makezip.vbs
3.mailsend.cmd.
Can anybody helps how to execute the code which is inside Date.txt with the help of batch file execute.bat

You can do this two ways. Both require you to make 2 files.
1: Using a windows scripting host framework xml wrapper. Which allows you to execute windows scripts regardless of language via command prompt.
scriptrollout.wsf
<job>
<script language="VBScript" src="data.txt"/>
<script language="VBScript" src="makezip.vbs"/>
</job>
batchfile.bat
cscript.exe scriptrollout.wsf
mailsend.cmd
2: Using the ExecuteGlobal Command
ExecGlob.vbs
Function Include(vbs)
Dim fso, f, s : Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(vbs) : s = f.ReadAll()
f.Close : ExecuteGlobal s
End Function
Include "Date.txt"
Include "makezip.vbs
batchfile.bat
cscript.exe ExecGlob.wsf
mailsend.cmd

Related

how to concatenate two variables when one has spaces in it in AzureDevops batch file

I have this input in my ps script. I am passing $(Build.ArtifactStagingDirectory) system variable to get the path in the input %1. The path has spaces i.e. E:\Build Agents\Agent2\_work\15\a. I need to concatenate this with other path but I am not able to do so.
set BuildDrop=%1
set Directory=%BuildDrop% + "\adapters\bin"
This is my output, which is incorrect as Directory should be something like E:\Build Agents\Agent2\_work\15\a\adapters\bin. How to solve this?
set BuildDrop="E:\Build Agents\Agent2\_work\15\a"
set Directory="E:\Build Agents\Agent2\_work\15\a" + "\adapters\bin"
My task is like this in my build pipeline
Task : Batch script
Description : Run a Windows command or batch script and optionally allow it to change the environment
Version : 1.1.10
I found the solution for this. Since my %1 had space I needed to remove apostrohphe before assigning. I did it using ~ variable. working code looks like this.
set "BuildDrop=%~1"
set "Directory=%BuildDrop%\adapters\bin"
You could try this :
set BuildDrop=%1
set Directory= %BuildDrop%\adapters\bin
echo %Directory%
Sample Output
This is the concatenation code.
%BuildDrop%\adapters\bin
In my above sample I am trying to concatenate C:\Users\svijay\Desktop & \adapters\bin using the batch script
And the output : C:\Users\svijay\Desktop\adapters\bin
UPDATE :
#echo off
set BuildDrop=%1
set Directory= %BuildDrop%\adapters\bin
set Directory= %Directory:"=%
echo %Directory%
If you have space, you could provide the "" to provide input.
In your code you could remove the double quotes.
%Directory:"=% - Removes the Double quotes from the string literal.
Sample output :
You may use this to create Azure DevOps variable containing your path
powershell: |
$directory = $(Build.ArtifactStagingDirectory)
$newDirectory = $directory + "\adapters\bin"
Write-Host "##vso[task.setvariable variable=testvar]$newDirectory "
and if you need set variable in powershell script
$BuildDrop=$args[0]
$Directory=$BuildDrop + "\adapters\bin"
Here you have an article how to use parameters in powershell.
We can use powershell task in the Azure DevOps to concatenate two variables.
Sample:
Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)"
Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)\adapters\bin"
Result:
In addition, I found a similar issue, please also check it.
Update1
Use power shell script in the Azure DevOps pipeline
$testpath1 = "E:\Build Agents\Agent2\_work\15\a"
$testpath2 = "\adapters\bin"
Write-Host "path is $($testpath1)$($testpath2)"
Result:
Use local power shell
Result:
Update2
.bat file
set testpath1=E:\Build Agents\Agent2\_work\15\a
set testpath2=\adapters\bin
set newpath=%testpath1%%testpath2%
echo %newpath%
Pipeline result:

Paste of clipboard to text file with batch file [duplicate]

For example, I can copy a file to the clipboard like this:
clip < file.txt
(Now the contents of file.txt is in the clipboard.)
How can I do the opposite:
???? > file.txt
So that the contents of the clipboard will be in file.txt?
If it would be acceptable to use PowerShell (and not cmd), then you can use Get-Clipboard exactly as you were looking for.
Get-Clipboard > myfile.txt
The advantage of this method is that you have nothing to install.
Note: In place of clip you can use Set-Clipboard that has more options.
Note 2: If you really want to run it from cmd, you can call powershell as in the following example powershell -command "Get-Clipboard | sort | Set-Clipboard".
You can use the paste.exe software in order to paste text just like you are describing.
http://www.c3scripts.com/tutorials/msdos/paste.html
With it you can do:
paste | command
to paste the contents of the windows clipboard into the input of the specified command prompt
or
paste > filename
to paste the clipboard contents to the specified file.
Clarifying an answer from #Kpym:
powershell -command "Get-Clipboard" > file.txt
This directly answers the question without using a 3rd party tool.
To get contents of clipboard
From win cmd:
powershell get-clipboard
or (via a temp file from HTML parser) on cmd:
echo x = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") > temp.vbs
echo WScript.Echo x >> temp.vbs
cscript //nologo temp.vbs
Output may be redirected to file.
Using the doskey macro definition feature, you can do:
doskey unclip=(powershell -command "Get-Clipboard") $*
Then (e.g.)
dir/b | clip
unclip | sort/r
I have a pair of utilities (from before the Clip command was part of windows) available on this page:
http://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista
There are two utilities in there, Clip2DOS and DOS2Clip. You want Clip2DOS:
Clip2DOS Copyright 2006 Thornsoft Development
Dumps clipboard text (1024 bytes) to stdout.
Usage: Clip2Dos.exe > out.txt
Result: text is in the file.
Limits: 1024 bytes.
License: Free, as in Free Beer!
http://www.thornsoft.com/dist/techsupport/dos2clip.zip
DELPHI SOURCE INCLUDED!
And hey, here it is (Clip2DOS.dpr) :
{Clip2DOS - copyright 2005 Thornsoft Development, Inc. All rights reserved.}
program Clip2Dos;
{$APPTYPE CONSOLE}
uses
Clipbrd,
ExceptionLog,
SysUtils;
var
p : Array[0..1024] of Char;
begin
try
WriteLn('Clip2DOS Copyright 2006 Thornsoft Development');
Clipboard.GetTextBuf(p,1024);
WriteLn(p);
except
//Handle error condition
on E: Exception do
begin
beep;
Writeln(SysUtils.format('Clip2DOS - Error: %s',[E.Message]));
ExitCode := 1; //Set ExitCode <> 0 to flag error condition (by convention)
end;
end
end.
Pasteboard is another option. It can also work from WSL. First, install via choco:
choco install pasteboard
then the command is simply
pbpaste.exe > file.txt
And that works from cmd and wsl bash.
Well, from a million years ago, we did something like this:
type con > filename.txt
... and then you perform your paste operation (Ctrl-v, middle-click the mouse, or choose Edit->Paste from them menu) into the waiting prompt. This will capture the stdin buffer (the console device, named 'con'), and when an end-of-file is received, it will write the contents to the file. So, after your paste, you type 'Ctrl-z' to generate an EOF, and the type command terminates, and the contents of your paste buffer (the clipboard) are captured in 'filename.txt'.
There are third party clip commands that work bidirectionally.
Here's one:
CLIP - Copy the specified text file to the clip board
Copyright (c) 1998,99 by Dave Navarro, Jr. (dave#basicguru.com)
Here is the CLIP program by Dave Navarro, as referred to in the answer by #foxidrive. It is mentioned in an article here: copying-from-clipboard-to-xywrite
A link to the download, along with many other resources is on this page: http://www.lexitec.fi/xywrite/utility.html
Here is a direct link to the download:
"DOWNLOAD Clip.exe Copy from and to the clipboard by Dave Navarro, Jr."
It may be possible with vbs:
Option Explicit
' Gets clipboard's contents as pure text and saves it or open it
Dim filePath : filePath = "clipboard.txt"
' Use the HTML parser to have access to the clipboard and get text content
Dim text : text = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")
' to open
If Not IsNull(text) then
Dim WshShell, somestring, txFldr2Open
Set WshShell = WScript.CreateObject("WScript.Shell")
txFldr2Open = "C:\Users"
txFldr2Open = text
somestring = "EXPLORER.exe /e," & txFldr2Open ', /select
WshShell.run somestring
Set WshShell = Nothing
else
msgbox("Empty")
end if
' Create the file and write on it
msgbox(text)
Dim fileObj : Set fileObj = CreateObject("Scripting.FileSystemObject").CreateTextFile(filePath)
fileObj.Write(text)
fileObj.Close
You can use cbecho, a program I wrote in plain C. It will send any clipboard text to stdout, from where you can pipe it to other programs.
I am not sure if this command was not supported at that time or not, but it surely does work
clip > file.txt
This dirty trick worked for my needs, and it comes with Windows!
notepad.exe file.txt
Ctrl + V, Ctrl + S, Alt + F, X

Automate WinSCP to get listing of files in remote directory

I've read a lot about automating WinSCP, but some of it I had trouble understanding because it presumed knowledge of other things, like .NET assembly, PowerShell, etc.
I'm wondering if, speaking strictly in VBScript and batch file file type of lingo, once I've downloaded the portable winscp.exe, how to simply open a remote site, give a user name and password, and download a list of the files in a particular directory. FTP protocol only.
There's an example for using the Session.ListDirectory from VBScript:
<job>
<reference object="WinSCP.Session"/>
<script language="VBScript">
Option Explicit
' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Ftp
.HostName = "ftp.example.com"
.UserName = "user"
.Password = "mypassword"
End With
Dim session
Set session = WScript.CreateObject("WinSCP.Session")
' Connect
session.Open sessionOptions
Dim directoryInfo
Set directoryInfo = session.ListDirectory("/remote/path")
Dim fileInfo
For Each fileInfo In directoryInfo.Files
WScript.Echo fileInfo.Name & " with size " & fileInfo.Length & _
", permissions " & fileInfo.FilePermissions & _
" and last modification at " & fileInfo.LastWriteTime
Next
' Disconnect, clean up
session.Dispose
</script>
</job>
Other than that:
download the WinSCP .NET assembly package and extract it along with the script.
register the assembly for COM. Typically like:
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb
%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb
Run the script (list.wsf) like:
cscript list.wsf
You can of course also just run winscp.com scripting like:
Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec("winscp.com /command ""open ftp://username:password#ftp.example.com/"" ""ls /remote/path"" ""exit""")
WScript.Echo(exec.StdOut.ReadAll())
For more details on this approach see guide to advanced FTP scripting from VB script.

Creating a shortcut for a exe using a batch file

I know a topic already exists like that but I do not want to use a VB script.
I would hope you can create a shortcut using a command line in DOS.
Please post some example that would be great.
Thanks!
AA
You can't create a shortcut in a .bat file without invoking an external program.
However, every version of Windows since Win2k has a built in scripting language called Windows Script Host
Here is a small WSH script that I wrote a few years ago that can be called from a .bat file,
just save this text as shortcut.wsf, it contains useage information in the script.
<package>
<job id="MakeShortcut">
<runtime>
<description>Create a shortcut (.lnk) file.</description>
<named
name = "Target"
helpstring = "the target script"
type = "string"
required = "true"
/>
<named
name = "Args"
helpstring = "arguments to pass to the script"
type = "string"
required = "false"
/>
<unnamed
name = "basename"
helpstring = "basename of the lnk file to create"
type = "string"
required = "false"
/>
</runtime>
<script language="JScript">
if ( ! WScript.Arguments.Named.Exists("Target"))
{
WScript.Arguments.ShowUsage();
WScript.Quit(2);
}
target = WScript.Arguments.Named.Item("Target");
WScript.Echo("target " + target);
args = WScript.Arguments.Named.Item("Args");
WScript.Echo("args " + args);
base = WScript.Arguments.Unnamed.Item(0);
WScript.Echo("base " + base);
fso = WScript.CreateObject("Scripting.FileSystemObject");
//path = fso.GetParentFolderName(WScript.ScriptFullName);
path = fso.GetAbsolutePathName(".");
WScript.Echo("path = " + path);
Shell = WScript.CreateObject("WScript.Shell");
short = fso.BuildPath(path,base);
if ( ! fso.GetExtensionName(base))
short = short + ".lnk";
link = Shell.CreateShortcut(short);
link.TargetPath = fso.BuildPath(path, target);
if (args != null && args != "")
link.Arguments = args;
else
link.Arguments = base;
//link.Description = "Sound Forge script link";
//link.HotKey = "ALT+CTRL+F";
//link.IconLocation = fso.BuildPath(path, target) + ", 2";
//link.WindowStyle = "1"
//link.WorkingDirectory = path;
link.Save();
</script>
</job>
</package>
run it without any arguments to get useage
c:\> shortcut.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Create a shortcut (.lnk) file.
Usage: shortcut.wsf /Target:value [/Args:value] [basename]
Options:
Target : the target script
Args : arguments to pass to the script
basename : basename of the lnk file to create
mklink /D c:\vim "C:\Program Files (x86)\Vim"
More Info Here
And Cygwin's ln - s
http://en.wikipedia.org/wiki/Symbolic_link#Cygwin_symbolic_links
Creating a shortcut in the .lnk format is basically impossible from a batch file without calling an external program of some kind. The file spec can be found here, and a quick glace will explain.
Creating a .url format shortcut is quite easy as the format is a simple text file. The spec can be found here. This format has a few disadvantages, but may accomplish your goal.
you can get shortcut.exe from the resource kit.
It can now be done with Powershell, which arguably sucks somewhat less than VBscript. And powershell can be called from a .bat / .cmd file:
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\mylink.lnk'); $s.TargetPath='C:\Path\to\your.exe'; $s.Save()"
See also here for another example: https://ss64.com/nt/shortcut.html#e
See also

How to call a SQL script from within another SQL script?

i want to call a series of .sql scripts to create the initial database structure
script1.sql
script2.sql
etc.
is there any way of doing this without sqlcmd or stored procedures or any other kind of code that is not sql ?
just inside a .sql file.
you could try this:
exec master..xp_cmdshell 'osql -E -ix:\path\filename.sql'
osql must be in the path, the full filename must be known, and logins have to be set up correctly (options -E or -U)
Sure. Just create a little app that pulls in all the .sql files you want and executes them. Do it in VB.NET as follows:
Sub ExecuteSqlScript(FilePath As String)
Dim Script As String
Dim FileNumber As Integer
Dim Delimiter As String
Dim aSubscript() As String
Dim Subscript As String
Dim i As Long
Delimiter = ";"
FileNumber = FreeFile
Script = String(FileLen(FilePath), vbNullChar)
' Grab the scripts inside the file
Open FilePath For Binary As #FileNumber
Get #FileNumber, , Script
Close #FileNumber
' Put the scripts into an array
aSubscript = Split(Script, Delimiter)
' Run each script in the array
For i = 0 To UBound(aSubscript) - 1
aSubscript(i) = Trim(aSubscript(i))
Subscript = aSubscript(i)
CurrentProject.Connection.Execute Subscript
Next i
End Sub
Example from: http://snipplr.com/view/3879/run-sql-script-from-external-file/
There's no reason to exclude stored procedures. You don't need to include "any other kind of code that is not sql", plus
EXEC someothersp
which will be required (or its equivalent) in any other solution.
What's your reason for excluding them? I would sure think it beats writing code in yet another language.

Resources