Import entire Folder as text

bobaol

Board Regular
Joined
Jun 3, 2002
Messages
225
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
Hello, I have a lot of files with a lot of dirty data. I want PowerQuery to import everything as text, regardless of the data. I tried this, but it is not working. I don't need PowerQuery to parse the data, although I want to keep the file name in column A.
let
Source = Folder.Files("C:\Temp2\6267b"),
#"Filtered Hidden Files1" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true),
#"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
#"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
#"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File"}),
#"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File")))
in
#"Expanded Table Column1"
''---------------------------------------------------------
I tried this for a single file, and it works, but I have a few thousand files.
let
/* Get the raw line by line contents of the file, preventing PQ from interpreting it */
fnRawFileContents = (fullpath as text) as table =>
let
Value = Table.FromList(Lines.FromBinary(File.Contents(fullpath)),Splitter.SplitByNothing())
in Value,

/* Use function to load file contents */
Source = fnRawFileContents("C:\Temp2\count_Distinct_6267\test6266.csv")
/*Source = fnRawFileContents("C:\Temp2\count_Distinct_6267\")*/


any help is appreciated. Thanks in advance.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Power Query:
let
    Source = Folder.Files("C:\Temp2\6267b"),
    csv_files = Table.SelectRows(Source, each ([Extension] = ".csv"))[[Content], [Name]],
    lines = Table.TransformColumns(csv_files, {"Content", Lines.FromBinary})
in
    lines
then expand list column to get your lines of text.
 
Upvote 0
Hi AlienSx, thanks so much for your reply. The code above only provides the file name in column A. Will you be able to provide the complete code so it imports all the data and have the file name in column A? all the data can simply go into column B. here is a sample of the data:


CODE_LEVEL
1​
2​
3​
4​

Thank you.
 
Upvote 0
what "Column A" are you talking about? Source = Folder.Files... gives you a table of file contents and with several attributes like Name, Content etc. Then csv_files selects all files with .csv extension while progression ([[Content], [Name]]) selects only Name (file name) and Content columns of Source step. Then lines step simply reads all text lines in your csv files and creates lists out of them. I am sorry, I don't get your message at all.
 
Upvote 0
Hi AlienSx, thanks again for the response. When I ran the code you provided, it only returns the name of the file, it is not returning the data in the file. so, for example, in the first file, i want to return the name of the file in row2 to row6, and in column B2 to B6, return the data inside the file. sorry for the confusion. thank you.
 
Upvote 0
@bobaol I am sorry, I still don't follow you. You wrote that you don't want to do any transformations, just read text from your (csv?) files. I downloaded simple csv file from internet. Placed it into some folder "tmp". Here is what happens next in PQ:
read_folder.jpg

csv_files.jpg

lines.jpg

my code reads the content of that file. I am sorry, I am lost.
 
Upvote 0

Forum statistics

Threads
1,223,872
Messages
6,175,100
Members
452,613
Latest member
amorehouse

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top