BAT script that sends commands to program it starts - windows

So I'm trying to do something like this:
ftp my.ip.add.ress
(user id)
(password)
cd /the/directory
send C:/code/project1/*
bye
I know you can send Windows ftp commands through the command line when you first start it like -send, but the question is can I use a .bat script to act interactively with the program it starts? By default what happens is the first line:
ftp my.ip
Starts ftp, but the subsequent lines never run until ftp exits.
edit
Another way to phrase this question is "Is it possible to use a .bat file to queue lines of text to feed to stdin when stdin is next checked for input?"

As far as I know, the command shell is not multi-threaded, so there is no way for a batch to continue executing while another interactive shell program is executing within it.
You need to move the FTP commands into their own file, and pass that to the FTP program so it can run them as its own internal script.
ftp -s:filename
Specifies a text file containing FTP commands; the commands will run automatically after FTP starts.

Related

Is there a command in Shell scripting for executing .exe file and running commands automatically inside of it? replacing the user interaction

I have a .sh script file that I'm modifying which runs an .EXE file that opens the Windows command line prompt automatically.
This .exe asks the user for an input (name of the file in the folder workspace that it will read)
I want to automate this step in my shell script so my user doesn't have to interact with this, and run the commands automatically
I read a bit about the expect command but I think that is for Linux only.
Can someone help me, I'm pretty new to Shell scripting and I couldn't find any useful information elsewhere.
I'm assuming that your executable accepts command-line arguments. So, here we go.
You can use the "start" command in Windows Shell. For example:
start C:\path\to\program.exe -argument
If you want to make the script wait until the .exe file finishes running before continuing, you can use the "/wait" command:
start /wait C:\path\to\program.exe -argument
IF all of that doesn't work, please try:
start myprogram.exe /command1 /command2 /command3
Hope it helps,

How to execute further batch file commands after running "ftp" commands contains in the batch file itself

I have a .bat document where I do some operations with FTP. I want to end the FTP connection and get to finish with the terminal on the desired directory.
I'm trying this, but when it gets to quit, it stops.
cd %USERPROFILE%\Foilder\project\angular
call ng build
#FTP -i -s:"%~f0"&GOTO:EOF
open connection
user
password
cd httpdocs/project
mdelete *.woff
quit
cd %USERPROFILE%\Foilder\Subfolder
It gives me this message right after doing the quit and never runs the next line.
221 Goodbye.
Ideal scenario, run this last cd command and have the terminal ready on the folder that I want to run it. Thing is that I have to do some uploads to two different FTP servers and it would be great to have the console finishing in the folder where I run "dev.bat" to get it to work
Is it possible to do this?
Your script uses a known hack that allows you to specify ftp commands directly in the batch file.
The %~f0 is replaced with the path to the batch file itself. So the ftp -s:%~f0 runs ftp and tells it to use the batch file itself as ftp script file. You have probably noticed that it results in couple of errors, as ftp fails on the first few lines of the batch file, which are not valid ftp commands (the cd ..., call ... and ftp ...).
Equivalently, the batch file would try to run all commands after ftp ... once ftp finishes, also failing, as those are not valid batch file commands. To avoid that, the hack uses GOTO:EOF to skip the rest of the batch file (EOF=eof of the file).
While you actually want to execute some commands after the ftp. At least the cd command. So do not skip the rest of the batch file. Skip only the ftp commands:
ftp -i -s:"%~f0"&goto AFTER_FTP
(ftp commands)
quit
:AFTER_FTP
cd %USERPROFILE%\Foilder\Subfolder
Note the # before ftp. That (and the GOTO:EOF) are clear signs, that the script you based your batch file on, was designed to start with ftp on the very first line and contain nothing else, but the ftp commands. You have deviated from that.
Alternatively, use some more capable FTP client that allows specifying the commands on its command-line without hacks.
For example with my WinSCP FTP client, you can do:
cd %USERPROFILE%\Foilder\project\angular
call ng build
winscp.com /ini=nul /command ^
"open ftp://user:password#connection/" ^
"cd httpdocs/project" ^
"rm *.woff" ^
"exit"
cd %USERPROFILE%\Foilder\Subfolder
There's a guide for converting Windows ftp script to WinSCP script.

How to script to read user input, then run in background itself even closing terminal in TCSH?

I am looking for a strategy suggestion.
I am very new to Linux shell scripting. Just learning tcsh not more than a month.
I need a script to automatically detects when is the result files are done copied back from a remote server to a folder in a remote machine, then start scp the files back to my workstation.
I do not know in advance when the job will finish run, so the folder could have no result files for a long while. I also do not know when will the last result file done copied back from remote server to the folder (and thus can start the scp).
I had tried crontab. Work fine when I guess correctly, most of the time just disappointing.
So I tried to write a script myself and I have it now. I intend to produce a script that serves me and my colleagues too.
To use the script, the user first need to login to the remote machine manually. Then only execute the script at remote machine. The script first asks for user to input their local machine name and directory where they wish to save the result files.
Then the script will just looping to test when is the total number of files change. When it detected that, which means the first result file is starting to be copied back from the remote server, then it loops again to detect when is the total files size in the folder stop changing, which means last result file is finished copied to the folder. After that it executes scp to send all the result files to the user workstation, at the initially specified directory.
Script works fine but I wish to make the script able to run in background and still running by itself even if the user logout from the remote machine and close the terminal. And also I wish to let the user just type in a simple command in terminal to start the script, something like a simple
./script.tcsh
I tried to run the script by command
./script.tcsh &
but fails, because background process unable to accept user input.
Google and found something called disown, but the command is not found. Apparently the remote machine and my machine does not support this command.
Tried to modify the script to first accept the user input, then attempt to use
cat > temp_script.tcsh << EOF
{rest of my script}
EOF
and then a line of
./temp_script.tcsh &
to try to create another script file and use the first script to initiate the second script in background. Also fail, because cat does not treat $variable as a literal text, it replaces it with values. I have a foreach i(1 2) loop, and the cat command just keep reporting error (missing value of variable i, which is just a counter in foreach loop syntax).
I am out of idea at the moment.
Can anyone enlighten me with some strategy that I can try myself?
The goal is to use only 1 script file, and prompt user for 2 inputs (machine name and directory to save), then no more interaction with user or waiting, and able to run even closing the terminal.
Note: I do not need password to login to remote machine and back.

WinSCP script file works in Windows 10 but not in Windows 7

I want to automate really simple ftp transfers with WinSCP (Example script file shown below. The real file would handle many files, but all simple stuff.)
open ftp://username:password#ftp.site.com/
option confirm off
cd remotedirectory
get file.csv
close
exit
A batch file containing:
winscp.com /script="staging get.txt"
opens a command prompt window and executes correctly in Windows 10, but in Windows 7 the command window opens and then immediately closes, and no files are transferred. WinSCP is in the path in both environments. I assume that a parameter or command is missing from one or the other file, but I don't know what it would be.
I was making a couple of small syntax errors. I couldn't see them because the command prompt window closed almost immediately, but the log file showed me what was happening and it was easy to fix. The lesson is - always create a log file.

Logging the exit of a batch file

So I wrote a tool with some batch commands, nothing specific. In the beginning the user can choose which task to perform thanks to a loop.
In that loop I included the "Q" option, as to quit the batch file. When this happens, it gets written to a logfile to check when the user started the script(s), and when it ended.
The issue is this only happens if the user actually quits/exits with Q. If (s)he quits by just closing the batch file, this won't be logged.
In short: how can I record when the user has quit the batch file without using the build-in function?
a batch file can't receive the "exiting"-event. What you can do is:
Make a launcher.bat file, that starts the original (yourfilename).bat file with:
start /wait (yourfilename).bat
the launcher.bat file will now wait until you close the second (yourfilename).bat file. place your log-information on the next line of launcher.bat
convert launcher.bat to launcher.exe using bat to exe converter (and make it invisible).

Resources