Batch file 在批处理文件上创建条件

Batch file 在批处理文件上创建条件,batch-file,Batch File,我刚刚学习了如何使用批处理文件来最小化在控制台/终端上键入命令的冗余。目前,我使用它跳转到深层路径,如path1/path2/path/3等等,但我认为可以创建一个批处理文件,并创建多行不同的路径,我可以使用条件触发这些路径,但我当前丢失了 目前,我使用批处理文件从一个路径跳到另一个路径,为此,我为我要访问的每个路径创建了一个批处理文件 我现在正在控制台/终端上使用dir projectname(这是批处理文件名),但我想将其优化为dir jump projectname(作为条件) 我搜索答案

我刚刚学习了如何使用批处理文件来最小化在控制台/终端上键入命令的冗余。目前,我使用它跳转到深层路径,如path1/path2/path/3等等,但我认为可以创建一个批处理文件,并创建多行不同的路径,我可以使用条件触发这些路径,但我当前丢失了

目前,我使用批处理文件从一个路径跳到另一个路径,为此,我为我要访问的每个路径创建了一个批处理文件

我现在正在控制台/终端上使用dir projectname(这是批处理文件名),但我想将其优化为dir jump projectname(作为条件)


我搜索答案已经有一段时间了,但我不确定如何对我的问题进行分类,也不确定正确的关键字是什么。

简单,你有两个文件。一个是文本文件(存储所有项目名称及其路径),另一个将接受一个参数,如果未提供任何参数,则将接受您想要去的地方的输入。当我开始批处理时,我已经制作了这样一个程序,但在我的转储文件夹中找不到它

跳棒 而且,应该起作用(请记住,未经测试)

C:\List.txt 因此,使用带有参数或输入“Game Proj”的程序将打开相关文件夹,而使用带有“/”的程序将打印文件的内容。现在,如果文件路径包含括号,可能会导致问题,但请记住,我还没有测试过它,稍后将尝试在其中查找错误

莫娜

@ECHO-OFF
SETLOCAL
::删除开始时的变量$
对于/F“delims=”('set$2^>Nul')中的%%a,请执行设置“%%a=”
设置“$alphabet=abcdefghijklm”
对于/f“tokens=1*delims=:”(q21569691.txt)中的%%a执行调用:setchoices“%%a”“%%b”
如果未定义$\u转到:EOF
对于/f“tokens=2*delims=quot=“%%a IN('set$quo;)中做回显(%%a:%%b
选项/c%$$%/m“从菜单中选择”
所选集合/a=%ERRORLEVEL%-1
调用集$$=%%$$:~%selected%,1%
调用集$$=%%$#%$$%%%
立即将CD回显到“%$$%”
后藤:EOF
:设置选项
对于(%$alphabet%)中的%%i,如果未定义,请执行%%i(
设置“$\%%i=%~1”
设置“$#%%i=%~2”
设置“$$=%%$$%%%i”
后藤:EOF
)
后藤:eof
我在测试中使用了一个名为
q21569691.txt
的文件

Application 1:c:\path\to\some\directory
Application 2:c:\path\two\some\directory
Application 3:c:\path\to\some\directory3
Application 4:c:\path\for\some\directory
Application 5:c:\path\to\some\dir ect ory
结果:

A : Application 1
B : Application 2
C : Application 3
D : Application 4
E : Application 5
Choose from menu [A,B,C,D,E]?E
now CD to "c:\path\to\some\dir ect ory"

这是为你需要的目的而写的我想:

:::
:::CDX.BAT - Provides a convenient way to navigate to common folder locations,
:::          and provides a way to manage the list of common folders.
:::          A separate list is maintained for each user.
:::
:::          CDX.BAT was written by Dave Benham based on an original idea by
:::          SS64 user chakri292. See http://ss64.org/viewtopic.php?id=1488
:::
:::  cdx [/P] [Number|String]
:::
:::    Changes the current directory to either the folder Number,
:::    or else to the first folder found that contains the String.
:::
:::    If neither Number nor String is specified, then presents a menu
:::    to choose the folder number.
:::
:::    If /P option is specifed then performs a PUSHD instead of CD.
:::
:::  cdx /A FolderPath [Number]
:::
:::    Inserts the FolderPath immediately before the specified folder Number.
:::
:::    If Number is not found then appends FolderPath to the end.
:::
:::    If Number is not specified then presents a menu to choose where to
:::    insert the FolderPath.
:::
:::    A relative FolderPath may be specified and the absolute path will be
:::    inserted into the common list. The FolderPath will not be added if
:::    it does not exist.
:::
:::    If FolderPath already exists in the common list, then it will be moved
:::    as specified by the Number.
:::
:::  cdx /M [Number InsertNumber]
:::
:::    Moves the folder specified by Number and inserts it before the position
:::    specified by InsertNumber. Moves the folder to the end if InsertNumber
:::    does not yet exist.
:::
:::    If either Number or InsertNumber is not specified then presents
:::    a menu to select a folder and insertion point.
:::
:::  cdx /D [Number]
:::
:::    Deletes the folder specified by folder Number.
:::
:::    If Number is not specified then presents a menu to choose which
:::    folder Number to delete.
:::
:::  cdx /C
:::
:::    Clears the list of common folders.
:::
:::  cdx /L
:::
:::    Lists the common folders currently stored.
:::
:::  cdx /?
:::
:::    Displays this help
:::

@echo off
setlocal

set "data=%userprofile%\cdx_%username%.dat"
set "newData=%data%.new"

set forEachDir=for /f "tokens=1* delims=:" %%A in ('2^^^>nul findstr /n "^" "%data%"') do
set move=move /y "%newData%" "%data%" ^>nul^&for %%A in ("%data%") do if "%%~zA"=="0" del "%data%"
set checkEmpty=if not exist "%data%" echo(^&echo Your list of common folders is empty. Use CDX /? to get help.^&exit /b

if "%~1"=="/?" goto :HELP
>nul findstr /xblic:":%~1" "%~f0" && goto :%~1
%checkEmpty%
set choice=%~1
if not defined choice call :menu CD quit
set "action=cd /d"
goto :setDir

:/P
%checkEmpty%
set "choice=%~2"
if not defined choice call :menu PUSHD quit
set "action=pushd"
goto :setDir

:/M
%checkEmpty%
set "choice=%~2"
set "choice2=%~3"
if not defined choice set "choice2="
if not defined choice2 set "choice="
if not defined choice call :menu "folder to MOVE" quit
set "folder="
%forEachDir% if "%choice%"=="%%A" set "folder=%%B"
if not defined folder exit /b
if not defined choice2 (
  set choice2=end
  set /p "choice2=Enter Number for new insertion point, nothing to move to end: "
)
call :/A dummy "%folder%" %choice2%
exit /b

:/A
if "%~2"=="" exit /b
if not exist "%~2\" exit /b
set choice=%3
if defined choice (set choice=%~3) else if exist "%data%" call :menu INSERT APPEND
set found=
>"%newData%" (
  %forEachDir% (
    if "%%A"=="%choice%" (
      echo(%~f2
      set found=1
    )
    if /i "%~f2" neq "%%B" echo(%%B
  )
  if not defined found echo(%~f2
)
%move%
exit /b

:/D
%checkEmpty%
set "choice=%~2"
if not defined choice call :menu DELETE quit
>"%newData%" (
  %forEachDir% if not "%%A"=="%choice%" echo(%%B
)
%move%
exit /b

:/C
del "%data%" 2>nul
exit /b

:/L
%checkEmpty%
call :menu LIST
exit /b

:help
(
  for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do @echo(%%A
)|more
exit /b

:menu  action  action2
%forEachDir% if %%A lss 10 (echo( %%A %%B) else (echo(%%A %%B)
if %1==LIST exit /b
echo(
set "choice="
set /p "choice=Enter the number for %~1, or nothing to %~2: "
exit /b

:setDir
%forEachDir% if "%choice%"=="%%A" endlocal & %action% "%%B" & exit /b
if defined choice for /f "tokens=* delims=:" %%A in (
  'findstr /i /c:"%choice%" "%data%"'
) do endlocal & %action% "%%A" & exit /b
exit /b

我测试了这个,它工作得很好,但是它在文件资源管理器上打开了项目的路径,而不是在控制台上打开了“cd path/path”.由于我刚刚开始了解如何使用批处理文件,我不确定如何修改它。@janharold哦…,您希望它在控制台上。好的,请尝试我的编辑。@janharold我的编辑将在cmd中打开路径,并像使用该目录打开cmd一样工作谢谢!它起作用了,但是如果我想理解整个代码,您能解释一下吗n it(如果你有时间的话)或指导我找到好的资源来扩展我对批处理文件的知识?我试过阅读,但我很确定我在网上看到的大多数东西对我来说都太超前了。@janharold有很多批处理教程(这是我第一个项目中制作的一个)但除此之外,你只需尝试一些东西。一旦你学会了
if
set
for
,你只需要在
帮助中尝试一些东西。除此之外,试着做点什么,如果你遇到麻烦,谷歌和类似的网站会帮你省钱。祝你玩得开心!它很有效。但它只是重复了这一部分[从菜单[A、B、C、D、E]中选择?E now CD到“C:\path\to\some\directory”]而不是实际转到该路径。我尝试通过删除[echo now CD to]并将其更改为[CD到“%$$%”来操作代码,但它总是说该路径无效。
A : Application 1
B : Application 2
C : Application 3
D : Application 4
E : Application 5
Choose from menu [A,B,C,D,E]?E
now CD to "c:\path\to\some\dir ect ory"
:::
:::CDX.BAT - Provides a convenient way to navigate to common folder locations,
:::          and provides a way to manage the list of common folders.
:::          A separate list is maintained for each user.
:::
:::          CDX.BAT was written by Dave Benham based on an original idea by
:::          SS64 user chakri292. See http://ss64.org/viewtopic.php?id=1488
:::
:::  cdx [/P] [Number|String]
:::
:::    Changes the current directory to either the folder Number,
:::    or else to the first folder found that contains the String.
:::
:::    If neither Number nor String is specified, then presents a menu
:::    to choose the folder number.
:::
:::    If /P option is specifed then performs a PUSHD instead of CD.
:::
:::  cdx /A FolderPath [Number]
:::
:::    Inserts the FolderPath immediately before the specified folder Number.
:::
:::    If Number is not found then appends FolderPath to the end.
:::
:::    If Number is not specified then presents a menu to choose where to
:::    insert the FolderPath.
:::
:::    A relative FolderPath may be specified and the absolute path will be
:::    inserted into the common list. The FolderPath will not be added if
:::    it does not exist.
:::
:::    If FolderPath already exists in the common list, then it will be moved
:::    as specified by the Number.
:::
:::  cdx /M [Number InsertNumber]
:::
:::    Moves the folder specified by Number and inserts it before the position
:::    specified by InsertNumber. Moves the folder to the end if InsertNumber
:::    does not yet exist.
:::
:::    If either Number or InsertNumber is not specified then presents
:::    a menu to select a folder and insertion point.
:::
:::  cdx /D [Number]
:::
:::    Deletes the folder specified by folder Number.
:::
:::    If Number is not specified then presents a menu to choose which
:::    folder Number to delete.
:::
:::  cdx /C
:::
:::    Clears the list of common folders.
:::
:::  cdx /L
:::
:::    Lists the common folders currently stored.
:::
:::  cdx /?
:::
:::    Displays this help
:::

@echo off
setlocal

set "data=%userprofile%\cdx_%username%.dat"
set "newData=%data%.new"

set forEachDir=for /f "tokens=1* delims=:" %%A in ('2^^^>nul findstr /n "^" "%data%"') do
set move=move /y "%newData%" "%data%" ^>nul^&for %%A in ("%data%") do if "%%~zA"=="0" del "%data%"
set checkEmpty=if not exist "%data%" echo(^&echo Your list of common folders is empty. Use CDX /? to get help.^&exit /b

if "%~1"=="/?" goto :HELP
>nul findstr /xblic:":%~1" "%~f0" && goto :%~1
%checkEmpty%
set choice=%~1
if not defined choice call :menu CD quit
set "action=cd /d"
goto :setDir

:/P
%checkEmpty%
set "choice=%~2"
if not defined choice call :menu PUSHD quit
set "action=pushd"
goto :setDir

:/M
%checkEmpty%
set "choice=%~2"
set "choice2=%~3"
if not defined choice set "choice2="
if not defined choice2 set "choice="
if not defined choice call :menu "folder to MOVE" quit
set "folder="
%forEachDir% if "%choice%"=="%%A" set "folder=%%B"
if not defined folder exit /b
if not defined choice2 (
  set choice2=end
  set /p "choice2=Enter Number for new insertion point, nothing to move to end: "
)
call :/A dummy "%folder%" %choice2%
exit /b

:/A
if "%~2"=="" exit /b
if not exist "%~2\" exit /b
set choice=%3
if defined choice (set choice=%~3) else if exist "%data%" call :menu INSERT APPEND
set found=
>"%newData%" (
  %forEachDir% (
    if "%%A"=="%choice%" (
      echo(%~f2
      set found=1
    )
    if /i "%~f2" neq "%%B" echo(%%B
  )
  if not defined found echo(%~f2
)
%move%
exit /b

:/D
%checkEmpty%
set "choice=%~2"
if not defined choice call :menu DELETE quit
>"%newData%" (
  %forEachDir% if not "%%A"=="%choice%" echo(%%B
)
%move%
exit /b

:/C
del "%data%" 2>nul
exit /b

:/L
%checkEmpty%
call :menu LIST
exit /b

:help
(
  for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do @echo(%%A
)|more
exit /b

:menu  action  action2
%forEachDir% if %%A lss 10 (echo( %%A %%B) else (echo(%%A %%B)
if %1==LIST exit /b
echo(
set "choice="
set /p "choice=Enter the number for %~1, or nothing to %~2: "
exit /b

:setDir
%forEachDir% if "%choice%"=="%%A" endlocal & %action% "%%B" & exit /b
if defined choice for /f "tokens=* delims=:" %%A in (
  'findstr /i /c:"%choice%" "%data%"'
) do endlocal & %action% "%%A" & exit /b
exit /b