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.
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).
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.
@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)
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. 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.