Conny Sjögren
Conny Sjögren

Reputation: 45

Command prompt "forgets" command after first use

I can't figure out why the command prompt suddenly forgets certain commands after first use. I have created a folder on my computer to store my custom commands to make my life easier. I have added this folder to my PATH environment variable so I can quickly access my commands.

For example: Running ipconfig twice or more results in the the command working as expected every time. Running the alias command (code included below) twice or more works as expected. Running my deletefolder command twice only works first time. Second time it says 'deletefolder' is not recognized as an internal or external command, operable program or batch file. Now the alias command does not work anymore either (not recognized), but ipconfig still works. To get my commands to work again, I have to restart the command prompt.

I have tried to search for the reason behind this, but have not found any answer to this behavior. It feels like it's something simple but I really can't find out what. I have tried to be as thorough as I can, please let me know if I should clarify anything.

deletefolder batch file:

@echo off

set path=%1

IF DEFINED path (
    GOTO run
) ELSE (
    GOTO help
)

:help

    echo. Usage:
    echo.   deletefolder [path] 

exit /B


:run

    CHOICE /C YN /M "Are you sure?"
    IF ERRORLEVEL 1 ( 
        :del /f/s/q %path% > nul
        :rmdir /s/q %path%
    )

exit /B

alias command (author: Benjamin Eidelman, [email protected])

@echo off

set operation=%1
set aliasname=%2
set aliasfile=%~dp0%2.cmd

IF "%~1"=="" GOTO help
IF /I "%~1"=="list" GOTO listaliases
IF /I "%~1"=="set" GOTO setalias
IF /I "%~1"=="get" GOTO getalias
IF /I "%~1"=="delete" GOTO deletealias
IF /I "%~1"=="here" GOTO setaliashere

:help

echo. Usage:
echo.   alias list                        - list available cmd aliases
echo.   alias set [name] [command line]   - set an alias
echo.   alias get [name]                  - show an alias
echo.   alias delete [name]               - delete alias
echo.   alias here [name] [command line]  - create alias cmd on cwd

exit /B

:listaliases

dir /B %~dp0*.cmd
exit /B

:setaliashere

set aliasfile=%2.cmd

:setalias

if "%aliasname%"=="alias" (
    echo ERROR: cannot set this alias
    exit /B 1
)

echo %1 %2> "%aliasfile%"
for %%a in ("%aliasfile%") do set /a length=%%~za 
set /a length=length-1
set commandline=%*
setlocal enableDelayedExpansion
call set commandline=!commandline:~%length%!
set commandline=%commandline% %%*
echo %commandline%> "%aliasfile%"
echo INFO: alias "%aliasname%" set
exit /B

:getalias

if exist %aliasfile% (
  type %aliasfile%
) ELSE (
  echo ERROR: alias not found
  exit /B 1
)
exit /B

:deletealias

if /I "%aliasname%"=="alias" (
    echo ERROR: cannot delete this alias
    exit /B 1
)

if exist %aliasfile% (
  del %aliasfile%
  echo INFO: alias deleted
) ELSE (
  echo INFO: alias not found
)
exit /B    

Upvotes: 3

Views: 199

Answers (1)

JosefZ
JosefZ

Reputation: 30113

The PATH environment variable has very special (and crucial) meaning:

The %PATH% environment variable contains a list of folders. When a command is issued at the CMD prompt, the operating system will first look for an executable file in the current folder, if not found it will scan %PATH% to find it.

Do not change the PATH variable arbitrary, use another variable name, e.g. _path as follows:

set "_path=%1"

IF DEFINED _path (
    GOTO run
) ELSE (
    GOTO help
)

rem your script continues here

Upvotes: 5

Related Questions