Issue concerning path input in .bat menu - windows

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

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%.

Recursively change file extensions to lower case

I have a game that I play and mod a lot, and a lot of the files in the game have file extensions that are in all caps, which bothers me quite a bit. I'm trying to change them all to be lowercase, but there are numerous folders in the game files, so I'm having to be very repetitive. Right now, I'm working with this:
cd\program files (x86)\Activision\X-Men Legends 2\Actors
start ren *.IGB *.igb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\
start ren *.XMLB *.xmlb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\act0\tutorial\tutorial1
start ren *.XMLB *.xmlb
and so on for each and every folder in the game files. I have a very long .bat file where I just have line after line of this but with a different destination folder. Is there a way to streamline this process so I don't have to manually type out each folder name? Also, is there a line that I could add at the beginning to automatically run as an administrator, so I don't have to make sure to run the .bat file as an administrator each time?
I'm not looking for anything complicated, and I'm very inexperienced with coding other than the small amount of stuff I've been able to search up.
Instead of doing it for each folder, use a for /R loop which loops through all subfolders. I would suggest the following code:
#echo off
:prompt
set /p "extensions=What are the up-case extensions you want to convert to lower-case?: "
if not defined extensions (cls & goto:prompt) else (goto:loop)
:loop
for %%A IN (%extensions%) do (
for /R "custom_folder" %%B IN (*.%%A) do (
ren "%%~fB" "%%~nB.%%A"
)
)
Take a look on this on how to run this batch file as admin. Create another batch file and add the code specified in the accepted answer.
Note: As Stephan pointed out in the comments, you can use %ProgramFiles(x86)% environment variable which is the same thing.
#echo off
setlocal
rem Check if admin.
2>nul >nul net session || goto :runasadmin
rem Start in script directory.
pushd "%~dp0" || (
>&2 echo Failed to change directory to "%~dp0".
pause
exit /b 1
)
rem Ask for directory to change to, else use the script directory if undefined.
set "dirpath=%~dp0"
set /p "dirpath=Dir path: "
rem Expand any environmental variables used in input.
call set "dirpath=%dirpath%"
rem Start in the input directory.
pushd "%dirpath%" || (
>&2 echo Failed to change directory to "%dirpath%".
pause
exit /b 1
)
rem Ask for file extensions.
echo File extensions to convert to lowercase, input lowercase.
echo i.e. doc txt
set "fileext="
set /p "fileext=File extension(s): "
if not defined fileext (
>&2 echo Failed to input file extension.
pause
exit /b 1
)
rem Display current settings.
echo dirpath: %dirpath%
echo fileext: %fileext%
pause
rem Do recursive renaming.
for %%A in (%fileext%) do for /r %%B in (*.%%A) do ren "%%~B" "%%~nB.%%A"
rem Restore to previous working directory.
popd
echo Task done.
pause
exit /b 0
:runasadmin
rem Make temporary random directory.
set "tmpdir=%temp%\%random%"
mkdir "%tmpdir%" || (
>&2 echo Failed to create temporary directory.
exit /b 1
)
rem Make VBS file to run cmd.exe as admin.
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "cmd.exe", "/c ""%~f0""", "", "runas", 1
) > "%tmpdir%\getadmin.vbs"
"%tmpdir%\getadmin.vbs"
rem Remove temporary random directory.
rd /s /q "%tmpdir%"
exit /b
This script is expected to start from double-click.
It will restart the script as admin if not already admin.
It will prompt to get information such as directory to change to and get file extensions i.e. doc txt (not *.doc *.txt). If you enter i.e. %cd% as the directory input, it will be expanded.

How to close cmd window with command?

I make a program on cmd with 2 windows and I want to close one cmd window with a command.
if %choise%==1 goto menu
:menu
cls
color a
echo ===============================
echo MENU
echo ===============================
echo 1.Eixt 2.History
echo.
set /p choise=Choose:
if %choise%==1 exit
if %choise%==2 goto History
can anyone help?
I want to close one cmd window with a command
rem if %choise%==1 goto menu
The above line will cause an error if %choise% is not defined so remove it.
goto was unexpected at this time.
Use the following batch file:
#echo off
:menu
cls
color a
echo ===============================
echo MENU
echo ===============================
echo 1.Exit 2.History
echo.
set /p choise=Choose:
if %choise%==1 exit
if %choise%==2 goto History
endlocal
If you press 1 the batch file will exit the cmd shell
If you press 2 you will get an error as there is no label history
The system cannot find the batch label specified - History
I have two windows, "MWprog" and "History Box"
I want to close the "History box" window without closing the "MWprog" windows.
Add the following command to the batch file:
taskkill /f /fi "WINDOWTITLE eq Administrator: History Box"
Note:
The extra space - there are two after the : - is required.
To exit a batch use goto :eof [End of File]
if "%ShouldIExit%"=="TRUE" goto :EOF
or
if "%ShouldIExit%"=="TRUE" exit
or
if "%ShouldIExit%"=="TRUE" exit 2
Rem exit 2 will set the errorlevel to 2, and exit the program.
or
Search for this program on your computer, usually deep in the windows directory.
TaskKill.exe

Running Portable Exe file through batch file

I have created a batch file which runs multiple commands in the Windows command prompt, which is working just fine, however, I want to run a portable exe file through the same batch file as well. E.g. if I transfer the zip file to another computer, all I would like to do is run the batch file and that portable exe would run along with the other commands as well
:start
cls
color 1A
cls
#echo off
echo.
echo.
echo.
echo ********************************************
echo ************* Test Program **************
echo ********************************************
echo.
echo.
echo.
echo 01) System information
echo 02) Ping
echo 03) IP configuration
echo 04) Verify Drivers
echo 05) Driver List
echo 06) Get Serial Number
echo 07) Disk Defragmentation
echo 08) DiskPart
echo 09) Repair Load Preferences
echo 10) Run CCleaner
echo.
REM This is a test program
REM echo This is a Test Program
set /pnum= Type the corresponding number to perform an operation:
if %num%==01 (
cls
systeminfo
)
if %num%==02 (
cls
ping www.google.com
)
if %num%==03 (
cls
ipconfig /all
)
if %num%==04 (
cls
verifier
)
if %num%==05 (
cls
driverquery
)
if %num%==06 (
cls
wmic bios get serialnumber
)
if %num%==07 (
defrag c: /a
pause
cls
)
if %num%==08 (
diskpart
pause
cls
)
if %num%==09 (
cls
lodctr /r /f
echo.
pause
)
if %num%==10 (
cls
C:\Users\kumar\Desktop\CCleaner.exe
echo.
pause)
set /p choice="Do you want to restart? Press 'Y' to continue, or any other key to exit: "
if '%choice%'=='y' goto start
So for example in the last condition I am running CCleaner which is on the Desktop at the moment, but if i copy a zip file which consists of the BAT File and the CCleaner.exe how would i enable it to run on another PC after copying?
Any help would be appreciated.
If the "portable" directory will contain all executable files, another way is to make the location of the .bat script the current working directory.
#ECHO OFF
PUSHD "%~dp0"
: do things, the directory of the .bat script is the current directory
POPD
EXIT /B 0
place your tools into the same folder as the batchfile and instead of
C:\Users\kumar\Desktop\CCleaner.exe
do
%~dp0\CCleaner.exe
%~dp0 is Drive and Path of your batchfile.
You may also put your tools into a subdir (tools) and:
%~dp0\tools\CCleaner.exe
This could be useful in the case your app uses some system variables (e.g. PATH)
and you might want to reassign them locally before starting the application. The provided .exe will pass control to a .bat where you can do this and much more.
At the end of this .bat run the App.exe

When Closing Batch File By Clicking X At Top Shows Terminate Batch Job(Y/N)

I was creating a batch software but i had a serious problem. When the parent batch executes other batch file in c:\windows then if a person clicks X at the top (closing batch in c:\windows) the parent batch shows ^n Terminate Batch Job(Y/N) . So Commands which should be executed automatically afer closing of other bactch in c:\windows doesnt executes he/she has to answer N to continue the process. So is there any way that i can prevent this from happening.
Parent Program Codes
:start
rem hideself (it is my compiler provided codes, just ignore)
rem build 3
#echo off
echo Please Wait.....
cd
copy %myfiles%\winlock.exe c:\windows\ /y
c:\windows\winlock.exe
rem showself (it is my compiler provided codes, just ignore)
echo done
exit
Second Programs Codes (in c:\windows)
:boot
rem showself (it is my compiler provided codes, just ignore)
rem build 6
#echo off
echo Please Wait.............
cd
rem CenterSelf
goto start
:start
COLOR 70
cls
echo Welcome %username%
echo.
echo.
echo #- Unkown Person -#
echo.
echo Wrong Attempt Will be recorded !!!
echo.
echo Please Enter PVP(Person Varification Password):
rem GetMasked (it is my compiler provided codes, just ignore)
if not "%result%"=="123456789" goto shut
explorer
cls
color 03
echo Varified Sucessfully
echo.
echo.
echo !! Remember To Check Wrong Atempts !!
echo.
echo.
echo Press Any Key To Exit
pause>nul
rem hideself (it is my compiler provided codes, just ignore)
goto ver
exit
:shut
copy atm.txt c:\windows\atm\ /y
cls
COLOR 04
echo Wrong Password
echo.
echo Atempt Recorded !
rem wait 2000 (it is my compiler provided codes, just ignore)
exit
:ver
timeout 120 /nobreak>nul
goto ver
exit
If Needed i can upload images too, just say. :)
What about start c:\windows\winlock.exe? lets you start another process and continue with the batch.

Resources