4

I have a config file originating from a unix environment, where the basepath is replaced with the basepath in windows:

inputdir = D:\etl-i_win_4/input/000/
inputdir = D:\etl-i_win_4/input/001/
inputdir = D:\etl-i_win_4/input/002/
inputdir = D:\etl-i_win_4/input/003/
inputdir = D:\etl-i_win_4/input/004/
inputdir = D:\etl-i_win_4/input/005/
inputdir = D:\etl-i_win_4/input/006/
inputdir = D:\etl-i_win_4/input/007/
inputdir = D:\etl-i_win_4/input/008/
inputdir = D:\etl-i_win_4/input/009/

movepostcmd = D:\etl-i_win_4/divider/bin/os-independant/divider.postprocessing

ctrldir = D:\etl-i_win_4/divider/applogs/

lockglob = /opt/netmind/test/etl-festo/kette/divider/applogs/dividerglob.lock

I need proper windows paths with backslashes. How would a function look like, that reads the config file, identifies the lines with windows paths, and replaces all / with \? Note that the last line with a unix path should be ignored.

mles
  • 4,534
  • 10
  • 54
  • 94
  • In PowerShell, generally, backslash and forward slash in paths are interchangeable. So, are you really sure that you *do* need to replace the forward slashes? – Klas Mellbourn Jun 18 '13 at 18:50
  • 2
    http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – Ian Kenney Jun 18 '13 at 18:51
  • @Klas: That's a config file for a c++ programm. It doesn't like windows paths with slashes in it. So yes I need to replace them. – mles Jun 18 '13 at 18:54

2 Answers2

2

you can try thid to replace all path:

Get-Content "C:\temp\config.txt" | % {$_ -replace '/','\'} | set-content "C:\temp\config Bis.txt"

Just for Windows path accordind to the fact each line containing a Windows path match "A_LETTER:\" patern ... not so good, but it can do the job :

Get-Content "C:\temp\path.txt" | % {if ($_ -match "[A-Z]:\\"){$_ -replace '/','\'}else {$_}} | set-content "C:\temp\path Bis.txt"
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
-1

You could also try using .Net Path class:

Get-Content "C:\temp\config.txt" | % { [System.Io.Path]::GetFullPath($_) }
Nick F
  • 1