Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

First I will give a brief overview of what im trying to achieve. I want to go through a series of HTML files, replace code and then re-save these HTML files. This all works however the PS command will only execute this on HTML files which are on the default Powershell path (for me this is the H drive).

I want to be able to have a seperate folder which contains my powershell script and HTML files and convert them in that folder NOT from the H drive.

The code I have is follows:

Powershell script

$HTMLfiles=get-childitem . *.html -rec
foreach ($files in $HTMLfiles)
{

(Get-Content $files.PSPath) | ForEach-Object { $_ -replace "this text", "TEST" } | Set-Content $files.PSPath

}

This successfully changes all HTML files on the H drive that contain the words 'this text' with 'TEST'. I want to be able to change these HTML files from where the Powershell script is located, NOT from the H drive?

I appreciate any help.

Thanks

share|improve this question

2 Answers 2

Use the built-in variable called $PSScriptRoot to retrieve the files from the same folder where the PowerShell script resides.

Get-ChildItem -Path $PSScriptRoot -Include *.HTML;
share|improve this answer
    
Hi thanks for your help. Im getting slightly confused. Where exactly do I use this code and wouldnt I have to change the PSPath to resave these HTML files to the same folder as the powershell script? – user3402227 Mar 28 '14 at 13:34
    
Maybe I'm not quite understanding you. Let me ask it in the form of a question: Do you want to: 1) Put PowerShell script in a local folder, 2) Copy files from H drive to the same folder as the script file, 3) manipulate the HTML files in that local folder? – Trevor Sullivan Mar 28 '14 at 18:48

In your script, you ask to the Get-ChildItem cmdlet to look for items in the current directory, to make the script look for files in another directory, you just have to specify it to Get-ChildItem :

$HTMLpath="C:\path\to\your\html\files"
$HTMLfiles=get-childitem $HTMLpath *.html -rec
foreach ($files in $HTMLfiles)
{

(Get-Content $files.PSPath) | ForEach-Object { $_ -replace "this text", "TEST" } | Set-Content $files.PSPath
}

Edit : if you want the path to be passed as an argument to your script, just do the following :

param($HTMLpath)
$HTMLfiles=get-childitem $HTMLpath *.html -rec
foreach ($files in $HTMLfiles)
{

(Get-Content $files.PSPath) | ForEach-Object { $_ -replace "this text", "TEST" } | Set-Content $files.PSPath
}

then you can call your script in the console (assuming you are in the directory where your script is) : ./myscript "C:\path\to\your\files"

share|improve this answer
    
Thanks so much for your response. Can I ask how could I set the HTMLpath to the path of the Powershell file? instead of having to specify it in the code – user3402227 Mar 28 '14 at 13:17
    
Im sorry I dont think I explained myself very well. I want to be able to get the execution path of the script, find and replace text in the HTML files that are in the same folder of the script and resave them in that same folder. I dotn want to have to specify the path to the HTMl files, they are in the same folder as the script. Thanks again for your help – user3402227 Mar 28 '14 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.