1

I need to create a powershell script that removes quotes from CSV files in a user friendly drag and drop way. I have the basics of the script down courtesy of this page:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/02/remove-unwanted-quotation-marks-from-csv-files-by-using-powershell.aspx

And I've already sucessfully made .ps1 files drag and droppable courtesy of this stack overflow question:

Drag and Drop to a Powershell script

The author of the answer implies that it's just as easy to drop a single file, many files, and folders with lots of files in them. However, I have yet to figure this out in a way that can also can write back to the source file. Here's my current code:

Param([string[]]$file)
(gc $file) | % {$_ -replace '"', ""} | out-file C:\Users\pfoster\Desktop\Output\test.txt -Fo -En ascii

Currently, this will only accept a single file, and output the result as a txt to a specified file regardless of the source file type (I can change that to CSV easily but I'd like the script to mirror the source). Ideally, I'd like it to accept files and folders, and to rewrite the source file. I have a feeling this would involve the get-ChildItem but I'm not sure how to implement that in the current scenario. I've also tried out-file $file and that didn't work either.

Thanks for the help!

Community
  • 1
  • 1
user2748350
  • 15
  • 1
  • 4

1 Answers1

0

For writing the modified content back to the original files try something like this:

foreach ($file in $ARGS) {
  (Get-Content $file) -replace '"', '' | Out-File $file -Encoding ASCII -Force
}

Use a foreach in loop, because you need the file name in more than one place in the pipeline. Reading the content in a subshell and then piping the modified content into the Out-File cmdlet makes sure that the output file is only written after the content was already read.

Don't use a redirection operator ((Get-Content $file) >$file), because that would first open the file for writing (effectively truncating it) and afterwards read the content from the now empty file.

Beware that this approach may cause problems with large files, because each file is read completely into the RAM before they're processed and written back to disk. If a file doesn't fit into the available RAM the computer will start swapping, thus causing significant performance degradation.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for the help, but this doesn't seem to work. It will strip the quotes from one file but not any others. Any ideas? – user2748350 Oct 16 '13 at 18:44
  • Got it. Just drop the `param()` definition altogether and use the default argument list (`$ARGS`). – Ansgar Wiechers Oct 16 '13 at 19:28
  • Thank you very much, this works great! I'll mark your answer as correct. Bonus question: Do you know why this creates a new line at the end of each file? That's the only thing left I can't figure out... – user2748350 Oct 16 '13 at 19:40
  • `Out-File` automatically appends a newline if the content doesn't end with one, as does `Set-Content`. To avoid this behavior you'd have to use [.NET methods](http://stackoverflow.com/a/19132572/1630171) for writing the content. – Ansgar Wiechers Oct 16 '13 at 19:54
  • Interesting. I think I'll stick with what I've got for now, but I'll keep that in mind. Thanks again! – user2748350 Oct 16 '13 at 20:01