batch-file - 首次使用后命令提示符 "forgets"命令

标签 batch-file cmd

我不明白为什么命令提示符在第一次使用后突然忘记了某些命令。 我在我的电脑上创建了一个文件夹来存储我的自定义命令,让我的生活更轻松。我已将此文件夹添加到我的 PATH 环境变量中,以便我可以快速访问我的命令。

例如: 运行 ipconfig 两次或更多次会导致命令每次都按预期工作。 运行 alias 命令(下面包含的代码)两次或更多次按预期工作。 运行我的 deletefolder 命令两次只在第一次有效。第二次说'deletefolder' 不是内部或外部命令,也不是可运行的程序或批处理文件。现在 alias 命令也不再起作用(无法识别),但 ipconfig 仍然有效。要让我的命令再次生效,我必须重新启动命令提示符。

我试图寻找这背后的原因,但没有找到任何解决此行为的方法。感觉好像很简单,但我真的找不到是什么。我已尽我所能,如果我需要澄清任何事情,请告诉我。

删除文件夹批处理文件:

@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

别名命令(作者:Benjamin Eidelman,beneidel@gmail.com)

@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    

最佳答案

The PATH environment variable有很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.

不要随意更改 PATH 变量,使用另一个变量名,例如_path如下:

set "_path=%1"

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

rem your script continues here

关于batch-file - 首次使用后命令提示符 "forgets"命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39843455/

相关文章:

python - WinError 87 尝试安装 Kivy

python - 启动cmd并在创建的cmd实例中运行多个命令

windows - 使用批处理文件打开多个 PDF 文档

c - 返回地址 GDB : . exe 与 .elf 不同?

windows - 将远程 FTP 主机上的文件重命名为命令的输出 (Windows)

windows - 7-Zip 关闭匹配 *.* 的文件夹的递归

batch-file - taskkill 关闭进程是否安全?

image - 如何将文件夹中的所有图像文件随机排列?

Windows:复制文件直到文件不存在

cmd - bat 文件和标签