5

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.

3
  • 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? Commented Jun 18, 2013 at 18:50
  • 2
    stackoverflow.com/questions/60034/… Commented Jun 18, 2013 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. Commented Jun 18, 2013 at 18:54

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

I'd make the pattern either just [A-Z]: or [A-Z]:[\\/], in case the first path separator is a / too.
0

You could also try using .Net Path class:

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

1 Comment

That would also transform Unix paths into Windows paths.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.