mles
mles

Reputation: 3476

Change paths from slash to backslash in a file with powershell

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.

Upvotes: 5

Views: 7575

Answers (2)

Nick F
Nick F

Reputation: 11

You could also try using .Net Path class:

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

Upvotes: 0

JPBlanc
JPBlanc

Reputation: 72680

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"

Upvotes: 3

Related Questions