简体   繁体   English

Powershell读取CSV,然后以逗号分隔字符串之一,但如果在引号内则不分割

[英]Powershell to Read a CSV then split one of the strings at commas but not if inside Quotes

So first what I'm trying to do I've got a CSV file which has a list of arguments to be passed to Robocopy 所以首先我要做的是获取一个CSV文件,其中包含要传递给Robocopy的参数列表

I open this with Import-csv and pass it on to robocopy - I have found there are folders I can't copy - so to stop errors I exclude them - but found that I need to split them before I pass them to robocopy. 我使用Import-csv打开此文件,并将其传递给robocopy-我发现有无法复制的文件夹-因此,为了消除错误,我将它们排除在外-但发现在将它们传递给robocopy之前,需要先将它们分开。 What I want to be able to do is quote them similar to the CSV quoting- so that the split does not split inside quotes so split ("My Documents,""Bad, Path""") gives 我想做的就是像CSV报价一样对它们进行报价-这样拆分不会在报价内拆分(“我的文档”,“错误,路径”,“”)

My Documents
Bad, Path

Rather than 而不是

My Documents
Bad
Path

I was thinking a possible path to investigate is to pipe it through some CSV like processing - I'm not sure where to start to find a command which would do that 我当时想研究的一种可能途径是通过一些类似处理的CSV管道处理-我不确定从哪里开始找到可以做到这一点的命令

This is my test code 这是我的测试代码

import-csv "R:\t.csv" | where { $_.class -eq "PC1" } | % {
$_.DirExclusions
$_.FileExclusions
  $DirExclusions = $_.DirExclusions
  $FileExclusions = $_.FileExclusions
  $DirExclusions 
  $FileExclusions
  if ($DirExclusions -ne "" )  { $XD ="/XD" } 
  if ($FileExclusions -ne "" )  { $XF ="/XF" } 
  robocopy "$($_.Folder)" "R:\Backups\TEST" *.* /r:0 /w:1 $XD $DirExclusions.split(",") $XF $FileExclusions.Split(",") 
}

The CSV file CSV文件

Class,Folder,DirExclusions,FileExclusions PC1,C:\\Users\\e156479,".*,My Documents,""Hard, Path""", ntuser 类,文件夹,DirExclusions,FileExclusions PC1,C:\\ Users \\ e156479,“。*,我的文档,”“ Hard,Path”“”, ntuser

The output showing what it excluded 输出显示它排除了什么

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Thu Jun 14 14:07:32 2018

   Source : C:\Users\e156479\
     Dest : R:\Backups\TEST\

    Files : *.*

Exc Files : *ntuser*

 Exc Dirs : My Documents
            Hard
            Path
            .*

  Options : *.* /COPY:DAT /R:0 /W:1

The actual code changes the paths so that the \\ and C$ type shares are processed so that the Backup path is consistent for all source directories - I've removed all that processing so as to make my problem obvious 实际的代码更改了路径,以便处理\\和C $类型的共享,以便所有源目录的备份路径都是一致的-我删除了所有处理,以使问题显而易见

This should do the trick: 这应该可以解决问题:

$myLine = 'Class,Folder,DirExclusions,FileExclusions PC1,C:\Users\e156479,".*,My Documents,""Hard, Path""",ntuser'
$myLine -replace '""(.*),(.*)"""','$1#$2' -split ',' -replace '#', ','

If '#' should not possible for any reason, you can use another dummy-delimiter, eg "!" 如果由于某种原因不可能使用“#”,则可以使用另一个虚拟分隔符,例如“!”。

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

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