0
我仍然相当新的PowerShell,我仍然在努力学习。powershell替换文字
这里是我的问题与IM工作:
我有一堆的,我需要有一定的文字来取代蝙蝠文件。 Only Catch是在文本中有一件事需要用变量替换。
让我说明:
Printer1.bat Printer2.bat
文本需要被写成:
@echo off
setlocal EnableDelayedExpansion
::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8)
do (
if "!Version!" equ "this" (
set Version=Windows %%a
) else if "!ver: %%a=!" neq "%ver%" (
set Version=this
)
)
if "!Version!" equ "Windows XP" (
rundll32 printui.dll,PrintUIEntry /ga /n\\printserver\$printername
call P:\Script\wait.cmd 1
sc stop spooler
call P:\Script\wait.cmd 4
sc start spooler
call P:\Script\wait.cmd 1
) else echo Scriptet skal ikke kjøres på Windows7
Ping –n 5 –w 1 127.0.0.1>null
"@
如果您发现变量$打印机名称实际上是同一个名字作为文件名称。 所以我想要的是用文本覆盖文件,但用文件名替换变量(不带.bat)
不知道你是否明白我想要做什么。
我知道该怎么做的分路只得到文件名没有.BAT和替换为“”
我用尽喜欢的foreach语句这么多的方法。 .do而..没有我做的是正确的
谢谢。这个伎俩。忘记了system.io.path 我完全没有尝试过。再次感谢您 – vya
'System.IO.FileInfo'对象包含一个'BaseName'属性,您可以使用它来代替'system.io.path'。例如:'$ printername = $ file.BaseName' –
只有在使用'Get-ChildItem'(其中'ls'是别名)获得FileInfo对象时'BaseName'属性才可用,因为它是cmdlet添加的脚本属性。如果以另一种方式获得文件列表,例如从任何'System.IO'类型,或者如果你只有带有文件名的字符串,你不能使用'BaseName',但你可以使用'[System.IO.Path]'。不过好的建议,因为'BaseName'只要使用'Get-ChildItem'获得'FileInfo'对象就可以缩短代码长度。 –