You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在指定名称前添加Admin前缀?批处理脚本修改需求

Batch Script Solutions: Adding Conditional Prefixes

Hey there! Let's work through your two batch script questions clearly and step by step.

1. Basic: How to Add an "Admin" Prefix to a Name

If you just need to prepend the "Admin" prefix to any input name, it's straightforward—you just concatenate the prefix with the user's input using batch variable syntax. Here's a simple example:

@echo off
echo Enter ID:
set /p input=""
cls
:: Combine the prefix with the input value
set "full_name=Admin%input%"
echo Welcome %full_name%
pause

Breakdown:

  • set /p input="": Captures the user's input into the input variable
  • set "full_name=Admin%input%": Creates a new variable that prepends "Admin" to the input
  • echo Welcome %full_name%: Outputs the final greeting with the prefixed name

2. Advanced: Conditional Prefix (Only for "Modman")

First, your existing script has a tiny typo (Welocme → Welcome)—we'll fix that too! To add the "Admin" prefix only when the input is "Modman" (and skip it for "Bartek" or other inputs), we'll use batch's if conditional statement, plus delayed expansion to avoid variable parsing issues in code blocks.

Here's the updated, robust script:

@echo off
:: Enable delayed expansion to handle variables inside code blocks correctly
setlocal enabledelayedexpansion

echo Enter ID
set /p input=""
cls

:: Check if input matches "Modman"—apply prefix if yes, use original name otherwise
if "!input!"=="Modman" (
    set "greeting=Welcome Admin!input!"
) else (
    set "greeting=Welcome !input!"
)

echo !greeting!
pause
:: Clean up the local variable scope
endlocal

Key Notes:

  • Delayed Expansion: Using !variable! instead of %variable% ensures that the variable's value is evaluated at execution time (not before the code block runs), which fixes common bugs with conditionals in batch.
  • Conditional Logic: The if statement checks exactly for "Modman"—any other input (like "Bartek") will use the original name without the prefix. If you need to add more names that should get the prefix, you can extend this with multiple if checks or a loop for bulk matches.
  • Typo Fix: We corrected Welocme to Welcome for proper grammar.

内容的提问来源于stack exchange,提问作者Modman 276

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:26:59