Goodday,

I have a folder with a lot of .csv-files. A part of the name of the csv-file contains the date it is made.
I want to copy some of these csv-files into 1 text-file.
My code below does its job the files with the date YYYYMMDD = 20210417 and the number ‘3’ at a certain place in the filename.

So it works, but I have to manually change the date to ‘Today’ everytime I use this code.
Can someone show me how I automatically use the date of Today (YYYYMMDD)?
And can that DateOfToday also be used as the ‘filename.txt’ (YYYYMMDD.txt)?

I am very new to PowerShell.

Kind regards

cat *20210417*3????.csv | ForEach-object { if("$_" -notlike "bonnr*") { $_ +";3" >> C:\Foldername\filename.txt}}
3 Spice ups

Welcome

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

No worries

CAT is alias in PowerShell, you should use the PowerShell cmdlet rather

PS C:\Users\neally> get-alias cat

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cat -> Get-Content

get-date -f yyyyMMdd

and you can use the same for the filename

e.g.

$today = get-date -f yyyyMMdd
get-content *$today*3*.csv | 
ForEach-object {
    if( $_.name -notlike "bonnr*"){
        $_.fullname | out-file  "$today.txt" -append
    }
}

2021-04-17_15-11-07.png
You see it is putting the whole path in the file, if you just want the name, you can change ‘fullname’ to ‘name’

do you have a sample name of a file name?
I’m a bit confused what that “;3” is supposed to do?

Hi!
Thank you both for replying.

The result of your code is the fileNAMES in a txt.file
The code should get the fileCONTENTS in 1 textfile. My posted code does that. And by skipping the lined that starts with “bonnr”, it doesn’t copy the column headers.

These are examples of file-names, they all have the same length:
6202104141728_002030001.csv
6202104141729_002030002.csv
6202104141733_002030008.csv

6202104141733_002030008
20210414 = date **3 = ‘**file created with computer3’
There are 3 computers, the number 3 in this example could also be a 1 or 2.
I want to add the number (1, 2 or 3) to every line of the text-file. The file will be appended to MS Access table, that’s why I add ;3 so the database recognises it as a column.

And yes, It would also be nice if the 1, 2 or 3 could be automatically be included in the code so I don;t have to use 3 different codes for the 3 different computers :slight_smile:

Gotcha, do you have some sample content too?

I edited my previous post, but in case you missed it:

The result of your code is the fileNAMES in a txt.file
The code should get the fileCONTENTS in 1 textfile. My posted code does that. And by skipping the lined that starts with “bonnr”, it doesn’t copy the column headers.

I add a csv.files, an example of a file that I want to merge into 1 txt-file (except the headers).

Somehow, when I attached a .csv-file, it became a .txt-file. I think this website does that?
And zipped files are not allowed it seems.

6202104101346_002030034.txt (1.05 KB)

is that ‘3’ (or 2 or 1) always in the same position in the string?

Yes, always at the same spot. And the length doesn’t change

idk something like so?

$today = get-date -f yyyyMMdd

$fileContent = 
foreach ($file in (get-childitem *$today*3*.csv)) {
    $pcnum = ($file.name)[-9]
    Get-content $file |
    select-object -skip 1 |
    ForEach-Object{
        $_ + ";$pcnum"
    }
}

$filecontent | 
out-file "FileContent_$today.txt" -force
1 Spice up

Thank you kindly!
The code does exactly what I want :slight_smile:
And I can “read” the code ( “more or less understand what it does”), what is helpful for me.

If you have questions feel free to ask, we don’t bit… hard ;¬)

I can handle a digital bite from time to time :wink:
Thanks!