i'm making a automatic file writing system that writes random numbers onto a .txt file, I need it to also write .bat, the problem is I'm a newbie a batch!
This is a common problem, I like to call it the autosave, this is the code:
(
echo %YourVariable%
) > Mylittlevariable.sav
What does it do? What it does is when the script reaches this code, it writes the number for %YourVariable% in a .sav file, the .sav can easily be changed to .txt or another format.
I want it to load the variable now! The code:
< Mylittlevariable.sav (
set /p YourVariable=
)
Well I want it to write random numbers! This is a code for random numbers:
(
echo %random%
echo %random%
) > Random#s.txt
So now we get how this works? Now the batch file, before having a command in an autosave batch writer you have to put an echo, just how echo makes a statement in the cmd, it types everything after it in an autosave, too. The code for a basic .bat:
(
echo #echo off
echo title Basic Autosave Batch
echo :LOOP
echo set SUPERVAR=15
echo cls
echo echo Hello, world!
echo echo SUPERVAR: %%SUPERVAR%%
echo pause
echo goto LOOP
) > Mybasicbatch.bat
So, know we know what this does, correct? I hope I helped you!
PS the double amount of percent signs is necessary, if you had the normal amount the code wouldn't be read properly!
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.
Is there a way that i can output(export) a %variable% in a batch file without expressing its value?
For instance, if i want random numbers in output.bat files i could do:
>output.bat(
set var1=%random%
echo var1
pause
)`
Now, if i open the output file, you might think i will find a random number each time a open it, right? Wrong, because when i output the variable it doesn't export the var itself but its value. How to 'solve' this?
I've tried a really arcaic way that works in CMD, but apparently not in the in anything.bat
This:
set test=random
echo %%test%%>output.bat
I would love your help guys, i'm trying to create a simple script and this is the only part that i haven't managed yet. Thank you for your attention.
It's unclear what your desired output.bat should look like.
This will create a file called output.bat with just %random%:
somebatfile.bat
>>output.bat (
set "var1=%%random%%"
echo %var1%
)
.
output.bat
%random%
This will create an output.bat file with two lines, and I think might be what you're looking for.
somebatfile.bat
set "outtxt=set var1=%%random%%"
echo %outtxt%> output.bat
set "outtxt=echo %%var1%%"
echo %outtxt%>> output.bat
.
output.bat
set var1=%random%
echo %var1%
I'm trying to work with batch, but I'm having some bugs that I didn't have before making similar applications.
First issue:
I'm using set /p to prompt for a string, then I save it to a file using
ECHO Myvar>"%systemroot%\name.ini"
That works fine, but when I open the file name.ini there is a space after the string I typed. I built already a batch in the past using this and it didn't happen.
Second issue:
When I load the file to string, it doesn't work. Something happens to the string that if I try to output it using
ECHO %Myvar%
the output is ECHO is off. As I said before, I used this once already in a different batch file and it worked fine.
Example code:
#ECHO OFF
if not exist "%systemroot%\asd.ini" (
set /p asd= COMR:
ECHO %asd%>"%systemroot%\asd.ini"
REM Save the string to a file
) else (
set /p asd=<"%systemroot%\asd.ini"
REM Read the string
ECHO %asd%
pause
)
P.S: I already tried using SETLOCAL EnableDelayedExpansion, but it still doesn't work as expected.
setlocal enabledelayedexpansion
if not exist "%systemroot%\asd.ini" (
set /p asd= COMR:
ECHO !asd!>"%systemroot%\asd.ini"
REM Save the string to a file
) else (
set /p asd=<"%systemroot%\asd.ini"
REM Read the string
ECHO !asd!
)
pause
Stuff in brackets are treated as one line.
I removed Echo Off - hiding messages is not a good idea when you have a problem.
2 Questions:
1) Changing scripts based on input
Basically, say I have a file, like search.html that changes based on what you type in.
Aside from doing
set/p string=What would you like to search for?
echo ^<!DOCTYPE html^> >>file.html
echo ^<html^> >>file.html
echo ^<title^>^</title> >>file.html
echo ^<script language="JavaScript""^> >>file.html
echo string = '%site%'
...
Is there another way to do this?
2) Getting things returned from a file?
I have no example for this. I was simply wondering if you could start a file, use wait, and once it has closed get what was in it?
try this:
#echo OFF &setlocal
(
echo ^<!DOCTYPE html^>
echo ^<html^>
echo ^<title^>^</title^>
echo ^<script language="JavaScript""^>
echo string = '%site%'
)>file.html
2) Getting things returned from a file
#echo OFF &setlocal
FOR /f "delims=" %%a IN (file.html) DO (
ECHO(%%a
)
There are many ways to replace text in a file with something else. You can get input and then replace a MARKER (text like that) with the input text. VBS, Powershell, SED, AWK, and batch can do it.
Your second question is a bit short on detail - but FINDSTR etc can read lines from a file.