I made a simple batch script with two diff options, it kinda annoys me that it covers my whole screen although there are 2 lines visible. It would be cool if someone teaches me how to edit the script so that it opens in a smaller (resized window). This is my current script:
#echo off
title Changing Configs..
:main
echo 1 = trade time
echo 2 = grind time
set /p ibo=
if %ibo% == 1 goto trade
if %ibo% == 2 goto grind
:trade
ren "config.yml" "config - nakano.yml"
ren "config - senpai.yml" "config.yml"
pause
exit
:grind
ren "config.yml" "config - senpai.yml"
ren "config - nakano.yml" "config.yml"
pause
exit
I want it to look like this if I run it without resizing it manually
Not sure if there is a way to change the window's size, run mode, or whatever it is called from the batch script itself. It really doesn't matter since you can either control it via shortcut, or just avoid the situation all together.
If you look at the comments on your question, Compo mentioned using a shortcut - this is the most direct answer to your question. Do the following to create desired shortcut:
Create a shortcut by opening windows explorer and navigating to where your batch file is.
Start a drag-n-drop operation by grabbing the batch file with the mouse and moving the mouse off of the file, but DO NOT let go yet. Hold down the CTRL+Shift and the icon you are dragging, or the text under it, will change to indicate you are about to create a link (A.K.A. shortcut). Letting go in the same folder will work, but you may want to move the shortcut later. Alternatively, you could try bringing up a context menu on the batch file by moving the mouse over it and using the secondary click, and then select "create shortcut", which should be found fairly far done on the context menu.
Select the newly created shortcut with the mouse and on the keyboard type Alt+Enter - a properties window should appear. If for some reason that fails. you can use the mouse to bring up a context menu on the newly created shortcut (same way as described in step 2) and select "Properties" (Should be at bottom of context menu).
Go to the "Shortcut" tab in the "Properties" windows, select "Normal windows" from "Run:" section, and click the "OK" button (See image below).
The resulting icon can be moved to the desktop or pinned to the taskbar by dragging to the taskbar.
Or instead, you could rewrite your code so it doesn't ask any questions and just does the job. If you only have 2 choices, why not check which is current, change to the other, and report which one is now the current option?
The following code:
Verifies that copies of both the senpai and nakano configs exist before making any changes.
Checks if the file "Nakano.Active" exist, if so then it does the code to switch to senpai, else it assumes senpai is active and does the code to switch to nakano.
Replaces the current "config.yml" file with the desired configuration.
Deletes the current "{Mode}.Active" file, "Nakano.Active" or "Senpai.Active", as needed.
Creates a new Active file, or flag, to indicate the current mode. That is , creates "Senpai.Active" or "Nakano.Active", as needed.
Echos/reports out what mode it just switch to.
Waits for 2.25 seconds, giving you the time to see what mode it switched to. Change the 225 to the number of centiseconds you want the code to wait before continuing.
Executes EXIT which closes the window.
#ECHO OFF
IF NOT EXIST "config - senpai.yml" GOTO :DoExit
IF NOT EXIST "config - nakano.yml" GOTO :DoExit
IF EXIST Nakano.Active (
COPY /Y "config - senpai.yml" "config.yml" >NUL
DEL Nakano.Active 2>NUL
ECHO;Senpai >Senpai.Active
ECHO;Senpai Mode
) ELSE (
COPY /Y "config - nakano.yml" "config.yml" >NUL
DEL Senpai.Active 2>NUL
ECHO;Nakano >Nakano.Active
ECHO;Nakano Mode
)
REM Pause for 2.25 seconds
SET Centiseconds=225
SET "NOWTIME=%TIME:00.=%"
SET "NOWTIME=%NOWTIME:.=%"
FOR /F "TOKENS=1-3 DELIMS=:" %%G IN ("%NOWTIME::0=:%") DO SET /A "StartCentiseconds=(%%G*60+%%H)*6000+%%I"
SET /A EndCentiseconds=StartCentiseconds+Centiseconds
:GetNow
SET "NOWTIME=%TIME:00.=%"
SET "NOWTIME=%NOWTIME:.=%"
FOR /F "TOKENS=1-3 DELIMS=:" %%G IN ("%NOWTIME::0=:%") DO SET /A "NowCentiseconds=(%%G*60+%%H)*6000+%%I"
IF %NowCentiseconds% LSS %StartCentiseconds% SET /A NowCentiseconds=NowCentiseconds+8640000
IF %NowCentiseconds% LSS %EndCentiseconds% GOTO :GetNow
:DoExit
EXIT
As per comments simply add mode 40,10 or similar at start of file
#echo off & mode 40,10
title Changing Configs..
......
Related
I'm fumbling around in the dark a little, so please bear with me if I've missed something basic.
We needed to implement a new project management process in our department. So I created a batch file to ask for the name of a project, then copy a template file structure and rename various files within them to the name supplied, then open a word document. The initial catch was that this all had to be done on a network drive. Fairly straightforward stuff in theory, however for one of my colleagues it occasionally does something quite irritating. Instead of creating one copy of the file structure, it creates 2. This second one that it creates is unaccessible, and also undeletable. I can't replicate the error on my own laptop, and happens roughly 1 in 3 times. I can't work out why it would create this second file, or why on earth it would be unremovable once created. Can anyone shed any light?
Here's the code of the file I'm using:
#echo off
pushd $networkdrive\folder\Projects
SET initDir=$networkdrive\folder\Projects
SET newDir=
SET /p ver=<"$networkdrive\folder\projectnum.txt"
SET /P newDir=Please enter the title of the new project: %=%
IF DEFINED newDir (
MD "$networkdrive\folder\Projects\%ver%. %newDir%\"
SET initDir="%initDir%\%ver%. %newDir%\"
)
xcopy "$networkdrive\folder\Creative Briefing Template" %initDir% /s/e
ren %initDir%"1. Campaign Initiation"\"Campaign Initiation Document.docx" "%newDir% Initiation Document.docx"
ren %initDir%"2. Campaign Development"\"Project Finances.xlsx" "%newDir% Project Finances.xlsx"
ren %initDir%"2. Campaign Development"\"1. Briefing"\"Creative Briefing Document.docx" "%newDir% Creative Briefing Document.docx"
ren %initDir%"2. Campaign Development"\"1. Briefing"\"Integrated CRM brief.xlsx" "%newDir% CRM Brief.xlsx"
ren %initDIR%"3. Final Assets and Sign Off Document"\"Sign off sheet.docx" "%newDir% Sign off sheet.docx"
SET /a newVer=%ver%+1
echo %newVer% > "$networkdrive\folder\projectnum.txt"
%initDir%"1. Campaign Initiation"\"%newDir% Initiation Document.docx"
popd
cls
Since it's not possible to reproduce your problem outside of your environment, I'd suggest a few changes:
Insert a setlocal command immediately after the #echo on line so that any variable changes within the routine are discarded when the routine ends.
Change your set syntax so that it follows the set "var=value" formula. This will mean that you don't have crazy amounts of " characters to balance - just one before %var% and one at the end of the required string. Personally, I'd also remove the \\ as the last character of the directoryname and insert it as required when you use the variable.
When you set ver (a logical but inadvisable name since ver is an inbuilt command) you should immediately write the new version number out so that if the user is interrupted (phone call, coffee, being "managed") then there's the least possibility that the current number is acquired by another user (before it's updated by the current one). This would probably be best managed by
(for /f %%a in (%filename%) do set whatever=%%a&set /a newnum=%%a+1&call echo %%newnum%%>%filename%)
Finally, consider what will happen if newdir is empty or contains illegal/unexpected characters.
I am a student and new here in Stack Overflow.
I am currently creating a project for my Operating System class. One of the menu would be this: The user can create, rename and delete a certain file anywhere in the directory. I already know how to rename and delete a file but I had a hard time finding the right command to create a file wherein the user can determine what file extension that certain file would be.
It is sort of like this:
//this is to show the user what available drives the computer has
echo Here are the available drives:
echo.
wmic logicaldisk get name
echo.
echo.
set /p ch2=Which drive do you want to access?
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do #echo %%x >>drive%%x.txt
if exist D:\drive!ch2! (
goto specificview
)
else (
goto Invalid
)
//here the chosen drive will be viewed with its saved files and the user is asked what to do next
:specificview
cls
set ch3=
dir !ch2!:\ /w /a
pause
echo.
echo.
echo What do you want to do in this directory?
echo ================================================================
echo Press [X] to go back to choose directory
echo Press [w] to Exit
echo Press [C] to create File
echo If none of the choices, type down the name of the directory file
echo NOTE: Just press [1] to go back to the main MENU
echo ================================================================
//let's say the user will choose C to create file
set /p ch3= Please Choose:
if /i !ch3!==c (
set /p crt=Input the name of the filename:
set /p ext=Input the desired extension:
Now, I don't know what command I should use in order to complete the code. I would like to ask your help so that I can finish my project.
Thanks a lot in advance.
Not really comment, not really answer so I will make it CW!
As this question is not really a duplicate I will add a few things to change it to your needs.
I assume that your file is going to be empty and not using some special formatting or encoding.
As already visible in the answers to above question, there are a lot of ways to do that. I would suggest the answer with the most upvotes that copies NUL to a new file making it empty.
The copy command as you can see is quite simply structured: copy firstFile.something secondFile.somethingElse or in this case copy NUL fileName.ext
So in your case you would go ahead and add the following line:
copy NUL !crt!.!ext!
I've made a chat program and it works great, the only problem I have is that the SET /P command will wait until you press something and then press ENTER. There's my code that I got problems with:
:CHAT
CLS
SET TEXT=
TYPE "%FILESDIR%\Files\CHAT.cht" << I need this line active all the time.
ECHO.
SET /P TEXT=Text:
IF NOT "%TEXT%"=="" ECHO %USERNAME%: %TEXT% >> "%FILESDIR%\Files\CHAT.cht"
PING LOCALHOST -n 1 > nul
GOTO CHAT
I want to update the TYPE command all the time, but you need to type something. Is there anyway I can get around this?
EDIT: I really don't want another cmd screen
Suppose you did have the text updated continuously, in real time. What happens when the user begins to enter text, and then the other user sends text to be displayed at the same time. You only have one window - so you get a jumbled mess (assuming you could figure out how to make it happen technically).
You are a long way from having a functional chat program - you haven't completely thought out the issues.
1) You need two windows that are updated independently:
An input window where the user can enter text
A dialog window that displays the running dialog of all participants, in real time.
Windows batch doesn't support multiple windows. But you can emulate it by having two batch processes running for each user, one for input, the other for dialog output. A single batch script can be used for both processes. The parent script can launch multiple batch processes using the START command. Each batch process will get its own console window.
2) Only one person (process) can write to the text file at a time. What do you think happens if two people try to write at the same time? One will succeed, and the other will fail. You need a method to detect failure and automatically try again until success. I describe a simple method to achieve this at How do you have shared log files under Windows?.
3) SET /P will simply return the previous entry if the user presses <Enter> without typing anything. You should clear the text variable prior to your SET /P statement. EDIT - I now see the OP already had this in a place I did not expect
4) You don't want to retype the entire dialog from the beginning every time there is an update. You only want to dispaly the newly appended lines. It is possible to redirect input to an endless FOR /L loop, and within the loop you can use SET /P to read the most recent line. If nothing has been appended, then it will return nothing (assuming the variable was cleared before SET /P). You simply don't ECHO anything if nothing was received.
Here is a very crude working example that demonstrates the above concepts. There is no way to exit the program. You will have to close both console windows to quit.
#echo off
setlocal enableDelayedExpansion
set "dialog=dialog.txt"
if "%~1" equ ":input" (
title Chat Input
goto :input
)
start "" "%~f0" :input
title Chat Dialog
::Show Dialog
<"%dialog%" ( for /l %%N in () do (
set "text="
set /p "text="
if defined text echo(!text!
))
:input
cls
set "text="
set /p "text=>"
:write
2>nul (
>>"%dialog%" (
echo(%username%: !text!
(call )
) || goto :write
)
goto :input
There is still a long way to go before this is a truly useful chat program. But it is a good starting point. Some additional things that could still be added.
5) A way to start a new dialog file for each independent chat.
6) A way to invite one or more users to join the chat.
7) A clean way to exit the program, including closing of the additional console window. This requires interprocess communication. I demonstrate this in my SNAKE.BAT game at http://www.dostips.com/forum/viewtopic.php?t=4741. Warning - there are a lot of advanced concepts in that script, so extracting the relevent information might be a challeng ;-)
8) It would be nice to have the dialog window have a scrollable display buffer. The user can control the size of the buffer via the console properties, but it would be nice to control it programmatically. Native batch cannot do this, but I show how hybrid PowerShell/batch can do this at CMD: Set buffer height independently of window height
I have a few things set up in a batch game. Instead of going where it is supposed to when the sure enters an option and hits "Enter" it goes to the next thing that starts with a : (I don't know what it is called).
Instead of it going to "Youtube" when the user types "Y".
:visitoption
echo Would you like to visit the RST Garry's mod gaming community website?
set /p option=Y or N:
if %option%==Y start chrome (Censored link)
if %option%==N cls goto :youtube
if %option%==y start chrome (Censored link)
if %option%==n cls goto :youtube
:version
cls
#echo off
echo.
echo[
#echo off
echo.
echo[
echo --Version--
echo Lightup Demo
#echo off
echo.
echo[
#echo off
echo.
echo[
#echo off
echo.
echo[
pause
goto :versionwhite
:youtube
echo Would you like to visit the Creator's Youtube channel?
echo Gameplay commentarys and such.
set /p option=Y or N:
if %option%==Y start chrome (Censored link)
if %option%==N goto :Beginning
if %option%==y start chrome (Censored link)
if %option%==n goto :Beginning
Essentially you were missing a command separator after the CLS but I've made some other changes such as /i case insensitive comparing and made the checking routines more robust to spaces or no input.
:visitoption
echo Would you like to visit the RST Garry's mod gaming community website?
set /p option=Y or N:
if /i "%option%"=="Y" start "" chrome "(Censored link)"
if /i "%option%"=="N" cls & goto :youtube
goto :visitoption
The "thing" is called a label
Since you have no control over what the user types, you should use
if "%option%"=="Y" start chrome (Censored link)
that is, quote both sides of the comparison (this is not bullet-proof, but serves adequately where the user is not deliberately trying to break your system.)
Adding the /i switch to the if will make the comparison case-insensitive.
if defined option set "option=%option:~0,1%"
will set option to just the first character.
Note that if the user replies simply Enter then the value of the variable remains unchanged. You can use this characteristic to your advantage
set "option=defaultvalue"
set /p option=Y or N:
will set option to defaultvalue if the user replies simply Enter.
start will start a process independently. The batch simply carries on to the next statement. You are probably beter off using start "window title for this instance" ... - it's a quirk of start that the first "quoted parameter" is used as the window title where you may be expecting it to be used as a parameter.
To concatenate a series of commands in a single line, you need to separate the individual commands with an ampersand &
Once you've turned echo off once, you don't need to do it again (unless you execute echo on, which you can do during debugging to show the program flow.) The leading # means don't echo this command - without it, the initial ECHO OFF would be reproduced.
You can use call :label to execute a subroutine that starts at :label in this batch file. If you use call label then the "subroutine" executed is the executable label. This is a very important distinction.
For this reason, I eschew the use of goto :label - although it works - because the colon is not necessary and for congruence between the goto and call commands.
The one exception to this omit-the-colons approach is where the colon actually does have an effect - goto :eof very specifically means 'goto the physical end of this batch file' - the label :eof is understood by cmd to have that meaning, and should not be defined in the batch.
You've set it so that it goes to youtube when the user presses n, not when the user enters y like you said your trying to in the question:
if %option%==N cls goto :youtube
Please try seeing what the cod eyour using is doing before you post a question on it.
And SO has one of the worlds easiest CodeSlabing systems. HOW BLOODY HARD IS IT TO PRESS SPACE 4 TIMES OR HIGHLIGHT AND PRESS THE "Code Sample" BUTTON?
Mona.
I want to make a popup box apear when my computer starts up with 3 buttons in it, 1 to start word 1 to start firefox and 1 to start steam. Is there a way to do this in a batch file?
i know how to make a pop up box appear in batch:
#echo off
msg * this is where the message goes.
but this only makes a button saying ok.
You must create a VBS Script to work with batch script, but i have no sure if is possible to make custom buttons, so you can make a little adjustment where the DialogBox will have three buttons (Yes[Word],No[FireFox],Cancel[Steam]):
#echo off
echo/WScript.echo MsgBox^(^"Start program^",vbYesNoCancel+vbInformation, ^"Start program^"^)>_temp.vbs
For /F "tokens=*" %%E in ('CScript //Nologo _temp.vbs') Do (Set "res=%%E")
::YES
if %res% == 6 (
::Here is the path to Microsoft Word
)
::NO
if %res% == 7 (
::Here is the path to FireFox
)
::CANCEL
if %res% == 2 (
::Here is the path to Steam
)
del _temp.vbs