简体   繁体   English

在批处理文件上创建条件

[英]Creating Conditions on Batch Files

I just learned how to use batch files to minimize the redundancies on typing commands on console/terminal. 我刚刚学习了如何使用批处理文件来最大程度地减少在控制台/终端上键入命令的冗余。 Currently I use it to jump to deep level paths like path1/path2/path/3 and so on but I think it's possible to make a single batch file and create multiple lines of different paths that I can trigger using a condition but I'm currently lost. 目前,我使用它来跳到更深层次的路径,例如path1 / path2 / path / 3等,但是我认为可以制作一个批处理文件并创建多行不同的路径,我可以使用条件触发这些路径,但是我目前丢失。

As of the moment, I use the batch files to jump from one path to another and in order to do this I create a batch file for every single path that I want to access. 目前,我使用批处理文件从一个路径跳转到另一路径,为此,我为要访问的每个路径创建了一个批处理文件。

I'm using dir-projectname(which is the batch file name) right now on console/terminal, but i want to optimize it to dir-jump projectname(as the condition). 我现在在控制台/终端上使用dir-projectname(这是批处理文件名),但我想将其优化为dir-jump projectname(作为条件)。

I've been searching for answers for a while but I'm not sure how to actually classify my question or what the proper keywords. 我一直在寻找答案已有一段时间,但不确定如何对问题进行实际分类或使用正确的关键字。

Easy, you have two files. 容易,您有两个文件。 One is a text file(Storing all project names, and their paths) and the other will take a parameter, and if none is given will take input for where you want to go. 一个是文本文件(存储所有项目名称及其路径),另一个将使用参数,如果未指定参数,则将输入您要输入的位置。 I already made a program like this when I started Batch, but could not find it in my dump folders. 启动批处理时,我已经制作了这样的程序,但是在我的转储文件夹中找不到它。

Jump.bat Jump.bat

@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

And That Should Work (Rememeber, not tested) 应该工作 (记住,未经测试)

C:\\List.txt C:\\ LIST.TXT

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

Thus, using the program with parameter or input "Game-Proj" Would open the relevent folder, while using the program with "/" will print the contents of the file. 因此,使用带有参数的程序或输入“ Game-Proj”将打开relevent文件夹,而带有“ /”的程序将打印文件的内容。 Now if the file path contains parenthesis, it might cause problems, but other then that remember I haven't tested this, and will try to find errors in it later. 现在,如果文件路径包含括号,则可能会引起问题,但是请记住,我还没有测试过它,以后将尝试在其中查找错误。

Mona 莫娜

@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

I used a file named q21569691.txt for my testing. 我使用名为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

Result: 结果:

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"

This was written for the kind of purpose you need I think: 我认为这是出于您需要的目的而编写的:

:::
:::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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM