Click here to Skip to main content
14,925,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
How to find and add the file name of each file in a folder to a meta tag -
<link rel"canonical" href="https://cure4incurables.in" />
(I am not a software engineer, so please give step by step instructions - I have implemented what is given here: Windows PowerShell Scripting Tutorial For Beginners - Varonis[^] )?


What I have tried:

I saw the matter here: Replace string value in a file with filename[^] but don't know how to implement it as the PowerShell is accepting only one line Commands (the Get-ChildItem -Path C:\Users\SKY\Desktop\cure.in command however did help me see all the files of that folder)
Posted
Updated 3-May-21 7:57am
v4

Quote:
please give step by step instructions
This forum is titled Quick Answers for a reason. We can try to help with code you have written, but there is not time or space to teach you in such a manner.
   
Comments
[no name] 3-May-21 3:52am
   
@Richard MacCutchan OK, I opened Windows Power shell as an admin, should I put all the code I found at https://www.codeproject.com/Questions/1258369/Replace-string-value-in-a-file-with-filename with a space for each next line (and everything together)? Please answer.
Richard MacCutchan 3-May-21 4:15am
   
Sorry, no idea. What is that code supposed to do, and how does it match your requirements?
[no name] 3-May-21 4:24am
   
@Richard MacCutchan Yes it is exactly what I want. I want to replace a string with the file name of each file for multiple files in a folder
Richard MacCutchan 3-May-21 4:38am
   
That code replaces a string in the contents of the file. And I am not sure what you mean by "to replace a string with the file name of each file for multiple files".
[no name] 3-May-21 4:51am
   
@Richard MacCutchan I want to know how to get/extract the file names of multiple files using the "$file.BaseName" command and add it to the Meta tag I mentioned in the beginning (which can be seen right on top) in each file for multiple files of a folder, just after the "url". How to do that?
Richard MacCutchan 3-May-21 8:30am
   
I still do not know what you mean by "add it to the Meta tag". Exactly where do you want the filenames to be placed?
However, the following code should give an idea:
$metaString = '<link rel"canonical" href="https://cure4incurables.in" />'
$files = Get-ChildItem -path C:\Users\rjmac\Documents\VSCode\PowerShell -filter *.txt
foreach ($file in $files)
{
    $newString = $metaString + $file.BaseName
    Write-Output $newString

}
[no name] 3-May-21 8:41am
   
The file names should be added to the point just after the URL of the meta tag (that is https://cure4incurables.in). Should I put all the code you gave me with a space for each next line (and everything together) and then hit "enter" on my Keyboard or should each line be separated by a semi-colon (but put all the Commands together)? Please also write the code out without a filter so that the file name of each file appears in each respective file of the folder. Thanks for your time and help!
Richard MacCutchan 3-May-21 9:01am
   
Ok, then try:
$metaString = '<link rel"canonical" href="https://cure4incurables.in/FNAME" />'
$files = Get-ChildItem -path C:\Users\rjmac\Documents\VSCode\PowerShell -filter *.txt
foreach ($file in $files){
  $newString = $metaString -replace "FNAME", $file.BaseName
  Write-Output $newString
}

Save this text in a file named test.ps1, and you can then execute it by typing:
powershell test.ps1
[no name] 3-May-21 11:26am
   
@Richard MacCutchan I saved it, opened Powershell as an administrator and typed powershell test.ps1 after saving the matter above in a "ps1" file but I got an error message thus:
The term 'test.ps1' is not recognised as the name of a cmdlet, function, script file or operable program. Check the spelling of the name or if a path was included, verify that the path is correct and try again.
At line:1 char:9
*test.ps1 <<<<
*CategoryInfo :ObjectNotFound: (test.ps1:String) [], CommandNotFoundException
*FullyQualifiedError Id : CommandNotFoundException
Should I also change the URL in the meta tag to make it '' (there is no FNAME right now)
Richard MacCutchan 3-May-21 11:38am
   
Sorry my mistake. Powershell does not assume the location of a script, so you need to add its relative path thus:
powershell ./test.ps1
[no name] 3-May-21 11:42am
   
I got the same error message as above
Richard MacCutchan 3-May-21 11:46am
   
Then you must have saved the script somewhere else.
[no name] 3-May-21 11:53am
   
I saved it on my Desktop. Where is it supposed to be saved? Please also remove any filters from the code you gave me above so that it will add the FILENAME to all the files
Should I also change the URL in the meta tag to make it "https://cure4incurables.in/FNAME (there is no FNAME right now)"?
Richard MacCutchan 3-May-21 11:56am
   
You can save it anywhere you like, but you must tell Powershell where it is located in order to run it.

As to your second request I do not understand what you are asking for; largely because I have to guess exactly what it is that you want this script to do.
[no name] 3-May-21 11:59am
   
OK, please give me the code without filters, I will try all permutations and combinations (I presume "-filter *.txt" is a filter to do the needful in only .txt files)
Richard MacCutchan 3-May-21 12:04pm
   
So remove those two fields from the script.
[no name] 3-May-21 12:58pm
   
@Richard MacCutchan I opened the file test.ps1 and changed the path to C:\Users\SKY\Desktop
Then I put all the files on my desktop and typed powershell ./test.ps1 and hit enter. It showed that string being replaced with the file name of each respective file on the screen but when I open each file to check, nothing has changed in any of those files. Where am I going wrong?
[no name] 3-May-21 13:34pm
   
OK, I copied the code from here: https://www.codeproject.com/Questions/1258369/Replace-string-value-in-a-file-with-filename and it finally worked! I still thank you for your time and help (of course, I changed the "99999999" to "FNAME"). In my meta tag, I had already added "FNAME" to the URL https://cure4incurables.in at the end
[no name] 4-May-21 4:56am
   
One can replace the "BaseName" with "FullName" to get the extension of each file but that adds even the full "path" which of course can be removed using the "find" and "replace" of Notepad++
$files = Get-ChildItem -path C:\Users\SKY\Desktop
foreach ($file in $files)
{Name"
    $content = Get-Content $file
    if ($content -match "FNAME")
    {
        $content = $content -replace "FNAME", $file.BaseName
        Set-Content $file $content
    }
}
   
v2
Comments
[no name] 4-May-21 9:23am
   
One can replace the "BaseName" with "FullName" to get the extension of each file (.htm or .html or .txt) but that adds even the full "path" which of course can be removed using the "find" and "replace" of Notepad++ (in my meta tag given in my question right at the top, I added "FNAME" to the URL https://cure4incurables.in at the end before running the script mentioned just above)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900