I have a bunch of aspx files in aspx folder. From each file extract everything inside…
- User
I have a bunch of aspx files in aspx folder. From each file extract everything inside and store it in folder justify, with same filename as aspx file
Assistant
Here's a Python solution using BeautifulSoup to extract content from
<div align="justify">tags:import os from bs4 import BeautifulSoup # Define directories input_dir = 'aspx' output_dir = 'justify' # Create output directory if it doesn't exist os.makedirs(output_dir, exist_ok=True) # Iterate over each .aspx file for filename in os.listdir(input_dir): if filename.endswith('.aspx'): input_path = os.path.join(input_dir, filename) # Read the file content with open(input_path, 'r', encoding='utf-8') as file: content = file.read() # Parse HTML soup = BeautifulSoup(content, 'html.parser') divs = soup.find_all('div', align='justify') if divs: # Extract inner HTML from all matching divs extracted_content = ''.join([div.decode_contents() for div in divs]) # Save to output file output_path = os.path.join(output_dir, filename) with open(output_path, 'w', encoding='utf-8') as out_file: out_file.write(extracted_content) else: print(f"⚠️ No <div align='justify'> found in {filename}")Instructions:
-
Install BeautifulSoup (if not already installed):
pip install beautifulsoup4 -
Run the script: Place your ASPX files in a folder named
aspx, then execute the script. It will create ajustifyfolder with extracted content.
Notes:
- The script:
- Preserves HTML structure inside the
<div align="justify">tags. - Handles multiple
<div align="justify">tags by concatenating their content. - Skips files with no matching divs (and prints a warning).
- Uses UTF-8 encoding. Adjust if your files use a different encoding.
- Preserves HTML structure inside the
PowerShell Alternative (for Windows):
Add-Type -AssemblyName System.Windows.Forms $aspxDir = "aspx" $justifyDir = "justify" if (-not (Test-Path $justifyDir)) { New-Item -ItemType Directory -Path $justifyDir | Out-Null } Get-ChildItem -Path $aspxDir -Filter *.aspx | ForEach-Object { $file = $_ $content = Get-Content -Path $file.FullName -Raw $htmlDoc = New-Object System.Windows.Forms.HtmlDocument $htmlDoc.LoadHtml($content) $divs = $htmlDoc.DocumentNode.SelectNodes("//div[@align='justify']") if ($divs) { $extractedContent = "" foreach ($div in $divs) { $extractedContent += $div.InnerHtml } $outputPath = Join-Path $justifyDir $file.Name Set-Content -Path $outputPath -Value $extractedContent -Encoding UTF8 } else { Write-Host "⚠️ No content found in $($file.Name)" } }PowerShell Notes:
- Requires Windows with .NET Framework installed.
- Works natively with ASPX files on a Windows system.
- Uses the
HtmlDocumentclass from .NET to parse HTML.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.