获取内容:无法将参数绑定到参数'Path',因为它是空的



我是新的ShellScript。下面是我从提到的路径中删除'ONEWORD'的代码:如果我没有在文件名中引入空格,它会像预期的那样工作。但是如果文件名中有空格,它会在下面抛出错误:

计划:

call:DoReplace "ONEWORD" "" "C:UsersyeturukrDesktopTestDestCMD COMET.txt" "C:UsersyeturukrDesktopTestDestCMD COMET.txt"
  exit /b
  :DoReplace
  echo ^(Get-Content "%3" ^) ^| ForEach-Object { $_ -replace %1, %2 } ^| Set-Content %4 >Rep.ps1
  Powershell.exe -executionpolicy ByPass -File Rep.ps1
  if exist Rep.ps1 del Rep.ps1
  echo Done
  pause

错误:

Get-Content:无法将参数绑定到参数'Path',因为它是空的s字符串。在C:UsersyeturukrDesktopTestRep.ps1:1 char: 13+ (Get-Content <<<"C: 用户桌面 yeturukr 测试 CMD COMET_APIN002234不在座位上_20161025_0745_1.txt") | ForEach-Object {$_ -replace "BARCAP", "} | Set-Co content "C:UsersyeturukrDesktopTestDestCMD .txtCOMET_APIN002234_20161025_0745_1 . txt"+ CategoryInfo: InvalidData:(:) [Get-Content], ParameterBinding ValidationException+ fulllyqualifiederrid: parameterargumentvalidationerrorremptystringnotal
低下,Microsoft.PowerShell.Commands.GetContentCommand

echo Get-Content "%3"的结果类似于Get-Content ""…CMD COMET.txt""双引号(无效的路径规范)。

按如下方式应用参数扩展:

:DoReplace
echo ^(Get-Content "%~3" ^) ^| ForEach-Object { $_ -replace "%~1", "%~2" } ^| Set-Content "%~4" >Rep.ps1

现在你可以完全控制使用双引号了。

编辑:制作了一个复杂的示例脚本来证明上面的工作:

@ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_fileIn=%userprofile%DesktopTestDestCMD COMET.txt"
set "_fileOu=%userprofile%DesktopTestDestCMD COMET.txt"
rem csteate sample files
md "%userprofile%DesktopTestDest" 2>NUL
 >"%_fileOu%" type NUL
 >"%_fileIn%" echo 1st line 
>>"%_fileIn%" echo 2nd oneword
>>"%_fileIn%" echo 3rd line
type "%_fileIn%"
call:DoReplace "ONEWORD" "" "%_fileIn%" "%_fileOu%"
  type "%_fileOu%"
  exit /b
:endlocal
ENDLOCAL
goto :eof
  :DoReplace
  echo ^(Get-Content "%~3" ^) ^| ForEach-Object { $_ -replace "%~1", "%~2" } ^| Set-Content "%~4" >Rep.ps1
  Powershell.exe -executionpolicy ByPass -File Rep.ps1
  REM type Rep.ps1
  if exist Rep.ps1 del Rep.ps1
  echo Done
  goto :eof

:

==> D:batSO40233525.bat
1st line
2nd oneword
3rd line
Done
1st line
2nd
3rd line
==>

相关内容

  • 没有找到相关文章

最新更新