BAT file programming assistance : how to make a response in a bat file copy to another part OR to clipboard? - batch-file

My apologies if the title was confusing. It should be fairly basic, but I cannot find a way to do this.
What i'm wanting to do is have a bat file prompt for an answer to a question and then surround that answer with another piece of code and either copy to the clipboard or after the connection.
Maybe it will make more sense if i give the code.
#echo off
set /p input ="what server would you like to connect to? (example srv02) :"
echo Myhome.%input%.com
pause
c:\program files\putty
the echo gives the correct response, but i would like to see if there is a way to paste this past the c:\program files\putty to connect to a server.
OR if there is a way to copy that response to the clipboard so the bat file would open putty (which it does now) and then you could just paste that response.
Or am i going about this the wrong way? thanks for the help!!

I'm not a PuTTY user, so I can't be sure - But I believe you are simply trying to pass the the server host that you want to connect to. I think the following will do what you asked:
"c:\program files\putty" %input%
or
"c:\program files\putty" Myhome.%input%.com
However, there are probably other command line options you need to really accomplish your goal.
You should learn the PuTTY command line arguments and options. I suspect a web search could find some tutorials or examples to get you started.

Perfect! That is what i ended up doing after a few more hours of research.
I came up with two different types of scripts as my company wants us to use a specific set of defaults for different servers.
#echo off
set /p input="what server would you like to connect to? : "
echo myhome.%input%.com| clip
echo ----
echo myhome.%input%.com has been copied to your clipboard,
echo you can now paste into the host name box in putty
echo -----
echo Press enter to open puTTY
echo -----
Pause
start c:\users\public\apps\putty.exe
Ended setting it to open putty instead of pasting because it should be defaulted settings, but not everyone kept it that way, of course.
Thanks for the assistance though!

Related

Windows CE 5 batch IF EXISTS

I'm trying to run a batch file on a WINCE device that will ping another networked device and capture the results in a text file.
However, whenever I try to execute the section that checks to see if the log file already exists, I get this error:
IF: incorrect command syntax
Can anyone shed some light as to why?
Here is the bit that creates the text file to log the results. It may or may not be worth mentioning, but it works fine on a windows 7 PC:
SET DATETIME=%date:~0,2%\%date:~3,2%\%date:~6,4% 0%time:~1,1%:%time:~3,2%:%time:~6,2%
if NOT exist %DATEVARIABLE%-log.txt type "New Log" > %DATEVARIABLE%-log.txt
Thanks
Doug
Something looks like this :
#echo off
SET "DATETIME=%date:~0,2%-%date:~3,2%-%date:~6,4%_%time:~1,1%-%time:~3,2%-%time:~6,2%"
echo "%DATETIME%"
pause
if NOT exist "%DATETIME%-log.txt" echo "New Log" > "%DATETIME%-log.txt"
pause

Installing exe through batch file

I am trying to install through batch file..
ECHO OFF
ECHO Installing MySoftware . . .
"%~dp0\MySoftware.exe" /S /v/qn"UPGRADEADD=link goes here"
pause
but it fails to install.
Not much info to go on. What you have will not work if executed from a UNC drive and may not work if you 'Run as administrator' because the current directory gets changed. Try this. Of course that may not fix it and further details would be nice.
#ECHO OFF
PUSHD "%~dp0"
ECHO Installing MySoftware . . .
"MySoftware.exe" /S /v/qn"UPGRADEADD=link goes here"
Adding to my answer based on comments provided.
Presumably your bat file is in the same folder as MySoftware.exe. If it takes that long, it sounds like the install is working. Try doing
"MySoftware.exe" /?
That may give you a help screen to tell you more about the arguments beng passed. Also, try what you are now doing without the /S (which probably specifies a "silent" install... which is why you don't see anything.
PART 1 - If you want to create a "Setup" File in batch.
Maybe it works, but this is will be very hard to you for done this program.
Let's call the EXE File "Game1:
I will recommend you to take all the Game1 file's code (Maybe you can use the program Notepad++ for do this) after you taked Game1's code do this like i writing here
Let's say that the code of Game1 is:
ABC
Copy the code, then go to the batch file.
The "Setup" file of Game1 HAVE to come with a empty EXE file.
You can make a empty EXE file with notepad - just save the file as:
Name.exe
Then you doing at the batch file script this thing:
set %something%=ABC
After you done this you adding this to the batch script:
Echo %something% >> Name.exe
Don't forget to name the EXE file at the name of the program / game.
And now, if this message didn't help to you, maybe you need to make a EXE from batch file.
PART 2 - If you want to make an EXE file of batch file.
Open the start menu of Windows and search this:
IExpress
Don't let the computer search for you the full name, its working only if you wtiting the full name.
After you search IExpress, click on "Activate Command".
Click on Next, Don't change the first options.
Click on "Extract files only" and click on Next.
Name the EXE program and click Next.
Stay on "No prompt." and continue.
Now you can display a program License. if you want do a txt file and choose the display option.
Add batch files and click Next.
click on the option you want and click Next.
If you want a finish message, click on display message and write the message.
Here browse where the EXE will be and choose your options, click Next.
click Next.
Wow that's was super-long! Hope I helped you!

Stop Batch file if its being run from a drive

Hi I want to stop a batch file if it is being run from a particular drive. I have tried somehting like this, it doesn't work though. I would appreciate it if someone has a better idea.
if %CD%=="^.*C:\" (goto :CDrive)
Where :CDrive is an error message saying that the user is trying to run it from the wrong drive.
Cheers
Chris
You can use a substring to check:
if "%CD:~0,2%"=="C:" goto CDrive
Another option might be that you just explicitly set the drive you're expecting:
pushd X:
or use full paths instead of relative ones.

Answer a question in a popup window

I have a batch file that I want to use to exicute a .exe file. The exe runs but it pops up a windows needing some information. How can I file in the popup with the correct information? The servername.domain is the server address that needs to be put in the popup. I thought that this was the syntax to populate the popup but its not working. Any help would be great.
SetupCodeGroup.exe servername.domain
i have found that you can not do what you want to do with a batch file. when i ran into a simaler problem i used vbscript sendkeys method. if you google that you should be able to do what you want.
and for your case i would have a batch file as a bass to run the vbscrit program you write to fill in the info in the .exe
somthing like this for the batch
(if you do copy and past this be sure to take out the comments (//) )
#echo off
set reapet=100 // just setting varibles to be used in the delay loop
set con=1
set time=0
start (your .exe) //starts your EXE
:begin
set /a time=%time%+%con%
if %time%==%reapet% goto:startvb // this makes a delay so that we can be sure your
goto:begin // computer fully started the .exe :)
:startvb
cls
start your (vbscript) // this starts your vbscript
cls
echo Done // tells you the batch file is done
pause
i hope this helps.

Gracefully trap error on start cmd

On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.

Resources