I have created a script to copy a file to a directory, yet prompt you if the file already exists. this current code doesn't work as intended if yes is selected, but the file already exists and then no input is given at the second prompt. Meaning in the exist section it doesn't default to the no option when the timeout time is over.
:: This is a script to move downloaded files into a folder titled with todays date
#echo ---------------------------------
#echo ARE YOU SURE YOU WANT TO PROCEED?
#echo ---------------------------------
#echo off
::present user with Yes/No option
choice /C YN /M "Press Y for Yes, N for No." /T 10 /D N
if "%errorlevel%"=="1" goto:yes
if "%errorlevel%"=="2" goto:no
:yes
if exist "%date:/=_%\cE0MB.m4a" (
goto EXIST
) else (goto MOVE)
:MOVE
if exist cE0MB.m4a (
md "%date:/=_%"
move cE0MB.m4a "%date:/=_%\cE0MB.m4a"
) else (echo FILES NOT DOWNLOADED, PLEASE DOWNLOAD THEN RUN SCRIPT AGAIN)
if exist "%date:/=_%\cE0MB.m4a" echo FILE SUCCESSFULLY COPIED TO DIRECTORY
TIMEOUT /T 5 >nul
exit
:EXIST
if exist cE0MB.m4a (
choice /C YN /M "Files already exist, overwrite?" /T 5 /D N
if "%errorlevel%"=="1" goto:move
if "%errorlevel%"=="2" goto:no
) else (echo NOTHING TO COPY, CLOSING...)
TIMEOUT /T 3 >nul
exit
:no
echo YOU HAVE SELECTED NO OR WAITED TOO LONG TO RESPOND
echo PRESS ANY KEY OR WAIT TO EXIT
TIMEOUT /T 5 >nul
exit
however if i modify the exist section to the following, it works as intended
:EXIST
if exist cE0MB.m4a (
choice /C NY /M "Files already exist, overwrite?" /T 5 /D N
if "%errorlevel%"=="1" goto:no
if "%errorlevel%"=="2" goto:move
) else (echo NOTHING TO COPY, CLOSING...)
TIMEOUT /T 3 >nul
exit
Can anybody tell me why? I don't see any obvious conflicts.
To answer the question directly. choice sets a value based on the position of choices, because setting values inside of a code block requires delayedexpansion you need to do the same when running choice inside of a code block. Currently, when you enter the code block with the previously defined errorlevel it will remain 1 inside of the loop. So simply do:
:EXIST
if exist cE0MB.m4a (
setlocal enabledelayedexpansion
choice /C YN /M "Files already exist, overwrite?" /T 5 /D N
if "!errorlevel!"=="1" goto:move
if "!errorlevel!"=="2" goto:no
endlocal
) else (
echo NOTHING TO COPY, CLOSING...
)
TIMEOUT /T 3 >nul
Related
This question already has answers here:
Shortest Windows batch file code to get first line of a file set to a variable?
(6 answers)
Closed 1 year ago.
I'm trying to make a Borderlands 3 .bat file to remove certain files (it's a no-intro script). I do want to store the path the user input so the next time the game updates and re-download these files you can remove them fast without asking for a path later on.
This is the first code I have:
set BL3=C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3
set /p BL3= Where's the Borderlands 3 folder? (Default: C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3) =
The full code is here:
#echo off
title Borderlands 3 No Intro Script
:inicreator
set BL3=C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3
set /p BL3= Where's the Borderlands 3 folder? (Default: C:\Program Files (x86)\Steam\steamapps\common\Borderlands 3) =
set errorlevel=0
choice /C YN /N /M "Remove the loading screen as well? (Y = Yes | N = No)"
echo If you haven't cached shaders the screen will be
echo black for awhile if you delete the loading screen.
if '%errorlevel%'=='1' (set LD=yes)
if '%errorlevel%'=='2' (set LD=no)
echo.
echo Creating .ini to store the path for future use ...
echo %BL3% >"bl3settings.ini"
if exist "%BL3%\OakGame\Content\Movies\2KLOGO.mp4" del "%BL3%\OakGame\Content\Movies\2KLOGO.mp4"
if exist "%BL3%\OakGame\Content\Movies\AMDLOGO.mp4" del "%BL3%\OakGame\Content\Movies\AMDLOGO.mp4"
if exist "%BL3%\OakGame\Content\Movies\GBXLOGO.mp4" del "%BL3%\OakGame\Content\Movies\GBXLOGO.mp4"
if '%LD%'=='yes' (if exist "%BL3%\OakGame\Content\Movies\Loading.mp4" del "%BL3%\OakGame\Content\Movies\Loading.mp4")
if '%LD%'=='yes' (echo All done! Removed 2KLOGO.mp4, AMDLOGO.mp4, GBXLOGO.mp4 and Loading.mp4)
if '%LD%'=='no' (echo All done! Removed 2KLOGO.mp4, AMDLOGO.mp4 and GBXLOGO.mp4)
echo These will come back after Boderlands 3 updates.
timeout /t 2 /nobreak>nul
The goal is to write the current loation to a file, then read it and make a variable that we can use...
Using the idea mentioned in SomethingDark's comment, here's a quick untested rewrite of my undertsanding of your script:
#Echo Off
SetLocal EnableExtensions
Title Borderlands 3 - No Intro Script
CD /D "%~dp0" 2>NUL || Exit /B
:DefaultLocation
Set "BL3="
If Exist "%~dp0bl3settings.ini" Set /P "BL3=" 0<"%~dp0bl3settings.ini" 2>NUL
If Not Defined BL3 Set "BL3=%ProgramFiles(x86)%\Steam\steamapps\common\Borderlands 3"
PushD "%BL3%\OakGame\Content\Movies" 2>NUL
If Not ErrorLevel 1 GoTo Actions
:GetLocation
Set "BL3="
Set /P "BL3=Where is the Borderlands 3 directory location?>"
If Not Defined BL3 GoTo GetLocation
PushD "%BL3:"=%\OakGame\Content\Movies" 2>NUL
If ErrorLevel 1 GoTo GetLocation
:Actions
Echo(
Echo Creating .ini to store the path for future use ...
CD 1>"%~dp0bl3settings.ini"
Set "StandardList="2KLOGO.mp4", "AMDLOGO.mp4", "GBXLOGO.mp4""
Set "ExtendedList=%StandardList%, "Loading.mp4""
"%SystemRoot%\System32\choice.exe /M "Remove the loading screen as well"
If ErrorLevel 2 (Del /A /F %StandardList% 2>NUL
Echo All done. Removed: %StandardList%
GoTo Finish
)
Echo If you have not cached shaders, the screen will be black for a while...
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
Del /A /F %ExtendedList% 2>NUL
Echo All done. Removed: %ExtendedList%
:Finish
PopD
Echo These files will come back after Borderlands 3 updates.
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
:deleteaccount
cls
echo what account do you want to delete?
set /p dan=
if not exist %~dp0\database\%dan%\ (
echo this account doesn't exist & pause >nul & goto stage
)
else %~dp0\database\%dan%\ (
cls & echo password:
set /p dap=
call %~dp0\database\%dan%\%dan%.bat
if %dap% == %rpassword1% (
echo are you sure you want to delete this account? yes/no
set /p daq=
if %daq% == yes (
#RD /S /Q %~dp0\database\%daq%\
echo account succesfully deleted
pause >nul & goto stage)
if %daq% == no (goto stage)
)
)
After I type the correct password for the account I wanted to delete, it says < is unexpected for some reason.
For the purposes of saving screen real estate, here's an example of your script, modified to use the correct syntax and to include a logical structure.
#Echo Off
:DeleteAccount
ClS
Set "dan=:"
Set /P "dan=Which account do you want to remove? "
If Not Exist "%~dp0database\%dan%\" (
Echo The account does not exist.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
ClS
If Not Exist "%~dp0database\%dan%\%dan%.bat" (
Echo The required batch file is missing for the account.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
Set "dap="
Set /P "dap=Please enter your password: "
Call "%~dp0database\%dan%\%dan%.bat" 2> NUL
If ErrorLevel 1 (
Echo A error occurred running the batch file.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
If "%dap%." == "%rpassword1%." (
"%__AppDir__%choice.exe" /M "Are you sure you want to remove this account"
If Not ErrorLevel 2 (
RD /S /Q "%~dp0database\%daq%" 2> NUL
If ErrorLevel 1 (
Echo An error occurred deleting the account.
) Else Echo Account successfully removed.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
) Else (
Echo Incorrect password.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
:Stage
Rem Rest of code goes here.
Please read and review it, taking particular notice of the included double-quotes which are used to enclose and protect special characters in strings. The first two and last two lines were included to keep this example independent, (as they were omitted from your posted snippet).
As part of my job I need to remove files from computers that are being shipped over seas, this task is done several times a week and can be scripted, however I am terrible with script, I am in no means a coder of any sort.
My issue is that I need to clear out the C:\Users folder whilst keeping the Public and Default folders. I know how to remove a directory and all subfolders however I don't know how to keep just those two folders.
The code I have so far is:
#Echo off
color F
:Choice
Echo.
Echo.
set choice=
set /p choice="Type Y to proceed or N to close the tool and then press ENTER:----"
IF '%Choice%' =='Y' GOTO DELETE
IF '%Choice%' =='y' GOTO DELETE
IF '%Choice%' =='N' GOTO END
IF '%Choice%' =='n' GOTO END
:DELETE
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
Echo.
Echo.
#Echo All Files Removed
GOTO END
:END
echo.
echo.
Echo *****...Closing programme... Please wait...*****
Timeout /t 3
exit
Do you know if this is possible?
The following method, ran As Administrator, may work for you:
#Echo Off
Color 0F
Echo=&Echo=
Choice /N /M "Type Y to proceed or N to close the tool"
If ErrorLevel 2 Exit /B
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do WMIC UserAccount Where "SID='%%A' And LocalAccount='TRUE'" Delete
Echo=&Echo=&Echo All Files Removed&Echo=&Echo=
Echo *****...Closing programme... Please wait...*****
Timeout 3 /NoBreak>Nul
Exit /B
Special!='True' will ignore special accounts, which include Administrator, Public and Default.
I am a dabbler in batch files so my knowledge is limited to my experiences. What I am trying to do is limit the "Y or N" inputs to just that Y or N. Right now you can put anything in the fields and the code progresses. What I am attempting to do is create a hotspot using a batch file. I have yet to figure out how to "save" the created network but that isn't really an issue.
I have included what I have, the lines being the start and finish, If anyone happens to see anything that can be improved upon or made less bulky feel free to comment.
#echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
#echo off
:SSID
set /P inputA="Input desired Network SSID:"
echo.
set /P c=Is %inputA% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :PSWD
if /I "%c%" EQU "N" goto :SSID
:PSWD
set /P inputB="Input desired 8 to 63 character Network Password:"
echo.
set /P c=Is %inputB% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :SETUP
if /I "%c%" EQU "N" goto :PSWD
:SETUP
netsh wlan set hostednetwork mode=allow ssid=%inputA% key=%inputB% >NUL
#echo Creating Network...
echo.
timeout /t 5 /nobreak > NUL
#echo Network Created!
echo.
timeout /t 1 /nobreak > NUL
set /P c=Would you like to start your new Network? [Press "Y" to continue/Press "N" to abort]
if /I "%c%" EQU "Y" goto :START
if /I "%c%" EQU "N" goto :BYE
:START
netsh wlan start hostednetwork
timeout /t 5 /nobreak > NUL
#echo Your Network has started!
pause
:BYE
Exit
Instead of using set /p, use the choice command. I, personally, would use:
choice /m Correct?
if %errorlevel% equ 1 goto PSWD
if %errorlevel% equ 2 goto SSID
This will display: Continue? [Y/N]?. If the hit y, it will go to :PSWD. If they hit n, it will go to :SSID.
The help section of the choice command (brought up in Command Prompt by choice /?)
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
This is the code I am working on with right now. The code run the *.msu file from a dir onto the system for update. Looking to add a number count down the files from 9 to 1 so can tell The code is using a bat file. where I am all most done with the install.
There is the code
#echo off
setlocal EnableDelayedExpansion
'This part code found the number of msu files
for /f %%A in ('dir *.msu') do (
set files=!dirs!
set dirs=%%A
)
'This part of code put the file msu file name
for /f %%A in ('dir /b *.msu') do (
echo == Installing Updates == "%files%" "%%A" ...
%%A /quiet /norestart
)
echo.
echo ########################################
echo.
echo == Updates installed ==
echo.
echo == Press any key to restart ==&pause>nul
echo.
shutdown.exe /r /t 0
I would the output to look like this if can
== Installing Updates == "9" "file" ...
== Installing Updates == "8" "File" ...
I just need help a way of every time install update it make the number go down by one. Any help would be good. Thank for all your help.
Krii's answer should work fine. However there are easier ways. Such as the timeout command. The following will wait for ten seconds, and display a timer:
timeout 10
Or if you didn't want any output you could add > nul at the end of the command.
Another possibility is to exploit the choice command:
choice /c a /n /d a /t 10 > nul
That example also won't have any output. Of course the user can press the a key to cancel the countdown and continue the script, but I don't see that being an issue.
There is probably a better way to do this, but is this what you want?
#echo off
for /f %%A in ('dir /b *.msu') do (
echo == Installing Updates == "%%A" ...
%%A /quiet /norestart
)
for /l %%A in (9,-1,1) do (
cls
echo %%A
ping 1.1.1.1 -n 1 -w 1 >nul
)
cls
echo.
echo ########################################
echo.
echo == Updates installed ==
echo.
echo == Press any key to restart ==&pause>nul
echo.
shutdown.exe /r /t 0