-1

My aim is really simple: I want to list hundreds of ebooks on my site. Just words that you click on and it opens the pdf in a new tab. My issue lays in that doing this manually would take days for a single folder. In powershell, I've managed to find a way to list all contents in a directory and save them to a file.

cd "directory\to\file"
ls $search_dir > text.txt

This prints the contents and saves them in a text file. What I want to do is wrap text around it. In HTML it's obvious you need to make your tags like

<p><a href="path/to/file/file.pdf" target="_blank" class="downloadlink2">file</a></p>

This is how I want my printed list to look. So how do I get the list of contents to get wrapped around like this (and also printed again between the tags without the ".pdf" extention)?

johan
  • 93
  • 1
  • 5

2 Answers2

0

Here's half the puzzle, solving the other half is more rewarding than being simply told the answer.

This will read the text file in ( ) and add the string Added to the end of every line item.

Thanks goes out to https://stackoverflow.com/a/4952679/4317867 @mjolinor

(gc C:\Temp\z.txt) | ForEach-Object { $_ + " Added" } | Set-Content C:\Temp\z.txt
Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57
  • 1
    I love you so much user. You just saved me days of work. Apologies if my question wasn't the best (noticed a downvote) but I was desperate. Thanks so much. -edit- also you were right, it was more rewarding finding out the other half – johan Oct 05 '15 at 01:01
  • You're welcome! Please consider accepting the answer or posting your own solution to the question to assist others who find this question. – user4317867 Oct 05 '15 at 01:46
0

In powershell, type in

cd "directory/to/file" 

Then type in

ls $search_dir

ALT+left click to select a block of the 'name' section. Copy it to a text file and name it something like filename.txt. Then use the following line to apply the wrapping:

(gc C:\location\to\filename.txt) | ForEach-Object { "<p><a href=$([char]34)ebooks/Books/path/to/file/$_$([char]34) target=$([char]34)_blank$([char]34) class=$([char]34)downloadlink2$([char]34)>$_</a></p>" } | Set-Content C:\location\to\filename.txt

gc grabs the file in the directory next to it. ForEach-Object tells powershell to run what's in the curly brackets for each line in the document. $([char]34) is quotation marks inside the string and $_ is a line in the filename.txt file. Set-Content tells powershell to apply the changes made to the .txt file. When it's done the thing will look something like:

<p><a href="C:\path\to\file.pdf" class="downloadlink2" target="_blank">file.pdf</a></p>

Hope this helps someone in the future.

johan
  • 93
  • 1
  • 5