加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 通过cmd打开文件并在特定编辑器中显示所选

发布时间:2020-12-13 20:37:50 所属栏目:Windows 来源:网络整理
导读:在特定位置打开cmd示例D:/ text 并且只显示扩展名为.txt的文件 例 Dir..../D:/textText1.txtText2.txtText3.txt 并添加函数以选择text1或2或3 使用数字或类似的东西 1- Text1.txt 2- Text2.txt 3- Text3.txt 选择1或任何之后,使用特定程序打开文件而不更改W
在特定位置打开cmd示例D:/ text
并且只显示扩展名为.txt的文件
Dir..../D:/text
Text1.txt
Text2.txt
Text3.txt

并添加函数以选择text1或2或3
使用数字或类似的东西

1- Text1.txt

2- Text2.txt

3- Text3.txt

选择1或任何之后,使用特定程序打开文件而不更改Windows中的默认应用程序开放(记事本)
我想用sublime文本编辑器(便携版)打开.txt文件

而这一切都在批处理文件中.

你至少应该告诉我们你到目前为止尝试了什么代码!
这有点开始列出带有数字的文本文件并计算它们:
@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

编辑:2016年7月25日@ 21:28

我下载并设置Sublime Text 2只是为了试试我的电脑!

Upadte版本与Sublime文本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,%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,%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 2sublime_text.exe" (
            Start "Sublime" "%programfiles%Sublime Text 2sublime_text.exe" "!list[%%i]!"
            Rem Otherwise we open the text file with defalut application like notepad
            ) else (
            Start "" "!list[%%i]!"
        )   
    )
)   
EndLocal
Goto:MenuLoop

编辑于2016年7月28日@ 14:15

具有sublime文本3的Upadte版本和子目录中的列表文件

@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,%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 3sublime_text.exe"
For /L %%i in (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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读