\n6202104141728_002030001.csv \n6202104141729_002030002.csv \n6202104141733_002030008.csv \n################^######<\/p>\n<\/blockquote>\n<\/aside>\n
is that ‘3’ (or 2 or 1) always in the same position in the string?<\/p>","upvoteCount":0,"datePublished":"2021-04-17T20:51:08.000Z","url":"https://community.spiceworks.com/t/using-todaydate-as-string/797216/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Yes, always at the same spot. And the length doesn’t change<\/p>","upvoteCount":0,"datePublished":"2021-04-17T20:55:50.000Z","url":"https://community.spiceworks.com/t/using-todaydate-as-string/797216/9","author":{"@type":"Person","name":"plinka","url":"https://community.spiceworks.com/u/plinka"}},{"@type":"Answer","text":"
Thank you kindly! \nThe code does exactly what I want \nAnd I can “read” the code ( “more or less understand what it does”), what is helpful for me.<\/p>","upvoteCount":0,"datePublished":"2021-04-17T21:01:53.000Z","url":"https://community.spiceworks.com/t/using-todaydate-as-string/797216/11","author":{"@type":"Person","name":"plinka","url":"https://community.spiceworks.com/u/plinka"}},{"@type":"Answer","text":"\n\n
<\/div>\n
plinka:<\/div>\n
\nAnd I can “read” the code ( “more or less understand what it does”)<\/p>\n
\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nIf you have questions feel free to ask, we don’t bit… hard ;¬)<\/p>","upvoteCount":0,"datePublished":"2021-04-17T21:12:23.000Z","url":"https://community.spiceworks.com/t/using-todaydate-as-string/797216/12","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
I can handle a digital bite from time to time \nThanks!<\/p>","upvoteCount":0,"datePublished":"2021-04-17T22:06:34.000Z","url":"https://community.spiceworks.com/t/using-todaydate-as-string/797216/13","author":{"@type":"Person","name":"plinka","url":"https://community.spiceworks.com/u/plinka"}}]}}
plinka
(plinka)
April 17, 2021, 7:43pm
1
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
Neally
(Neally)
April 17, 2021, 7:50pm
2
Welcome
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…
Neally
(Neally)
April 17, 2021, 7:57pm
3
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
Neally
(Neally)
April 17, 2021, 8:08pm
4
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
}
}
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?
plinka
(plinka)
April 17, 2021, 8:25pm
5
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
620210414 1733_00203 0008
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
Neally
(Neally)
April 17, 2021, 8:33pm
6
Gotcha, do you have some sample content too?
plinka
(plinka)
April 17, 2021, 8:37pm
7
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)
Neally
(Neally)
April 17, 2021, 8:51pm
8
is that ‘3’ (or 2 or 1) always in the same position in the string?
plinka
(plinka)
April 17, 2021, 8:55pm
9
Yes, always at the same spot. And the length doesn’t change
Neally
(Neally)
April 17, 2021, 8:56pm
10
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
plinka
(plinka)
April 17, 2021, 9:01pm
11
Thank you kindly!
The code does exactly what I want
And I can “read” the code ( “more or less understand what it does”), what is helpful for me.
Neally
(Neally)
April 17, 2021, 9:12pm
12
If you have questions feel free to ask, we don’t bit… hard ;¬)
plinka
(plinka)
April 17, 2021, 10:06pm
13
I can handle a digital bite from time to time
Thanks!