:exe
dir /a plugins
cls
set /p load="Which file do you want to load?: "
if exist plugins\%load%.bat goto loadtrue
if not exist plugins\%load%.bat goto loadfail
:loadfail
cls
echo error, file does not exist or you typed the file name wrong, try again
pause
goto exe
:loadtrue
cls
start %load%.bat
pause
goto terminal
Here is a batch file with comments which are the lines starting with rem:
#echo off
:UserPrompt
cls
rem Output the names of all batch files in subdirectory plugins in directory
rem of this batch file without file extension and next an empty line.
for /F "eol=| delims=" %%I in ('dir "%~dp0plugins\*.bat" /A /B 2^>nul') do echo %%~nI
echo/
rem Make sure the environment variable Load is not defined by chance
rem for example by a previous execution of this batch file in same
rem Windows command prompt window.
set "Load="
rem Prompt the user for the batch file name to load respectively execute.
set /P "Load=Which file do you want to load? "
rem Has the user input anything at all?
if not defined Load goto UserPrompt
rem Remove all double quotes from input string.
set "Load=%Load:"=%"
rem Is there anything left after removing all double quotes?
if not defined Load goto UserPrompt
rem Is there a batch file with user input name in subdirectory plugins
rem in the directory containing this batch file?
if exist "%~dp0plugins\%Load%.bat" goto StartPlugin
echo/
echo Error: The file name was typed wrong.
echo Please try it again.
echo/
pause
goto UserPrompt
:StartPlugin
cls
rem Start a separate command process for execution of the user selected
rem batch file with window title of console window set to name of batch
rem file and current directory set to subdirectory plugins of the
rem directory containing this batch file.
start "%Load%" /D"%~dp0plugins" ".\%Load%.bat"
pause
goto Terminal
:Terminal
I recommend to read in addition to the comments:
What does %~dp0 mean, and how does it work?
What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
Please note that the directory path referenced with %~dp0 always ends with a backslash and therefore concatenation of this path with a directory or file name should be done without an additional backslash as shown in code above even if it makes the file/folder strings more difficult to read.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
dir /?
echo /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
start /?
Related
currently all I have is this
#echo off
title "title"
:A
set /p Name=Whats your name?
if /i "%answer:~,1%" EQU "" goto a
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
set /p still=whats the password yes or no? (Y,N) :
if /i "%answer:~,1%" EQU "Y" goto b
if /i "%answer:~,1%: EQU "N" exit /b
:b
echo secret file users/desktop/name/files
Here is your user prompt demo batch file with additional command lines to safely process the user input. The batch file contains remarks to explain the code.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
title User prompt demo
cls
:GetName
rem Undefine the environment variable Name.
set "Name="
rem Prompt the user for the name.
set /P "Name=What is your name? "
rem Has the user just pressed RETURN or ENTER without entering a string?
if not defined Name goto GetName
rem Remove all double quotes from user input string.
set "Name=%Name:"=%"
rem Has the user entered just double quotes?
if not defined Name goto GetName
rem Output an empty line.
echo/
rem Enable delayed expansion and output the input string which could
rem contain characters like ampersands, angle brackets or pipes which
rem would modify the command line with ECHO before execution on not
rem using delayed environment variable expansion. The exclamation mark
rem at end must be escaped twice with caret character to be interpreted
rem as literal character to print by command ECHO in this case. Then
rem restore the previous execution environment as defined at top.
setlocal EnableDelayedExpansion
echo Hello !Name!^^!
endlocal
echo/
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo/
rem A classic yes/no prompt is done best with using command CHOICE which
rem is by default available since Windows Vista and Windows Server 2003.
%SystemRoot%\System32\choice.exe /C YN /N /M "What is the password, yes or no? (Y,N):"
if errorlevel 2 exit /B
rem The user pressed key Y and so the batch file processing continues.
echo/
echo Secret file: %UserProfile%\Desktop\name\files
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
cls /?
echo /?
endlocal /?
exit /?
goto /?
if /?
rem /?
set /?
setlocal /?
title /?
See also:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
This answer for details about the commands SETLOCAL and ENDLOCAL.
You could be also interested in How to create a directory in the user's desktop directory? It offers a batch file with a few lines to determine directly from Windows registry which directory is the user's desktop directory because of %UserProfile%\Desktop is just the default and every user has the freedom to use a different directory as desktop directory with a few clicks.
I have 300 files (.txt) with different name.
I want 2nd line of each file copied next to its file name into single text file.
Path of the file is D:\WCR\
Please help me with a script to be created in notepad with .Bat extension or commands to be given in cmd
Early response will be appreciated..!!
This batch file can be used for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=D:\WCR"
del "%SourceFolder%\SecondLines.log" 2>nul
for %%I in ("%SourceFolder%\*.txt") do call :ProcessFile "%%I"
endlocal
goto :EOF
:ProcessFile
for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in (%1) do (
>>"%SourceFolder%\SecondLines.log" echo %~nx1: %%L
goto :EOF
)
goto :EOF
Please note that command FOR ignores always empty lines. So if the second line is an empty line, the next non empty line is written into the LOG file.
The log file is created in source directory. For that reason it is important that the log file has not file extension txt.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
endlocal /?
for /?
goto /?
set /?
setlocal /?
See also:
Where does GOTO :EOF return to?
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Microsoft article about Using command redirection operators
How can I remove the last 2 folder in path of a file using batch script?
The result should be the last 4 folders path.
C:\Test\Test01\Test02\Test03\Test04\Test05\Test06\Test.txt
Should be like this:
C:\Test\Test01\Test02\Test03\Test04
Here is a commented batch code for this task:
#echo off
rem Is the batch file not called with an argument which
rem is expected to be the name of a file with full path?
if "%~1" == "" goto DemoCode
rem This code demonstrates how to get path to last but one folder
rem with accessing the file system and therefore working only if
rem the specified file respectively its folders really exist.
for %%I in ("%~dp1..\..\") do set "FilePath=%%~dpI"
rem Remove the backslash at end.
set "FilePath=%FilePath:~0,-1%"
goto DisplayResult
rem The demo code below demonstrates how to remove file name
rem and last two folders in path from a file name string with
rem complete path without accessing the file system at all.
:DemoCode
rem Get path of file ending with a backslash.
for /F "delims=" %%I in ("C:\Test\Test01\Test02\Test03\Test04\Test05\Test06\Test.txt") do set "FilePath=%%~dpI"
rem Remove the backslash at end.
set "FilePath=%FilePath:~0,-1%"
rem Get path to last folder of original file name with full path.
for /F "delims=" %%I in ("%FilePath%") do set "FilePath=%%~dpI"
rem Remove the backslash at end.
set "FilePath=%FilePath:~0,-1%"
rem Get path to last but one folder of original file name with full path.
for /F "delims=" %%I in ("%FilePath%") do set "FilePath=%%~dpI"
rem Remove the backslash at end a last time.
set "FilePath=%FilePath:~0,-1%"
:DisplayResult
echo/
echo File path is: %FilePath%
echo/
set "FilePath="
pause
There are two examples as the comments explain.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~1 (first argument string with surrounding double quotes removed) and %~dp1 (drive and path of first argument).
echo /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
Am trying to extract a RAR file to a directory (C:\autoexe\source). Here the "autoexe" folder name changes everyday. The fullname does not change, the constant thing is "auto" in the string "autoexe", the exe part changes. I tried the below
for /f "delims=" %%a in ('dir /b "%cd%\samples\package.rar"') do start "" "%ProgramFiles%\WinRAR\WinRAR.exe" x -ibck "%cd%\samples\package.rar" *.* "%cd%\auto * \source"
This commented batch code with error handling should do the job:
#echo off
rem Get name of first non hidden subfolder starting with auto in current folder.
for /D %%I in (auto*) do set "FolderName=%%I" & goto CheckArchive
echo Error: There is no auto* folder in: "%CD%"
echo/
pause
goto :EOF
:CheckArchive
if exist "samples\package.rar" goto ExtractArchive
echo Error: There is no file "package.rar" in subfolder "samples" of
echo folder: "%CD%"
echo/
pause
goto :EOF
:ExtractArchive
"%ProgramFiles%\WinRAR\UnRAR.exe" x -c- -idq -y -- "samples\package.rar" "%FolderName%\"
if not errorlevel 1 goto :EOF
echo/
echo/
pause
Read text file Rar.txt in program files folder of WinRAR for details on the used switches on UnRAR command line or run UnRAR.exe from within a command prompt window without any option to get displayed a brief help on how to use this freeware console application.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
pause /?
set /?
Read answer on Single line with multiple commands using Windows batch file for an explanation of & operator. And read the Microsoft support article Testing for a Specific Error Level in Batch Files.
I have the following MS Windows batch file code:
#echo off
cd\
dir/s *.docx *.xlsx *.ppt *.docx
SET /p input= %data% "
copy "%data%" C:\abc\
pause
This command shows all 4 types of extension list all over drives, but I want to take input from user and then copy to the desired location.
What am I missing?
Your main mistake is assigning the string entered by the user of the batch file to environment variable input but referencing on command copy the environment variable data which is not defined at all and therefore %data% is replaced twice before execution of the command line by nothing.
What about using this batch code?
#echo off
rem The next 2 commands are commented out as not needed for this task.
rem cd \
rem dir /s *.doc *.docx *.xlsx *.ppt
setlocal EnableExtensions EnableDelayedExpansion
:UserPrompt
echo/
echo Enter the file name with complete path or drag and drop
echo the file to copy from Windows Explorer over this window.
echo/
set "FileName=""
set /p "FileName=Enter file name: "
echo/
rem Remove all double quotes from entered string.
set "FileName=!FileName:"=!"
rem Has the user entered any file name at all?
if "!FileName!" == "" goto UserPrompt
rem Does the file really exist?
if exist "!FileName!" goto CopyFile
echo Error: The specified file does not exist.
echo/
goto UserPrompt
:CopyFile
copy "!FileName!" C:\abc\
echo/
endlocal
pause
Read the answer on Why is string comparison after prompting user for a string/option not working as expected? for an explanation on all the extra code used here.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
endlocal /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?