通过CMD打开文件,并显示在特定的编辑器选择 [英] Open a file through cmd and display the selected in specific editor

查看:280
本文介绍了通过CMD打开文件,并显示在特定的编辑器选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个特定的位置比例D打开CMD:/文
而只显示扩展名为.txt文件
示例

Open cmd in a specific location Example D:/text And only show the files with the .txt extension Example

Dir..../D:/text
Text1.txt
Text2.txt
Text3.txt

和添加功能来选择文本1或2或3
使用数字或类似

And add the function to select the text1 or 2 or 3 Using a numbers or something like

1 Text1.txt

1- Text1.txt

2 - Text2.txt

2- Text2.txt

3 Text3.txt

3- Text3.txt

和选择1或后,打开一个特定的程序文件而不改变窗口的默认应用开放(记事本)
我想打开与崇高的文本编辑器.txt文件(携带版)

And after selecting 1 or any, open the file with a specific program without changing the default app opening in windows (notepad) I wanna open the .txt file with sublime text editor (portable version)

而这一切,在一个批处理文件。

And all this in a batch file.

推荐答案

您至少应该告诉我们你是怎么试图为code到现在为止!
这一点出发,列出与数字文本文件和计数它们:

You should at least show us what did you tried as code until now ! This a little starting to list text files with numbers and counting them :

@ECHO OFF
SETLOCAL 
SET "ROOT=%~dp0"
SET "EXT=*.txt"
ECHO -----------------------------------------------
ECHO Listing text files on this folder - "%ROOT%"
ECHO -----------------------------------------------

SET "Count=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
REM And Populate the array with existent files in folder
FOR %%f IN ("%ROOT%\%EXT%") DO (
    SET /a "Count+=1"
    set list[!Count!]=%%~nxf
)

set Files=%Count%
ECHO.
rem Display array elements
for /L %%i in (1,1,%Files%) do echo file number [%%i] : "!list[%%i]!"
SET /a "COUNT_TOT=%Count%"
ECHO.
ECHO Total of text files(s) : %Count% file(s)
EndLocal
PAUSE>nul

编辑:于25/07/2016 @ 21:28

EDIT : on 25/07/2016 @21:28

我下载和安装的 崇高文本2 只是我的电脑上给予一试!

I download and setup Sublime Text 2 just for giving a try on my computer !

UPADTE版本与崇高文字2

Upadte Version with Sublime Text 2

@ECHO OFF
Title Open the selected file with Sublime Text
:MenuLoop
Cls & Color 0A
SETLOCAL 
SET "ROOT=%~dp0"
SET "EXT=*.txt"
SET "Count=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
REM And Populate the array with existent files in folder
FOR %%f IN ("%ROOT%\%EXT%") DO (
    SET /a "Count+=1"
    set list[!Count!]=%%~nxf
)

echo wscript.echo Len("%ROOT%"^) + 15 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")

set Files=%Count%
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO ********************************************************
ECHO   Folder : "%ROOT%"
ECHO ********************************************************
ECHO.
rem Display array elements
for /L %%i in (1,1,%Files%) do echo [%%i] : !list[%%i]!

SET /a "COUNT_TOT=%Count%"
ECHO.
ECHO Total of text files(s) : %Count% file(s)
echo(
echo Type the number of what file did you want to open ?"
set /p "Input="

For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Rem Testing if sublime_text.exe exist to open with it the text file
        If Exist "%programfiles%\Sublime Text 2\sublime_text.exe" (
            Start "Sublime" "%programfiles%\Sublime Text 2\sublime_text.exe" "!list[%%i]!"
            Rem Otherwise we open the text file with defalut application like notepad
            ) else (
            Start "" "!list[%%i]!"
        )   
    )
)   
EndLocal
Goto:MenuLoop 

上的编辑28/07/2016 @ 14:15

Edit on 28/07/2016 @14:15

UPADTE版本崇高的文本3和清单文件子目录

Upadte version with sublime text 3 and listing files in sub-directories

@ECHO OFF
REM : To download Sublime Text 
REM https://www.sublimetext.com/3
Title Edit the selected file with Sublime Text 3
:MenuLoop
Cls & Color 0A
SETLOCAL 
SET "ROOT=%~dp0"
SET "EXT=*.bat"
SET "Count=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
REM And Populate the array with existent files in folder
FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\%EXT%"') DO (
    SET /a "Count+=1"
    set "list[!Count!]=%%~nxf"
    set "listpath[!Count!]=%%~dpFf"
)

echo wscript.echo Len("%ROOT%"^) + 18 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 30 set /a cols=%cols% + 10
set Files=%Count%
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO  *******************************************************
ECHO   Folder : "%ROOT%"
ECHO  *******************************************************
rem Display array elements
for /L %%i in (1,1,%Files%) do echo [%%i] : !list[%%i]!

SET /a "COUNT_TOT=%Count%"
ECHO.
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo Type the number of what file did you want to edit ?
set /p "Input="
set "sublimeEXE=%programfiles%\Sublime Text 3\sublime_text.exe"
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Rem Testing if sublime_text.exe exist to open with it the text file
        If Exist "%sublimeEXE%" (
            Start "Sublime" "%sublimeEXE%" "!listpath[%%i]!"
            Rem Otherwise we open the text file with defalut application like notepad
            ) else (
            Start "" Notepad.exe "!listpath[%%i]!"
        )   
    )
)   
EndLocal
Goto:MenuLoop 

这篇关于通过CMD打开文件,并显示在特定的编辑器选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆