Is it possible to have one batch file that reads another and gets data such as a password from another? for example
batch file 1:
# echo off
//get data from batch file 2
set /p pass=Password:
if pass == password goto a
if not pass == password goto b
:a
//something that happens if password is good
pause
exit
:b
echo wrong password
pause
exit
batch file 2:
MyPassword
Parameters are passed in batch over the way they are called/started:
bat1.bat:
set /p input= Parameter to pass here:
start "Title here" bat2.bat %input%
bat2.bat
echo Passed value: %~1
The parameters usually have the indexes from 1 to 9 and 0 is "reservered" for the path of the batch-file itself.
Alternative:
You can read the output of en executable using for:
bat1.bat
echo This will be displayed in bat2
bat2.bat
for /f "tokens=*" %%i in ('bat1.bat') do echo %%i
Where the second batch file reads the output of the first one and outputs it. The addition tokens=* is needed as it will then read all the output.
Feel free to ask questions if something is not clear :)
Related
Key Generator
#ECHO OFF
COLOR A
ECHO Generating Key!
choice /d y /t 3 > nul
set /p "genkey"="%random%-%random%-%random%-%random%"
PAUSE
EXIT
Batch 2
COLOR A
#ECHO OFF
set /p base=
if %base% == %genkey% GOTO :ecs
:ecs
PAUSE
EXIT
The way I normally do this is by writing to a file and using SET to recall from the file.
For example:
BATCH FILE 1
echo off
set var1=%Random%-%Random%-%Random%
echo %var1%>temp.log
pause
exit
BATCH FILE 2
echo off
set Var1=nul
if EXIST Temp.log (set /p Var1=<Temp.log && del /Q Temp.log)
echo %Var1%
pause
exit
In this case, if you run the second batch file without running the first one, the output will be "nul". However, if you ran the first batch file before the seccond, the output of the first will be displayed.
You can change %Random%-%Random%-%Random% to whatever text or variable you want.
The program acts like the type function, however with this method it prints the contents of the file to a variable.
One last thing to note is that this method will ONLY read the first line of the file. This is useful where you are transfering numbers, then using that number in an operation. If you want to transfer the whole file, you can use a FOR state ment, but also note, the FOR statement will recall the entire into a singe line.
I am trying to get this script to jump to another section of the script if there is no input from the user.
Down at the if %input%== area.
What I'm trying to do is skip to the section where the script checks for .mp4 files and moves them if they are there. Am I supposed to set a variable or loop for that section? Thanks for any replies
#echo off
echo Checking for youtube-dl updates.
pause
youtube-dl -U
rem Enter the url or urls that you want to download from
set /p input="Enter the url(s) you want to download:"
rem Uses the youtube-dl continue (-c) option to download multiple files if not in a playlist
youtube-dl -c "%input%"
rem pause
if %input%=="" GOTO:EOF
cls
echo Download complete, please wait while files are transfered to appropiate folder
pause
for %%o in (.mp4) do move "*%%o" "E:\Documents\scripts\videos\"
if not exist do echo .mp4 files are no longer in this directory
pause
How about following script? This script waits for 3 seconds while "file.mp4" doesn't exist. It keeps to wait until the file exists. About "filename", you can change for your script.
#echo off
set waittime=3
set filename=file.mp4
:loop
if not exist %filename% (
Timeout /t %waittime% /nobreak > nul
goto loop
)
echo Find %filename%
When doing string comparison in batch you have to make sure, that both parts are equal, which in your case will never happen! In most languages strings have double quotes around them. In batch they usually do not.
To solve your problem enclose %input% in double quotes as well.
Note that it can be useful to do something like "x%input%"=="x" to prevent certain characters like <>|to be at the beginning of the comparison string.
You can check this on your own with these few lines:
#echo off
set /p input="Input something or nothing here "
echo %input%
echo "%input%"
pause
If you are hitting Return without any input you will see that only the bottom one will output "" which is the string you are comparing to.
Basically here's what I want: A batch file that prompts the user to set a variable,
set /p x=
then the batch file writes the variable to a file of some sort (abc.txt) Then later on, in a different batch file, the program retrieves the variable from the text document, and sets it as %x% again for whatever use. If there are any questions, or if I'm not clear enough, please comment, and I will revise. thanks.
In Batch Files, you can redirect input and ouput using < and > respectively.
Input.bat
#echo off
:: Take input and set value to x
set /p "x=: "
:: Print out the value of x to the screen, but redirect this to a text file
Echo %x% >> abc.txt
Echo EOF & Pause & Exit
Read.bat
#echo off
:: Set x to the first line in abc.txt
set /p x=< abc.txt
Echo First Line of abc.txt: %x%
Echo.
:: Set x to last line in abc.txt, incase it is multi-line file
for /f "delims=" %%a in (abc.txt) do (set x=%%a)
Echo Last Line of abc.txt: %x%
Echo.
Echo EOF & Pause & Exit
That should help you understand.
Mona.
I figured out a way that works best for me.
So I have the variable %x%, right? I got it from this:
set /p x=
then I write a mini-batch file.
echo set y=%x% >> abc.bat
then later on in a different script, I can use
call abc.bat
the variable y will be the value that I had in the origional batch script.
Dear StackOverFlow Members,
Please help me with this batch file. I would like to use the answer given from the "SET /P INPUT=%=%" and have it update another batch file permanently.
This is the first batch file that runs to get an answer from the user
#echo off
cls
echo.
echo .................................................................
echo ..... Specify what the name of the Store is, this will send .....
echo ............... alerts to abd#abc.co.za ..............
echo .................................................................
echo.
pause
:option
cls
color 5E
echo.
echo "............ Press 1 to specify what the store name is......"
echo "............ Press 2 to exit the program ................."
echo.
SET /P M=Type from the menu above 1 or 2 then press ENTER:
IF %M%==1 GOTO SEND
IF %M%==2 GOTO EOF
:SEND
cls
color 0A
set INPUT=
set /P INPUT=Enter Store Name: %=%
if "%INPUT%"=="" goto input
echo "You said that the store name is: %INPUT%"
:: Have the user confirm his/her choice
SET /P ANSWER=Is the name correct (Y/N)?
echo You chose: %ANSWER%
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
goto :no
:yes
echo You pressed YES!... The name is updating
goto name
:no
echo You pressed NO!... The program will exit
pause
cls
goto eof
:name
::set /A store=%INPUT%
echo %INPUT% >> notify_support.bat
::Terminate the program
:EOF
As you can see I am struggling to specify where I should "echo %INPUT% >> notify_support.bat". This is code taken from the second batch file
#echo off
call senditquiet -s smtp.gmail.com -port 587 -u rsupp0rt#gmail.com -protocol ssl -p access -f rsupp0rt#gmail.com -t 888#gmail.com -subject "Store ABC" -body "Hello there, There is an issue logged at the store.<br>Best regards."
When the first batch file runs, it updates the second one but just dumps it at the end of the file.
I need the INPUT ECHOed to replace "Store ABC" in the second batch file.
Please assist, I'm rather rusty with batch files.
echo %INPUT% >> notify_support.bat
That line contains >> which means 'dump at the end of the file'. You can use a single > to overwrite the existing file contents. That way, you can re-generate the whole file (which is only 2 lines anyway).
A different solution is to actually parse the exising file and replace that text. You can do that by using for /F ..., which allows you to traverse through the lines of a file. You can then generate a new file, based on the (altered) contents of the existing file. Disadvantage is that this file-parsing method is especially suitable for data files in which each line has the same format with fields and delimiters (like a CSV file). It is less suited for parsing 'complex' files like a batch file or program source file.
try the code:
#echo off
echo Welcome
echo > echo #echo off >> batchfilename.bat
echo > echo echo hello >> batchfilename.bat
echo > echo pause >> batchfilename.bat
it will input the code into the batch file and when you run batchfilename.bat you will get something like:
hello
press any key to continue . . .
Could anybody please tell me a shell command for Windows 7 which would take a file path as argument and return the size of that file - Something like:
fileSize.cmd file.txt
...which will give me 1KB.
One question in SO noted the command echo %~z1, but for this, I have to write a separate batch file and use this command in it. I was thinking of modifying my existing bat file and incorporate this command somehow. My batch file looks like this:
p4 diff //sources/j2cs/output.txt >> diff_out.txt
I have to add above command in the existing bat file to find the file size of diff_out.txt.
You don't need an extra batch file, you could move your filename into %1 with a call to a function or you can use a FOR loop.
call :getFilesize diff_out.txt
echo %fileSize%
exit /b
:getFilesize
set filesize=%~z1
exit /b
Or
for %%A in (diff_out.txt) do set fileSize=%%~zA
another variant:
#echo off
set file=c:\bookmarks.html
%1 %0 :: %file%
set len=%~z2
echo %len%
pause
or with wmic:
D:\>set wql="drive='g:' and filename='function2' and extension='txt'"
D:\>wmic path cim_datafile where %wql% get name,filesize
FileSize Name
621 g:\function2.txt
D:\>
or:
set file=G:\function2.txt
echo set len=%%~z1 >_tmp.bat
call _tmp.bat %file% && del _tmp.bat
echo %len%