Batch command paste several lines as input via Batch command - batch-file

I am currently writing a script that automatically decrypts a mail that a user pastes.
#echo off
REM create a textfile with the name of the mail
set /p textfileName=What would you like the mail to be named?
REM create contents of the textdocument
#echo On the line below copy the contents of your mail that you received.
set /p textfileContents=
REM write the contents to textfile
#echo %textfileContents% > %textfileName%.txt
#echo Your file has been created and is in the same directory as this batch file.
#echo It is named %textfilename%.txt
REM decrypting the textfile with gnupg
gpg --output %textfilename% --decrypt doc.gpg
My problem is that I want the user to be able to paste a multi line mail that is encrypted. Every time I try the script it only uses the first line that was pasted.
So if I paste:
hQIMA/G0CbVUEcdqAQ/9HwDqyk8xfQDdF/6iogMs7u3eW/6wMTGG8p3RawPKttbU
yduQF6lcF3diMHh2yBU93HAcU0xFL5mysm1AKQGYQSNaB5KheG2hSet80ViQePqy
It only uses:
hQIMA/G0CbVUEcdqAQ/9HwDqyk8xfQDdF/6iogMs7u3eW/6wMTGG8p3RawPKttbU
Is there a ways to paste several lines as an input?

(
SET /p line1=Please enter whatever
SET /p line2=
)<con
ECHO -------------
ECHO %line1%
ECHO -------------
ECHO %line2%
Should do what you want. Windows may object to trying to paste more than one line from the clipboard and you'll probably need to press Enter having pasted the data.

Related

set /p failing to read sftp mapped file

I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)

Copy specific text line with bat file?

Can anyone show me a example of a bat file that copies the first and second line from a txt file..
I need way to have 2 bat files. One copies the first line to clipboard, and a second bat file to copy the second line.
So if I have a txt file that contains:
username
password
I want one bat file to copy the username and the second bat the password..
Could some give me an example of how to do this?
copy first line to clipboard:
<file.txt set /p usr=
echo %usr%|clip
copy second line:
<file.txt (
set /p pwd=
set /p pwd=
)
(where the first set /p pwd= is reading the first line and the second one overwrites the variable with the second line)
or use one batchfile for both and put two lines to clipboard (might or might not work with your application; try it out):
<file.txt (
set /p usr=
set /p pwd=
)
(
echo %usr%
echo %pwd%
)|clip
this is probably easier with just:
clip < file.txt
which copies the whole file content to the clipboard.

Upload text file to FTP in batch file, which has random generated file name

I have a batch file, and I want the output of it (txt) uploaded to my FTP server.
Code:
echo off
set /p line= Input:
echo %line%> C:\Users\Public\Folder\%random%%random%%random%%random%.txt
The question is;
How can I make my batch file automatically upload the generated txt which has a fully random generated name, into my FTP?
You have to save the random filename to a variable, so that the %RANDOM% pseudo-variables are resolved only once.
And then you have to generate the ftp script using that variable:
echo off
set RANDOM_FILE=C:\Users\Public\Folder\%random%%random%%random%%random%.txt
set /p line= Input:
echo %line%> %RANDOM_FILE%
echo open hostname>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo put %RANDOM_FILE%>>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt
Start by saving the filename in a variable, as so:
set FILENAME=C:\Users\Public\Folder\%RANDOM%%RANDOM%%RANDOM%.txt
Then, it should just be a matter of using the variable (FILENAME) in your FTP command.

How can I read a line from .txt file and compare it to user input in CMD?

I'm fairly new to CMD but I keep experimenting and searching a lot about it and I've came to a dead-end.
So my question is this:
Is is possible for a batch file to read a certain line from a .txt file, and then
compare it to some user input? In essence, what I want to create is to have a standart .txt file (which will be holding some information) and a batch file which, once initiated, will be asking the user for some information, and then compare that with a specific .txt file line to see wether the user input matches the info in the .txt. So far, the comparison is what I cannot code. Any help?
Thanks. ;)
You can read the textfile into a variable using SET.
For example, place a file data.txt on the desktop containing the word password. Then put this in your batch file:
#echo off
SET /p txt=<%userprofile%\Desktop\data.txt
IF "%1" == "%txt%" (
ECHO "%1 matched!"
) ELSE (
ECHO "%1 did not match"
)
If you run:
test.bat wrongpassword the output is: "wrongpassword did not match"
However, if you run: test.bat password the output is "password matched!"
Side Note: storing and checking a password like this would be a really really bad idea.
You can do it with looping through your "test.txt" file on your desktop that could look as follows:
data1
data2
data3
data4
data5
data6
data7
data8
data9
And "test.bat" file then:
set /p #var= Add your input here:
cls
ECHO Script initiated!
ECHO %#var%
cd "C:\Users\%USERNAME%\desktop"
:: loop over file line by line
for /f "tokens=*" %%a in (test.txt) do (
:: test if data exist in file
IF %#var%==%%a (
echo data exists in txt file
)
)
pause
Try to type as input: "data4" and you will see that after fourth loop data4 is outputed.

Batch copy text to an echo

So I plan to make a little file downloader in batch and I want to display something like the latest version number. I would like to create a .txt file with something like v1.2 etc and then when you run the batch script, it uses the "echo" command and echoes exactly what it says in the text file. I've tried googling this but no luck. Thanks in advanced!
You can put this in your batch file:
#echo off
pushd "%~dp0"
type "version.txt"
This will echo out the contents of version.txt, which is on the same direction of the normal Batch file.
If you want to go more complex with a save feature telling the program what happened and stuff, do this to save:
(
echo %Version%
echo %log%
) > Save.prgmsav
That saves the version and log variable, this is how you load them:
< Save.prgmsav (
set /p Version=
set /p log=
)
The .prgmsav can be .txt too. The save name can be a variable, as well.

Resources