我怎样才能让一个可选列表出一个批处理脚本的文件搜索? [英] How can i make a selectable list out of a file search in a batch script?

查看:498
本文介绍了我怎样才能让一个可选列表出一个批处理脚本的文件搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使一个批处理文件,将用于在目录是的.sln文件,然后它们显示为列表中的所有文件中搜索,因此,用户可以选择他们从列表中选择所需其中一个,它就会打开该文件。不知道如何来存储文件,使他们到一个列表。这是我迄今为止..

 集TT_API_PATH = C:\\目录
CD%TT_API_PATH%
DIR / B / S *的.sln
设置CMD = DIR / B / S *的.sln
FOR / F %% i的('%CMD%')DO SET X = %%我


解决方案

编辑:替换不必要的和危险的调用回声%% X]选择[%% X]与 echo命令%% X]!选择[%% X]!。甚至不知道为什么我在首位包含它。

注意:

忽略独行线,由于某种原因,在我的code中的空行倒塌,这是nessicary穿上空行的东西,以保持格式。对不起。

 关闭@echo
SETLOCAL enabledelayedexpansion
设置计数= 0:
::阅读文件
在%% X(*的.sln)做(
  设置/计数=计数+ 1
  设置选择[!算!= %%点¯x
):
回声。
回声选择一:
回声。:
::打印文件列表
对/ L %%倍In(1,1,!算!)做(
   回声%% X]!选择[%% X]!

回声。:
::获取用户输入
集/ p =选择?
回声。:
::打印出所选的文件名
回声你选择!选择[%选择%]!


编辑:更改例如包含子目录的要求

要满足您的子目录中发现SLN文件要求,我改变了在%% X(*的.sln),它返回只有在发现了SLN文件当前目录下,以 FOR / F %%倍In('DIR / S / b *的.sln'),其执行 DIR / S / b在当前目录下*的.sln 命令并返回结果。

为了使输出干净清晰越好,我也改从整个路径列出文件时发生了什么印刷!选择[%% X]!,从当前目录选择[%% X]开始的相对路径:!%CD%\\ = (取代当前路径和目录(%CD%),加上一个反斜杠( \\ ),什么也没有)。

 关闭@echo
SETLOCAL enabledelayedexpansion
设置计数= 0:
::阅读文件
FOR / F %%倍In('DIR / S / B *的.sln')做(
    设置/计数=计数+ 1
    设置选择[!算!= %%点¯x
):
回声。
回声选择一:
回声。:
::打印文件列表
对/ L %%倍In(1,1,!算!)做(
     !回声%% X]选择[%% X]:%CD%\\ =!

回声。:
::获取用户输入
集/ p =选择?
回声。:
::打印出所选的文件名
回声你选择!选择[%选择%]!

以上code只显示当前目录到文件的相对路径,但保留变量中的完整路径选择[#]


如果你不希望在所有处理的全路径,可以添加一行 FOR / F %% X ... 循环,并恢复在为/升%% X ... 循环,在(编辑)第一个例子,让回声线code看起来是这样的(解释其中的变化去一样长需要为展示整个code,左右):

 关闭@echo
SETLOCAL enabledelayedexpansion
设置计数= 0:
::阅读文件
FOR / F %%倍In('DIR / S / B *的.sln')做(
    设置/计数=计数+ 1
    设置选择[!算!= %%点¯x
    (!计数)在%%Ÿ不要设置选择[%% Y] =选择[%% Y]:%CD%\\ =!
):
回声。
回声选择一:
回声。:
::打印文件列表
对/ L %%倍In(1,1,!算!)做(
     回声%% X]!选择[%% X]!

回声。:
::获取用户输入
集/ p =选择?
回声。:
::打印出所选的文件名
回声你选择!选择[%选择%]!

编辑/恢复该回声语句不是绝对必要的,而且根本不改变输出,但它是良好的编程习惯,因为它只是承认一个事实,即路径在当前目录中的选择[#] 时,文件名在最初阅读已移除变量。改变它消除了不必要的复杂性并消除可能的意外的结果,如果code为曾经改变或调整一些其他用途。如果你独自离开的路线,它会再次尝试,以取代当前目录的所有实例变量选择[#] 一无所有。当它失败时更换任何它就会立即离开该字符串不变。

I want to make a batch file that will search for all files in a directory that are .sln files and then display them as a list, so the user can select which one they want from the list and it will open that file. Not sure about how to store the file to make them into a list. This is what i have so far..

set TT_API_PATH=C:\dir
cd %TT_API_PATH%
dir /b /s *.sln 
set cmd=dir /b /s *.sln 
FOR /F %%i IN (' %cmd% ') DO SET X=%%i

解决方案

EDIT: Replaced the unnecessary and dangerous call echo %%x] !choice[%%x]! command with echo %%x] !choice[%%x]!. Not even sure why I included it in the first place.

NOTE:

Ignore the lone : lines, for some reason all the blank lines in my code collapsed, and it was nessicary to put something on blank lines to keep the formatting. Sorry.

@echo off
setlocal enabledelayedexpansion
set count=0

:
:: Read in files
for %%x in (*.sln) do (
  set /a count=count+1
  set choice[!count!]=%%x
)

:
echo.
echo Select one:
echo.

:
:: Print list of files
for /l %%x in (1,1,!count!) do (
   echo %%x] !choice[%%x]!
)
echo.

:
:: Retrieve User input
set /p select=? 
echo.

:
:: Print out selected filename
echo You chose !choice[%select%]!


EDIT: Changed example to include subdirectories as requested.

To meet your request for sln files found in subdirectories, I changed the for %%x in (*.sln), which returns only the sln files found in the current directory, to for /f %%x in ('dir /s /b *.sln') which executes the dir /s /b *.sln command in the current directory and returns the results.

To make the output as clean and clear as possible, I also changed what was printed when listing the files from the entire path !choice[%%x]!, to the relative path starting from the current directory !choice[%%x]:%cd%\=! ( Which replaces the current path and directory (%cd%) plus a backslash (\) with nothing).

@echo off
setlocal enabledelayedexpansion
set count=0

:
:: Read in files
for /f %%x in ('dir /s /b *.sln') do (
    set /a count=count+1
    set choice[!count!]=%%x
)

:
echo.
echo Select one:
echo.

:
:: Print list of files
for /l %%x in (1,1,!count!) do (
     echo %%x] !choice[%%x]:%cd%\=!
)
echo.

:
:: Retrieve User input
set /p select=? 
echo.

:
:: Print out selected filename
echo You chose !choice[%select%]!

The above code displays only the relative path from the current directory to the files, but leaves the full path inside the variable choice[#].


If you don't want to deal with the entire path at all, you can add a single line to the for /f %%x... loop, and restore the echo line in the for /l %%x ... loop to that in the (edited) first example, so that the code looks like this (Explaining where the changes go takes just as long as showing the entire code, so):

@echo off
setlocal enabledelayedexpansion
set count=0

:
:: Read in files
for /f %%x in ('dir /s /b *.sln') do (
    set /a count=count+1
    set choice[!count!]=%%x
    for %%y in (!count!) do set "choice[%%y]=!choice[%%y]:%cd%\=!"
)

:
echo.
echo Select one:
echo.

:
:: Print list of files
for /l %%x in (1,1,!count!) do (
     echo  %%x] !choice[%%x]!
)
echo.

:
:: Retrieve User input
set /p select=? 
echo.

:
:: Print out selected filename
echo You chose !choice[%select%]!

Editing / restoring that echo statement is not strictly necessary, and does not change the output at all, but it's good programming practice because it just recognizes the fact that the path of the current directory in the choice[#] variable was already removed when the filenames were originally read in. Changing it removes unnecessary complexity and removes possible unexpected results if the code is ever changed or adapted for some other use. If you leave the line alone, it will once again attempt to replace all instances of the current directory in the variable choice[#] with nothing. When it fails to replace anything it will simply leave the string unchanged.

这篇关于我怎样才能让一个可选列表出一个批处理脚本的文件搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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