我刚刚学会了如何使用批处理文件来最小化在控制台/终端上键入命令时的冗余。目前我使用它来跳转到深层次路径,如path1 / path2 / path / 3等等,但我认为可以创建一个批处理文件并创建多行不同的路径,我可以使用条件触发但我是目前输了。
目前,我使用批处理文件从一个路径跳转到另一个路径,为此,我为每个要访问的路径创建一个批处理文件。
我现在正在控制台/终端上使用dir-projectname(这是批处理文件名),但我想将它优化为dir-jump projectname(作为条件)。
我一直在寻找答案,但我不确定如何对我的问题进行实际分类,或者选择合适的关键字。
答案 0 :(得分:1)
很简单,你有两个文件。一个是文本文件(存储所有项目名称及其路径),另一个是参数,如果没有给出,则输入您想去的地方。我在启动Batch时已经创建了这样的程序,但是在我的转储文件夹中找不到它。
@echo off
set target=%~1
set path=%path%;%~p0
if "%target%" NEQ "" (Echo Using parameter %target% &goto :find)
:input
Echo No Parameter, Please Input desired Project, or type "/" for list of all projects
set /p target="Project Name: "
cls
:find
if "%target%"=="" (Echo Field Can Not Be Blank! &Echo.&Echo.&goto :input)
if %target%==/ (Echo Listing all Projects Available in List.txt&Echo.%type C:\List.txt&Echo.&Echo.&goto :input)
Echo Searching For "%target%"
setlocal EnableDelayedExpansion
for /f "tokens=1*" %%a in (C:\List.txt) do (
if "%%~a"=="!target!" (
:: THIS is the code that will be run was the project name is found
cls
cmd /k cd "%%~b"
Exit
))
Echo.&Echo.
Echo Sorry Project Not Found!
goto :input
:end
那 应该工作 (记住,未经测试)
Game-proj C:\devolpment\Games\Project-Files
Essays C:\Work\Essays\General
;Commented Line, Note: Any line starting with ";" will be ingored by the search
因此,使用带参数的程序或输入“Game-Proj”将打开相关文件夹,而使用带有“/”的程序将打印文件的内容。现在,如果文件路径包含括号,它可能会导致问题,但是除此之外还记得我没有测试过这个问题,并且稍后会尝试在其中查找错误。
莫纳
答案 1 :(得分:0)
@ECHO OFF
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET "$alphabet=A B C D E F G H I J K L M"
FOR /f "tokens=1*delims=:" %%a IN (q21569691.txt) DO CALL :setchoices "%%a" "%%b"
IF NOT DEFINED $_A GOTO :EOF
FOR /f "tokens=2*delims=_=" %%a IN ('set $_') DO ECHO(%%a : %%b
choice /c %$$% /m "Choose from menu "
SET /a chosen=%ERRORLEVEL%-1
CALL SET $$=%%$$:~%chosen%,1%%
CALL SET $$=%%$#%$$%%%
ECHO now CD to "%$$%"
GOTO :EOF
:setchoices
FOR %%i IN (%$alphabet%) DO IF NOT DEFINED $_%%i (
SET "$_%%i=%~1"
SET "$#%%i=%~2"
SET "$$=%$$%%%i"
GOTO :EOF
)
GOTO :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"
答案 2 :(得分:0)
这是为了我认为你需要的目的而编写的:
:::
:::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