Convert multiple HTML files into CSV files - powershell

I have hundreds of HTML files in tabular format which needs to be converted to CSV. To do it manually, I'd be opening each HTML file in Excel and saving as *.CSV.
How can I automate this through PowerShell?
$FolderPath = 'C:\Users\abcd\Desktop\New folder'
$FilePaths = get-childitem $FolderPath -recurse | where {$_.extension -eq ".html"}
foreach($FilePath in $FilePaths)
{
Export-CSV -Path $FilePath
}

If I understand you correctly you have HTML files that actually have CSV content is that correct? In that case you would only need to rename them from .html to .csv wouldn't you?
Move-Item -Path $filePath -Destination $filePath.Replace(".html", ".csv")
If not what exactly is the format of the "tabular format" in the source files? Can u provide example data from these HTML files?

Related

How to rename large number of files using Powershell and a CSV

Ultimately, I need a solid PowerShell script that will take a folder with several hundred video files, import the existing file names into the program, lookup the new file name in a CSV, and rename it. The old filename is simply (ie. File1.mp4, File2.mp4, etc.) I would like to appended a date to the front of the file in the format of (YYYY-MM-DD).
For testing, I created a folder on my desktop with (10) text files, each with a unique file name.
My CSV file appears as follows:
Image of CSV
The "newfilename" column, was created by using the Concatenate command in Excel.
`(=CONCATENATE(TEXT(A2, "yyyy-mm-dd")," ", B2)`
As much as I would just like PowerShell to handle everything, I feel using Excel for most of this might be the best way.
In my testing, everything was in one folder. However, at work, I will have video files on one drive, and the script will have to be in a folder on my desktop. Because I am in a corporate network, I need a special batch file to run my scripts, which is nothing new. I just modify the script name, and away it goes!
So what commands do I need to do in order to have the script separate from the video files AND the CSV file?
Here is the code that I have so far. Everything works when it's in one folder.
PS C:\Users\ceran\Desktop\Rename Project> Import-Csv -Path .\MyFileList.csv | ForEach-Object {
>> $Src = Join-Path -Path $TargetDir -ChildPath $_.filename
>> $Dst = Join-Path -Path $TargetDir -ChildPath $_.newfilename
>> Rename-Item -Path $Src -NewName $Dst
>> }
Thanks in advance for the help!
Chris
I'm not sure what the date column is in your Excel file and if you want to rename all files in the folder, but if that is the case, you don't need a csv file at all and can do this:
$sourceFolder = 'X:\Path\to\the\video\files' # change this to the real path
Get-ChildItem -Path $sourceFolder -Filter '*.mp4' -File | # iterate through the files in the folder
Where-Object {$_.Name -notmatch '^\d{4}-\d{2}-\d{2}'} | # don't rename files that already start with the date
Rename-Item -NewName { '{0:yyyy-MM-dd} {1}' -f $_.LastWriteTime, $_.Name } -WhatIf
This uses parameter -Filter '*.mp4', to get only files with an .mp4 extension. For the files in your testfolder (Desktop\Rename Project), change this to -Filter '*.txt'.
If you want all files renamed, no matter what the extension, simply remove the Filter from the cmdlet.
Because of the -WhatIf switch, no file is actually renamed and the code just shows in the console what would happen. Once satisfied that this is OK, remove the -WhatIf
Hope that helps.
$targetdir="C:\path\to\where\our\file\directory\is"
$pathtocsv="c:\path\to\csv.csv"
Import-Csv -Path $pathtocsv | ForEach-Object {
$Src = Join-Path -Path $TargetDir -ChildPath $_.filename
$Dst = Join-Path -Path $TargetDir -ChildPath $_.newfilename
Rename-Item -Path $Src -NewName $Dst
}
Why would this not work in any situation?
By the way, if the csv had the columns path and newname, it could be piped directly to rename-item:
path,newname
file.txt,file2.txt
import-csv ren.csv | Rename-Item -whatif
What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/file.txt Destination: /Users/js/foo/file2.txt".

Copying files to specific folder declared in a CSV file using Powershell Script

i am quite new to powershell and i am trying to make a script that copy files to certain folders that are declared in a CSV file. But till now i am getting errors from everywhere and can't find nothing to resolve this issue.
I have this folders and .txt files created in the same folder as the script.
Till now i could only do this:
$files = Import-Csv .\files.csv
$files
foreach ($file in $files) {
$name = $file.name
$final = $file.destination
Copy-Item $name -Destination $final
}
This is my CSV
name;destination
file1.txt;folderX
file2.txt;folderY
file3.txt;folderZ
As the comments indicate, if you are not using default system delimiters, you should make sure to specify them.
I also recommend typically to use quotes for your csv to ensure no problems with accidentally including an entry that includes the delimiter in the name.
#"
"taco1.txt";"C:\temp\taco2;.txt"
"# | ConvertFrom-CSV -Delimiter ';' -Header #('file','destination')
will output
file destination
---- -----------
taco1.txt C:\temp\taco2;.txt
The quotes make sure the values are correctly interpreted. And yes... you can name a file foobar;test..txt. Never underestimate what users might do. 😁
If you take the command Get-ChildItem | Select-Object BaseName,Directory | ConvertTo-CSV -NoTypeInformation and review the output, you should see it quoted like this.
Sourcing Your File List
One last tip. Most of the time I've come across a CSV for file input lists a CSV hasn't been needed. Consider looking at grabbing the files you in your script itself.
For example, if you have a folder and need to filter the list down, you can do this on the fly very easily in PowerShell by using Get-ChildItem.
For example:
$Directory = 'C:\temp'
$Destination = $ENV:TEMP
Get-ChildItem -Path $Directory -Filter *.txt -Recurse | Copy-Item -Destination $Destination
If you need to have more granular matching control, consider using the Where-Object cmdlet and doing something like this:
Get-ChildItem -Path $Directory -Filter *.txt -Recurse | Where-Object Name -match '(taco)|(burrito)' | Copy-Item -Destination $Destination
Often you'll find that you can easily use this type of filtering to keep CSV and input files out of the solution.
example
Using techniques like this, you might be able to get files from 2 directories, filter the match, and copy all in a short statement like this:
Get-ChildItem -Path 'C:\temp' -Filter '*.xlsx' -Recurse | Where-Object Name -match 'taco' | Copy-Item -Destination $ENV:TEMP -Verbose
Hope that gives you some other ideas! Welcome to Stack Overflow. 👋

Powershell recursive search to chose .txt files then output contents of all files into a single .txt file

Any help greatly appreciated.
I have a folder that contains 30+ folders which each have a .txt file that I can search for using:
Get-ChildItem -Filter *.txt -Recurse
I want to read the contents of each .txt file discovered and output the contents int a new .csv file on my desktop that also includes the directory of each .txt file contents being displayed.
The question is twofold,
how to use pipe and powershell commands to read/show all the words in the files.
how to create the csv data that will output both the directory name and the contents of the .txt files.
I can already pipe results to:
c:\desktop\test.csv -Encoding ascii -noTypeInformation
The following script reads all .txt files within a specific directory, stores the full filename and path as well as the files content into an array and saves it as a csv.
$csvOut = #()
Get-ChildItem -LiteralPath C:\temp -Filter *.txt -File -Recurse | foreach {
$fileData = #{
"File"=$_.FullName;
"Content"=(Get-Content -LiteralPath $_.FullName -Raw)
}
$csvOut += (New-Object psobject -Property $fileData)
}
$csvOut | Export-Csv -LiteralPath "C:\temp\csvout.csv" -NoTypeInformation

Change file name with excel table

I have a Excel file with the current filename and new filename, both are the whole path to the files. I would like to write a PowerShell script to change the filename.
I have created a csv file but I don't know how to create this within the for each iteration.
With a CSV file with the data like this:
OldName,NewName
C:\folder\file.txt,new_file.txt
C:\folder\song.mp3,different_song.mp3
This code will rename each file:
$files = Import-Csv "C:\folder\rename_files.csv"
foreach ($file in $files){
Rename-Item -Path $file.OldName -NewName $file.NewName -Verbose
}

Powershell script to search folders using .csv files without Path

I am new to the powershell scripting and hence need one help in scripting, the script which I am looking for should search for the folders as per the entries in .csv file, please note that it should search for the folders in the drive without knowing the path and move it to the destination.
I did some research and created below script which is taking data from .txt file and moving the data to the destination however it does not work if I just write C:\ at the place of source folder.
Request you to please help me :)
Get-Content C:\abc.txt |
Foreach-Object {
move-item -path "C:\0123\$_" -destination "C:\To Archive\$_"
}
With what you've given, I'd do something like the following:
$File = Import-Csv C:\share\test\files.txt
Foreach($fileName in $File.FileName)
{
Move-Item -path "C:\share\test\OldLocation\$fileName" -Destination "C:\share\test\NewLocation\$fileName"
}
I did this with a .csv file that had one column whose title was FileName. Notable differences from your code include using the Import-Csv cmdlet and specifying the .csv header title in the foreach loop.
If you wanted to do this with a single command:
Import-Csv C:\share\test\files.txt | ForEach-Object {
Move-Item -path "C:\share\test\OldLocation\$($_.[csvHeader])" -Destination "C:\share\test\NewLocation\$($_.[csvHeader])"
}
Where csvHeader is the title of the column in your .csv file.
My .csv file looked like:
FileName
file1.txt
file2.txt
file3.txt