0

I've tried looking every where how to do this but I cant find out how. How do I check if a variable is defined and if it is replace every in variable in that file with a different variable. thanks

okay here is the code

@echo off
set/p "directoryname= Name of directory (to run on startup type "startup").    : "
if %directoryname%==startup goto startup

:namedirectory
if not exist "%directoryname%" (
mkdir "%directoryname%"
if "!errorlevel!" EQU "0" (
echo Folder created successfully
) else (
echo Error while creating folder
)
) else (
echo Folder already exists
)

goto skip_startup

:startup
:: here i would like to check if the variable %directoryname% is defined,
:: and  it is, replace the variable named %directoryname% with
:: (Not changing the variables name) with the directory,
:: "C:\Documents and Settings\All Users\Start Menu\Programs\Startup". that
:: way when ever i can recall my variables in my file i can change my folder
:: to the startup directory.

:skip_startup 
cd %directoryname%

::        ^
::        |_ replaced variable
zask
  • 181
  • 1
  • 6

1 Answers1

2

Here is the commented batch code with the requested and some more enhancements:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define as default directory name "startup". The user has to hit just
rem ENTER or RETURN with this default value and variable DirectoryName
rem keeps the default value "startup". Then ask user for directory name
rem and remove all double quotes from the entered directory name.

:EnterDirectory
set "DirectoryName=startup"
echo Please enter the name of the directory.
echo/
echo Hit just RETURN or ENTER to run on startup directory.
echo/
set /P "DirectoryName=Name of directory: "
set "DirectoryName=%DirectoryName:"=%"

rem Check if something other than just one or more double quotes was entered.
rem Prompt user once more after clearing screen if this is really the case.
if not defined DirectoryName cls & goto EnterDirectory

if "%DirectoryName%" == "startup" goto Startup

rem Check if entered directory name does not contain only spaces.

if "%DirectoryName: =%" == "" cls & goto EnterDirectory

echo/
if not exist "%DirectoryName%" (
    mkdir "%DirectoryName%"
    if errorlevel 1 (
        echo Error on creating folder: "%DirectoryName%"
    ) else (
        echo Folder "%DirectoryName%" created successfully.
    )
) else (
    echo Folder "%DirectoryName%" already exists.
)
goto SetDirectory

rem The variable DirectoryName has the default value "startup". Replace
rem this value by directory path of "Startup" folder in all users start
rem menu of Windows.

:Startup
set "DirectoryName=%ALLUSERSPROFILE%\Start Menu\Programs\Startup"

rem Set current directory to "Startup" directory or entered directory.

:SetDirectory
endlocal & cd /D "%DirectoryName%"

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.

  • cd /?
  • cls /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • mkdir /?
  • rem /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143