Trim leading slashes using only a batch file

独自空忆成欢 提交于 2019-12-24 01:18:32

问题


For where I work my hands are tied on what I can use in order to do what I want.

What I need to do is create a .bat that does a net view and outputs it to a .txt and remove the leading slashes from the output.

Getting the net view to a file is the easy part but removing the leading slashes is what I am having an issues with.

Not sure if it will matter but the naming system that my company uses is #####-XX-##

I have looked around quite a bit and have not been able to find a way to easily do this.

EDIT: Powershell is also something that could be used if someone has a script for that.


回答1:


PowerShell:

net view | select-string '^\\\\([^\\\s]+)' | foreach-object {
  $_.Matches[0].Groups[1].Value
} | out-file netview.txt

Bill




回答2:


CMD (replace %m with %%m for running this in a batch file):

for /f "delims=\ tokens=*" %m in ('net view ^| findstr /r "^\\\\"') do (
  echo %m
) >out.txt

PowerShell:

net view | select-string '^\\\\' -AllMatches `
  | % { $_ -replace '^\\\\', '' } >out.txt


来源:https://stackoverflow.com/questions/15794831/trim-leading-slashes-using-only-a-batch-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!