简体   繁体   English

从文本文件中删除字符串

[英]Delete string from a text file

I'm trying to grab only the drive letter and drive path from the net use command. 我正在尝试仅从net use命令中获取驱动器号和驱动器路径。 The problem is that I can get the drive letter and path but with my loop I also get a "Microsoft Windows Network" string. 问题是我可以获取驱动器号和路径,但是通过循环我也可以获得“ Microsoft Windows Network”字符串。

I have started out using tokens=2,3 but that unfortunately cuts out any spaces in the drive path which I do not want. 我已经开始使用tokens = 2,3了,但是不幸的是,它删除了我不想要的驱动器路径中的任何空格。

So bassically what I am asking is, is there a way to delete a string from a variable or from the text file. 因此,我要问的是,有没有一种方法可以从变量或文本文件中删除字符串。 I have tried using the findstr /v but that removes the whole line which is also undesirable. 我尝试使用findstr / v,但是删除了整个行,​​这也是不希望的。

:GetDrives
setlocal EnableDelayedExpansion
@echo off
cls
set i=0
for /F "tokens=2,*" %%a in ('net use ^| Find "\\"') do (
   set /A i+=1
   set array1[!i!]=%%a
   set array2[!i!]=%%b

)
set n=%i%
for /L %%i in (1,1,%n%) do echo !array1[%%i]! !array2[%%i]! >> DriveFile.txt

pause
GOTO:EOF

Below are examples of what I currently get and what I would like to be getting. 以下是我目前得到的以及我想要得到的东西的示例。

Net use befor script: 网路使用前的指令码:

Status       Local     Remote                    Network

----------------------------------------------------------------------------
OK           H:        \\Server\User$\UID   Microsoft Windows Network
OK           X:        \\Server\Folder\Folder   Microsoft Windows Network
OK           Z:        \\Server\Folder\Folder Name
                                            Microsoft Windows Network
The command completed successfully.

This is the current out put of my code in the DriveFile.txt: 这是我的代码在DriveFile.txt中的当前输出:

X:        \\Server\Folder\Folder   Microsoft Windows Network
Z:        \\Server\Folder\Folder Name

This is what happens when I use /F "tokens=2,3": 这是当我使用/ F“ tokens = 2,3”时发生的情况:

X:        \\Server\Folder\Folder
Z:        \\Server\Folder\Folder

This is what I would like to see: 这是我想看到的:

X:        \\Server\Folder\Folder
Z:        \\Server\Folder\Folder Name

But also want it to extend out past more than just 4 tokens so if the file path mapped to Z drive where say "\\server\\Folder Name\\Folder Name\\Folder Name" I would still get the full file path. 但也希望它扩展到超过4个令牌之外,因此,如果映射到Z驱动器的文件路径说“ \\ server \\ Folder Name \\ Folder Name \\ Folder Name”,我仍将获得完整的文件路径。

Any help is greatly appreciated, thanks in advance. 感谢您的任何帮助,在此先感谢。

for /f "tokens=2-3" %A in ('net use^|findstr  /i /c:"\\"')do echo %A %B

for /f "tokens=1-2* delims=:\" %A in ('net use^|findstr  /i /c:"\\"') do echo %A: \\%B %C %D %E

Unfortunately it is harder than it seems. 不幸的是,这比看起来要难。

  • The Status field can hold information or be empty. Status字段可以保存信息或为空。

  • The Network field can have multiple values depending of network mapping. Network字段可以具有多个值,具体取决于网络映射。 In my case i have "Microsoft Windows Network" and "Netware Services". 就我而言,我有“ Microsoft Windows网络”和“ Netware服务”。 So, there is no direct substitution. 因此,没有直接替代。

  • The Network field can be in the same line that the Remote field or can be on the next line, and as the Remote field may include spaces, checking the character at the column limit position is not reliable. Network字段可以与“ Remote字段位于同一行,也可以位于下一行,并且由于“ Remote字段可能包含空格,因此不可靠地检查列限制位置处的字符。 It is necessary to delay the check until the next line is readed to determine if it contains remote data. 有必要将检查延迟到读取下一行以确定它是否包含远程数据为止。

Sure, it can be done, but there are too much cases to handle. 当然可以,但是要处理的案例太多。 Some alternative options, and answer to asked question are: 一些替代选项以及对问题的回答是:

@echo off

    setlocal enableextensions enabledelayedexpansion

    :: Option 1 - Enumerate and ask each drive

    for /f "delims=:" %%a in ('net use ^| find ":"') do (
        for /f "tokens=2" %%b in ("x%%a") do set "drive=%%b"
        for /f "tokens=2,* delims=\" %%b in ('net use !drive!:'
        ) do echo(!drive!: \\%%b\%%c
    )

    echo -----------------------------------------------

    :: Option 2 - Ask WMI

    for /f "tokens=2,3 delims=," %%a in (
        'wmic netuse get LocalName^, RemoteName^, Status /format:csv ^| find ":"'
    ) do echo(%%a %%b

    echo -----------------------------------------------

    :: Option 3 - Only one net use command

    set "drive="
    set "remote="
    for /f "skip=6 delims=" %%a in ('net use') do (
        for /f "tokens=1,2 delims=:" %%b in ("%%a") do (
            if "%%c"=="" (
                if defined drive (
                    echo(!drive! !remote!
                    set "drive="
                    set "remote="
                )
            ) else (
                if defined drive echo(!drive! !remote:~0,26!
                for /f "tokens=2" %%d in ("x%%b") do ( 
                    set "drive=%%d:" 
                    for /f "tokens=2,* delims=\" %%e in ("%%c") do set "remote=\\%%e\%%f"
                )
            )
        )
    )
    if defined drive echo(!drive! !remote:~0,26!

First option uses a first net use command to get the list of drives and for each of them (be careful, the status field can be empty, so a second for is used to retrieve the letter) the drive letter is retrieved and used to emit a new net use x: command to retrieve the path. 第一个选项使用第一个net use命令获取驱动器列表,并且对于每个驱动器(请注意,状态字段可以为空,因此第二个for用于检索字母),驱动器字母被检索并用于发出一个新的net use x:命令来检索路径。

In the second option, wmic is used to retrieve the information from WMI. 在第二个选项中,使用wmic从WMI检索信息。

The third option is the original problem. 第三种选择是原始问题。 How to parse the net use command. 如何解析net use命令。

First option is slower (there is a initial net use and one net use x: for each drive) but second option, while faster, needs administrator rights. 第一种选择的速度较慢(每个驱动器都有一个初始net use ,每个驱动器有一个net use x: :),而第二种选择虽然速度较快,但需要管理员权限。 Third option is a nightmare but it is fast and does not require aditional rights. 第三种选择是一场噩梦,但它是快速的,不需要附加权利。

edited there was a problem in option 1 handling tokens. 编辑选项1处理令牌时出现问题。 Now corrected 现在已更正

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

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