I believe I've replicated the stachoverflow recommended code to sort a .txt file using VBA in Excel. The following is the relevant code from my program. It processes this code without error but does not produce the sorted output file. The IntualityInputRealTimeFIFOTable file is produced correctly. Can't figure out what I'm doing wrong.
Public MyCommand As String
.
.
IntualityInputRealTimeFIFOTable = WorkbookPath & "IntualityInputRealTimeFIFOTable.txt"
IntualityInputRealTimeFIFOTableTemp = WorkbookPath & "IntualityInputRealTimeFIFOTableTemp.txt"
.
.
Close #3
MyCommand = "Sort IntualityInputRealTimeFIFOTable /O IntualityInputRealTimeFIFOTableTemp"
Shell MyCommand, vbHide
I want to open a cmd.exe and then execute a few lines of code.
I searched the web for some examples.
Code I tried modifying:
strToPrint = "Hello World!"
Shell "cmd.exe /K echo " & strToPrint, vbNormalFocus
I found How to write message to command window from VBA?
I tried multiple lines of coding, but the lines are executed in different command windows:
Sub CMD_VBA_Script()
Shell "cmd.exe /K echo Hello World!", vbNormalFocus
Shell "cmd.exe /K color 0a", vbNormalFocus
End Sub
I understand when I call the Shell two times, that it will execute two times.
My goal is to call the following script from VBA:
#echo off
title Matrix
color 0a
mode 1000
:a
echo %random%%random%
goto a
How can I execute multiple lines of code from VBA in command prompt?
MyFile = "C:\cmdcode.bat"
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, "#echo off"
Print #fnum, "title Matrix"
Print #fnum, "color 0a"
Print #fnum, "mode 1000"
Print #fnum, ""
Print #fnum, ":a"
Print #fnum, "echo %random%%random%"
Print #fnum, "goto a"
Close #fnum
' Run bat-file:
Shell MyFile, vbNormalFocus
' optional, remove bat-file:
Kill "C:\cmdcode.bat"
So in short. You need to create a bat-file that you run.
If you don't need the bat-file after it's done you can delete it with Kill
You can write something like this -
Call Shell("cmd.exe /c "cd C:\Users\username\local\temp\" & " && temp.vbs" & " && mkdir newfolder")
This executes 3 lines of command:
Change directory
Execute a vbs file
Make a new folder
It looks like you want to execute a Command Prompt Batch file - batches are stateful, so simply executing each line separately is not going to have the same effect as executing the batch as a whole.
Two other alternative approaches involve faking batch execution by instructing an interactive instance of cmd.exe to execute commands as-they're-entered by some automated process: either sending window messages, or piping into the cmd process' stdin stream. I do not recommend either of these approaches because of their inherent flakiness (i.e. dependency on undocumented behavior)
So the best approach would be to just execute a batch file as it's intended - you would need to write the batch to a temporary file first, and then execute it:
Using the code from here: https://support.microsoft.com/en-us/kb/195763
Dim tempFileName As String
tempFileName = CreateTempFile("SomeBatch.cmd")
WriteToBatchFile( tempFileName ) ' you will have to write to the temp batch file yourself here
Shell "cmd.exe /c """ & tempFileName & """", vbHide, True ' Run the batch file, then cmd.exe will terminate. The Shell function will block until cmd is closed
Kill tempFile ' delete the temp batch file
Use "cmd.exe /K" option, and '&' to connect multiple commands.
In C:\Users\%USERNAME%, create test.bat with two simple commands:
echo %PATH%
dir
Here is the complete sample:
Sub Demo_Multi_Commands()
Cmd1 = "cd " & Environ("USERPROFILE")
Cmd2 = "test.bat"
Connector = " & "
Commands = "cmd.exe /K " & Cmd1 & Connector & Cmd2 & Connector & "systeminfo"
Debug.Print Commands
pid = Shell(Commands, vbNormalFocus)
End Sub
Environment: Windows 10 Enterprise, VBA 7.1
I want to open a cmd utility placed at C:\Users\123456\Desktop\Automation\Cucumber\ansi160\x64\ansicon.exe and execute below two DOS commands using VB script.
Commands,
1.CD C:\Users\123456\Desktop\Automation\Cucumber\ruby-1.9.3-p551-i386-mingw32\ruby-1.9.3-p551-i386-mingw32\bin 'change the directory
2.cucumber C:\Users\123456\Desktop\Automation\Cucumber\FeatureFiles\Lib_Max_Create_Lead.feature 'execute a script
Code i am using is -
Set owshell = CreateObject("Wscript.Shell")
cmd0 = "C:\Users\123456\Desktop\Automation\Cucumber\ansi160\x64\ansicon.exe" 'for switching the cmd window
cmd1 = "cd C:\Users\123456\Desktop\Automation\Cucumber\ruby-1.9.3-p551-i386-mingw32\ruby-1.9.3-p551-i386-mingw32\bin" 'for changing the directory to ruby set up
cmd2 = "cucumber C:\Users\123456\Desktop\Automation\Cucumber\FeatureFiles\Lib_Max_Create_Lead.feature" 'for executing a script
'Execute the command
owshell.Run "%comspec% /K pushd " & "&" & cmd0 & "&" & cmd1 & "&" & cmd2
When i execute the above statement from a VBS file,its swtiching the window using cmd0,but the second(cmd2) & third(cmd3) commands are not getting executed.Can some one help me to correct the mistake?
Thanks in Advance
Sona Shetty
I want to extract text from the excel file so i used xlsx2csv command
the text extracted does not give me sheetname
I have used command as :
/usr/bin/xlsx2csv #{excel_name}.xls >> #{excel_name}.txt
Can we get sheetname from using xlsx2csv??
Try:
xlsx2csv -s 0 ${excel_name}.xlsx >> ${excel_name}.txt
From man xlsx2csv:
-s SHEETID, --sheet=SHEETID
Sheet to convert (0 for all sheets).
Sorry new to coding Applescript so any help is appreciated.
I'm trying to create a script that pastes text from clipboard into the middle of a telnet command. The output needs to be in the same window and look kind of like this:
I8,A,001
Q102,024
q448
rN
S4
D15
ZT
JF
O
R71,0
f100
N
B264,65,2,UA0,2,4,56,B,"100000000045"
A203,82,2,1,2,2,N,"xxxxx"
P1
The quoted 12 digit number on line 13 is what I need to insert.
This is what I've coded so far but it's not working:
tell application "Terminal"
do script "telnet xxx.xxx.xx.xx xxxx"
delay 1
do script "I8,A,001" in window 1
do script "Q102,024" in window 1
do script "q448" in window 1
do script "rN" in window 1
do script "S4" in window 1
do script "D15" in window 1
do script "ZT" in window 1
do script "JF" in window 1
do script "O" in window 1
do script "R71,0" in window 1
do script "f100" in window 1
do script "N" in window 1
do script "B264,65,2,UA0,2,4,56,B,\""
tell application "System Events"
tell application process "Terminal" in window 1
keystroke "v" using {command down}
end tell
keystroke "\""
keystroke return
do script "\"A203,82,2,1,2,2,N,\"xxxxx\""
do script "P1"
keystroke return
end tell
end tell
As soon as I try to use Command V to paste it exits the Terminal window and pastes whats on the clipboard on the script instead and it wont let me tell it to stay in Terminal window 1.
You do not need to use command-v to get the clipboard contents into your Terminal window. Applescript can get the clipboard and then you just add it to the other part of your string before "do script". Something like this works... of course you don't need the first line of the code because the clipboard should already have that value.
set the clipboard to "100000000045"
set t1 to "B264,65,2,UA0,2,4,56,B,\""
set t2 to the clipboard
set t to t1 & t2 & "\""
do script t in window 1
This is what I ended up using to get it to work in case anyone else needs it:
tell application "Terminal"
do script "telnet xxx.xxx.xx.xx xxxx"
delay 1
do script "I8,A,001" in window 1
do script "Q102,024" in window 1
do script "q448" in window 1
do script "rN" in window 1
do script "S4" in window 1
do script "D15" in window 1
do script "ZT" in window 1
do script "JF" in window 1
do script "O" in window 1
do script "R71,0" in window 1
do script "f100" in window 1
do script "N" in window 1
set t1 to "B264,65,2,UA0,2,4,56,B,\""
set t2 to the clipboard
set t to t1 & t2 & "\""
do script t in window 1
do script "A203,82,2,1,2,2,N,\"xxxxx\"" in window 1
do script "P1" in window 1
end tell
Thanks for the help regulus!