Send values to csv with batch file - windows

I have a .bat file that takes a list of options, the user selects an option and the code goes to a .csv file. The problem is that it isn't filling the cells. I have other menus that are built the same way and work, but this one won't.
REM Prompt for Machine Type
echo Choose Machine Type
echo 1. Fat Client
echo 2. Thin Client
echo 3. Tracker Board
echo 4. Laptop
echo 5. Dock
echo.
SET /p mt=Type option:
if "%mt%"==1 set machinetype='Fat Client'
if "%mt%"==2 set machinetype='Thin Client'
if "%mt%"==3 set machinetype='Tracker Board'
if "%mt%"==4 set machinetype='Laptop'
if "%mt%"==5 set machinetype='Dock'
set machinetype=
FOR /F "tokens=1 delims='|'" %%A in ("%machinetype%") do SET machinetype=%%A
echo Machine Type: %machinetype%
SET file="> %~dp0addresses.txt"
echo Machine Type: %machinetype% >> %file%
echo %machinetype%, >> hardware-audit.csv

Related

how can I make a batch file create a batch file that can still contain percentage values

I'm trying to create a batch file that creates a batch file, but as it seems I can't get the first batch file to also send over percentage value's (e.g. %example%). How do I make the first batch file, which creates the second batch file ? But the generated file should contain % ? Also if you find any other errors in my script please correct it :)
Also while I'm here, as you can see I've put an "example, please help!" underneath the ":prep" I can't seem to figure out the ">nul" thing. No matter how many "^" I put it won't save to the last sector (which is "gnome.bat"). It will however save to the "setup.bat" but not any further. Please also help me with that!
The error is under ":Prep"
here's my full script: (not even close to being done with the script...)
#echo off
title Annoying Menu
mode 150
color a
:begin
cls
echo Welcome to "Annoying Menu"
echo This menu is made to prank your friends
echo All pranks that you make on your friends are completely your own responsibility
echo Please be aware that this menu contains what could be consideret "Virus"
echo No harm will however be done to either your, or your freinds pc
echo Please read through all the instructions before use, of the menu...
echo ---------------------------------------------------------------------------------------
echo 1. Please select a password to cancel the prank, by pressing "3" (only numbers)
echo 2. Please make sure that the setup files are ready to be transferred, by pressing "1"
echo 3. Now you are ready to send off the setup file to one of your freinds
echo ---------------------------------------------------------------------------------------
echo type "1" to create setup file
echo type "2" to prepare setup files for use
echo type "3" to download it on your own pc
echo type "4" to select password to cancel the prank (please only use numbers)
echo type "5" to test password
echo type "exit" to close the menu
echo ---------------------------------------------------------------------------------------
echo.
set /p opt= Option:
if %opt%==1 goto create
if %opt%==2 goto prep
if %opt%==3 goto UD
if %opt%==4 goto pass_select
if %opt%==5 goto testpass
if %opt%==exit goto end
cls
echo please enter a valid number to continue...
echo.
pause
goto begin
:create
break>"C:\Users\%Username%\Desktop\setup.bat"
echo.
echo setup file created, please prepare the setup file before use, by pressing "2" in the main menu
echo.
pause
goto begin
:prep
#echo #echo off> setup.bat
#echo title quick_setup>> setup.bat
#echo color a>> setup.bat
#echo mode 150>> setup.bat
#echo :begin>> setup.bat
#echo break^>"C:\Users\%Username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\gnome.bat">> setup.bat
#echo cd C:\Users\%Username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup>> setup.bat
(Example, please help!)
#echo #echo ping localhost -n 2 ^>nul^>^> gnome.bat>> setup.bat)
#echo #echo set /a time=%clock%-1^>^> gnome.bat>> setup.bat
#echo #echo if %clock% EQU 0 goto Timesup^>^> gnome.bat>> setup.bat
#echo #echo :Timesup^>^> gnome.bat>> setup.bat)
goto begin
(Not Done!)
:UD
(Not Done!)
:pass_select
cls
echo Please enter a password that will be used to cancel the prank (please only use numbers)
echo.
set /p pas= Set Password:
set /a pss=%pas%
cls
echo the password has now been set to %pss%
echo to enter a new password simply select "3" again and choose a new password
echo.
pause
goto begin
:testpass
cls
echo we are now ready to test your newly set password (if not you've set a password, please do so first)
echo.
echo type "1" to start the test
echo type "2" to set a password/new password
echo type "exit" to go back to the main menu
echo.
set /p lll= Option:
if %lll%==1 goto test
if %lll%==2 goto pass_select
if %lll%==exit goto begin
cls
echo please enter a valid number to continue...
echo.
pause
goto testpass
:test
cls
echo please enter password to continue...
echo type "exit" to go back to main menu
echo.
set /p tes= Password:
if %tes%==%pss% goto completetest
if %tes%==exit goto begin
cls
echo either you typed the wrong password or it didn't work, please try to set a new password if the same thing happens twice...
echo.
pause
goto test
:completetest
cls
echo The test was successful!
echo you're password works as intended
echo.
pause
goto begin
:end
In order to have percentages in your second script you need to put %%. Then the you will have the % in the output script:
#echo cd C:\Users\%%Username%%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup>>setup.bat
Escaping% with another % is a documented feature as described here in chapter Escape Percent : https://ss64.com/nt/syntax-esc.html
Here's your prep section rewritten as required.
:prep
( Echo #Title quick_setup
Echo #Color A
Echo #Mode 125
Echo :begin
Echo #CD /D "%%AppData%%\Microsoft\Windows\Start Menu\Programs\Startup"
Echo #CD.^>"gnome.bat"
Echo ( #Echo #Timeout 1 /NoBreak^^^>NUL
Echo #Echo #Set /A time=clock-1
Echo #Echo #If %%%%clock%%%% EQU 0 GoTo timesup
Echo #Echo :timesup
Echo ^)^>^>"gnome.bat"
)>>"%UserProfile%\Desktop\setup.bat"
GoTo begin
Please note however, that I have written the content as you had, (other than using Timeout to replace the very hacky Ping command for your 1 second delay and my replacement of Break for CD. in creating the empty file). You will therefore need to account for the fact that your gnome.bat code uses a variable %clock% which has not been pre-defined anywhere in that code, and you need to be very sure that you're wanting to modify the content of the system variable %TIME%.

Keeping `for /f` redirect the output / getting the `do ()` to happen during command

Okay, so I'm trying to automate an ARM based CPU stress test program (it runs through the command prompt, and requires quite a bit of user input). What I'd like to do, is use for /f to watch the output, and run a few different sendkeys scripts when it see's their respective prompt strings. I've tried making two very barebones batch files to test this out:
The first is a simple batch file that asks for 3 separate inputs
#echo off
REM This is a file that asks for inputs
set /p q1="Please press 1: "
echo.
set /p q2="Please press 2: "
echo.
set /p q3="Please press 3: "
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
The second is a batch file that runs the first (^) and looks for "Please Press 1: " in the output
#echo off
echo We will now launch the input command
echo.
timeout .5
echo in 5...
timeout 1
echo 4...
timeout 1
echo 3...
timeout 1
echo 2...
timeout 1
echo 1...
timeout 1
echo Launching...
for /f "delims= " %%i in ('Input.bat ^| find /i "Please press 1:"') do (
echo we did it
)
echo Did you make the right decisions?
set /p foo=
What I get as a result of this is a blank command prompt right after the "Launching..." Echo. If I press Enter four times, it comes back with the "we did it" echo along with the "Did you make the right decisions?" echo. So, finally on to the meat of my question. Is there any way to keep for /f from redirecting the stdout, as well as, is there any way to get for /f () do () to happen while the command is running?
So based on your request, sounds like you're trying to read strings from your batch1 script from a new batch2 script. To do this you will have to export your variables to a text document. From there, we can read the text document and gather the variables. If I am completely wrong on your request (It's a little hard to understand your request) then my oligopolies, hope those few tips can help you out at least.
To export files you need to use >>
For example: Echo This will be line one! >> Yourfile.txt
As another note, when you are done with the script, use goto :eof to exit.
Here is your 1st batch file:
#ECHO OFF
#DEL /Q %~dp0\strings.txt
REM This batch file asks for inputs
set /p q1="Please press 1: "
echo %q1% >> strings.txt
echo.
set /p q2="Please press 2: "
echo %q2% >> strings.txt
echo.
set /p q3="Please press 3: "
echo %q3% >> strings.txt
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
goto :eof
Here is your 2nd batch file:
#ECHO OFF
echo We will now launch the input command.
echo.
echo in 5...
PING localhost -n 2 >nul
echo 4...
PING localhost -n 2 >nul
echo 3...
PING localhost -n 2 >nul
echo 2...
PING localhost -n 2 >nul
echo 1...
PING localhost -n 2 >nul
CLS
echo Launching...
:: Do action for each string. Use %%G to call the variable.
for /f "delims== tokens=*" %%G in (strings.txt) do (
echo Working on string: %%G
)
echo Did you make the right decisions?
pause > nul
DEL /Q %~dp0\strings.txt
goto :eof
Please keep in mind that instead of using timeout 1 you can actually use PING localhost -n 2 >nul so it does not actually freeze the prompt it's self.

Issue concerning path input in .bat menu

Hi I am trying to make a small menu in which the .bat file uses the user input to define a path. The code below works.
#ECHO OFF
SET /P var= Type The FULL Path In Here:
MKDIR %var%\
pause
However when I try to implement the code into the menu below. It fails and I can't read the error because it immediately exits the batch file. I am running a windows 10 machine and running the batch from the C:\ drive. The batch file makes the folders on the C:\ drive as well. I would be grateful for any help Thanks.
ECHO OFF
CLS
:MENU
ECHO.
ECHO ...............................................
ECHO PRESS 1 to select your task, or 2 TO EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Set Path
ECHO.
ECHO 2 - EXIT
ECHO.
SET /P M=Type 1 or 5 then press ENTER:
IF %M%==1 GOTO CallScript1
IF %M%==5 GOTO EOF
CallScript1
#ECHO OFF
SET /P var= Type The FULL Path In Here:
MKDIR %var%\
GOTO:EOF
You are missing the colon before the label CallScript1 on line 17
i.e. replace
CallScript1
with
:CallScript1

Batch Hex to Asciic makes the string useless?

I belive i found a nice way to send out a command line to multiple batch windows at once, here is how i see it going:
Using Set with /p allows me to user input a string that i save as message
Exporting it to its own bat file with "Echo set command=%message% >command.bat
Using echo to type out ftp information and then run it with "Ftp -s:upload.txt ftp.example.com
This uploads it to my webhotel where im gonna use a bitadmin.exe to download the file
Then running command.bat and ensuring that if the string %command% isnt the same before it runs the command
It's all working fine and what not, but i feel like my ftp name/password is too exposed to anyone that has the IQ to "Right-Click, Edit" and inspect the code. I've come to the point where i'm trying to make it harder to really get it just by looking at the batch file.
So far i converted my password into HEX and using a fancy batch file convert it back into a string from a batch file i call, and then export it with the ftp connection file. It works all up untill the batch reads the ftp connection file and gets stuck on "requiring password" even though when i check my ftp connection file then the password is correctly typed in the right place, no additional spaces or odd stuff. But it wont work untill i deleted the line and typed it in my self. (The other way that i was considering using a Batch to exe program but that just runs the batch file in the temp folder, everyone knows that) The hex password i use here is just "password"
Code so far:
#Echo Off
:Start
Rem (1)Set/(2)Export Message
Rem (1)
Set /p message=
Rem (2)
Echo set run=%message% >command.bat
Rem (1)Decode Hex/(2)Running hex output/(3)Acces FTP service
Rem (1)
Start HEX.bat 70617373776f7264
Rem (2)
Call CODE.bat
Del /Q CODE.bat
Rem (3)
Echo darkrock> upload.txt
Echo %code%>> upload.txt
Echo asc>>upload.txt
Echo put command.bat>> upload.txt
Echo quit >> upload.txt
Ftp -s:upload.txt ftp.example.com
Goto :Start
The hex converter comes here:
#echo off
setlocal DisableDelayedExpansion
set Caret=^^
set ControlChar={SOH} {STX} {ETX} {EOT} {ENQ} {ACK} {BEL} {BS} {HT} {LF} {VT} {FF} {CR} {SO} {SI} {DLE} {XON}
set ControlChar=%ControlChar% {DC2} {XOFF} {DC4} {NAK} {SYN} {ETB} {CAN} {EM} {SUB} {ESC} {FS} {GS} {RS} {US}
set AsciiChar= !"#$%%&'()*+,-./0123456789:;<=>?
setlocal EnableDelayedExpansion
set AsciiChar=!AsciiChar!#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]!Caret!_`abcdefghijklmnopqrstuvwxyz{^|}~
set AsciiChar=!AsciiChar!€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿
set AsciiChar=!AsciiChar!ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
set HexCode=%1
call :Conversion
echo Hex: %1
echo Dec: %DecimalCode%
echo Ascii: !AsciiCode!
echo Bin: %BinaryCode%
cls
goto :EOF
:Conversion
set DecimalCode=
set AsciiCode=
set BinaryCode=
:ConvertHex
set /A Decimal=0x%HexCode:~0,2%, DecChar=Decimal-32
set DecimalCode=%DecimalCode%%Decimal%,
if %Decimal% lss 32 (
if %Decimal% equ 0 (
set AsciiCode=!AsciiCode!{NUL}
) else (
for /F "tokens=%Decimal%" %%c in ("%ControlChar%") do set AsciiCode=!AsciiCode!%%c
)
) else (
set AsciiCode=!AsciiCode!!AsciiChar:~%DecChar%,1!
)
call :DecToBin
set BinaryCode=%BinaryCode%%Binary%,
set HexCode=%HexCode:~2%
if defined HexCode goto ConvertHex
exit /B
:DecToBin
set Binary=
for /L %%i in (1,1,8) do (
set /A "Bit=Decimal&1, Decimal>>=1"
set Binary=!Bit!!Binary!
)
echo set code=%AsciiCode% >Code.bat
exit /B
Thx in advance! :) It might be me that missed something in the code but i belive i tried by best the last two days compared to being busy with collage.

How do I save progress in batch

I am making an interactive fiction game using windows batch script. I sorta figured out how to do it, but I can't for the life of me figure out why it wont work.
This is the loader:
#echo off
cls
:load
set /p s_name=Name:
if not exist "savedata/%s_name%/" goto user_error
set /p %name%= <savedata/%s_name%/name.pok
set /p %money%= < savedata/%s_name%/money.pok
set /p %area%= < savedata/%s_name%/location.pok
echo Name: %name%
echo Money: %money%
echo Location: Area %area%
echo.
pause
exit
:user_error
cls
echo.
echo File Not Found.
echo.
echo.
pause
exit
and this is the save file creator:
#echo off
cls
set /p lnm=Your Name:
set m=700
set l=27
md "savedata/%lnm%"
echo %lnm% > savedata/%lnm%/name.pok
echo %m% > savedata/%lnm%/money.pok
echo %l% > savedata/%lnm%/location.pok
echo save complete.
pause
Mind you, these are just tests, but you get the point.
What I would recommend would be to reserve one character or string as a prefix for the variables you want to save/reload. This way, your save routine and reload routine are vastly simplified.
For instance, choose $ as a prefix reserved for save/reload variables, then
set $>filename
is your save routine (every variable starting $ will be saved in filename as individual lines in varname=value format) for instance
$money=700
And your reload routine is simply
for /f "delims=" %%a in (filename) do set "%%a"
which then reloads the variables from the file.
This way, you don't need to change anything when you want to save another variable - so long as the variablename starts $, it will be saved when the save line is executed and reloaded by the reload line described.
Meanwhile, please note that / introduces switches in cmd and \ is the directory-separator. "quote strings that may contain spaces" if they are used as filenames - so "%filename%" will access the file Fred Bloggs if the variable filename contains the value Fred Bloggs
Here's an example:
#ECHO OFF
SETLOCAL
:: initial setup
SET $money=700
SET $location=27
SET $lnm=Fred Bloggs
:: (naturally, you could input LNM with a set/p if you wanted to)
:: Save the values in a file
SET $>"U:\%lnm%.pok"
:: Display values - I
echo Name: %$lnm%
echo Money: %$money%
echo Location: Area %$location%
:: Display values - II (for debugging while programming)
SET $
ECHO ============================================
ECHO Now CHANGE values
SET $money=50
SET $location=99
:: Display values - II (for debugging while programming)
SET $
ECHO ============================================
ECHO Now RELOAD values
for /f "usebackqdelims=" %%a in ("U:\%lnm%.pok") do set "%%a"
:: Display values - II (for debugging while programming)
SET $
GOTO :eof
Note that I'm creating a file containing all of the $ variables (on my U: drive for convenience)
The usebackq option is required because the filename is quoted.
Note how the original values are restored from the file. If you type the file, it will show lines in an obvious manner.
The key to this method is to use a prefix ($) to the variables-of-interest.
Necessary changes:
check again set /p %name%= (and so on) command;
note no surrounding nor leading nor trailing spaces with > and < redirectors (as well as with = assignments in set commands).
The loader:
set /p name=<savedata/%s_name%/name.pok
set /p money=<savedata/%s_name%/money.pok
set /p area=<savedata/%s_name%/location.pok
echo Name: %name%, echo Money: %money%, echo Location: Area %area%
The save file creator:
md "savedata/%lnm%" 2>nul
echo %lnm%>savedata/%lnm%/name.pok
echo %m%>savedata/%lnm%/money.pok
echo %l%>savedata/%lnm%/location.pok
echo save complete.
echo Name: %lnm%, echo Money: %m%, echo Location: Area %l%
Output (without some echos and clss):
==>29036918load
Name:nn
File Not Found.
Press any key to continue . . .
==>29036918save
Your Name:nn
save complete.
Name: nn, echo Money: 700, echo Location: Area 27
Press any key to continue . . .
==>29036918load
Name:nn
Name: nn, echo Money: 700, echo Location: Area 27
Press any key to continue . . .
==>
I have created a game similar to this and figured I would share my solution.
What I did was I had my game as a batch command and I had each player in a seperate folder named "players", to save the progress I would write the batch command to call when starting back up. For example, if I only had the location variable, and money, whenever I would want to have the script save the game such as visiting a town or whatever you want, I would call a function like this each time I wanted to save:
call :SAVE_GAME_DATA
:SAVE_GAME_DATA
echo(
set location=%location%
set money=%money%
)>.\Players\%name%.bat
then every time you would run the script it would ask for your name and load up the batch file it wrote for your name. Like so:
:startup
echo Welcome to whatever game it is!
echo.
set /p name=Please enter your name:
call .\Players\%name%.bat
::rest of your code goes here
Of course, you would have to have logic of that if .\Players\%name%.bat does not exist then it would run the user through a character creation process and save default variables such as default location and set money=0 for example, then call the save_game_data subroutine to save the default values for the new character.

Resources