0

I'm writing a windows powershell script to open/edit a text file.

It has many records and each record is sorted of comma-separated values (csv):

Steps I want to achieve:

  1. Open the text files in a directory on Server A.
  2. Edit the "Date" field with "Current Date" or other.
  3. Save the same text file at the same location(folder).
  4. Copying all the files to a new folder in different Server B.

I've just written this code snippet:

$path = "C:\PSFiles\Rec_File_001.txt" 
$Filedata = Get-Content $path
$Record01 = $Filedata[0].split(",")
$Record01Date = $Record01[3]
$Record01CurrentDate = Get-Date -format yyMMdd
$Record01 -replace $Record01Date, $Record01CurrentDate
Set-Content $path

Please, any help on this?

7
  • Break your question to smaller pieces and ask a specific question about the piece which you have a problem with. Commented Dec 19, 2017 at 7:44
  • I am complete newbie in scripting. The first thing is replacing the date. I will edit the question to be more specific. Thanks Commented Dec 19, 2017 at 7:46
  • To answer your first question- Yes it is possible. then second one to help you out, you can use get-content and you can use the replace Commented Dec 19, 2017 at 7:55
  • The date is not always in the same column in your data example. Is this a mistake or is this how the data really looks like? Commented Dec 19, 2017 at 8:00
  • Thanks for the input. The issue is that firstly, how can i store records in array. Next, when i replace the dates, how can i save the new date in the file. When i use "Set-Content" -path, it deletes all data in the File. Commented Dec 19, 2017 at 8:00

3 Answers 3

1

You are having multiple questions here. I'll address the one that is presented in the title - replacing text in a text file.

The script:

# current date in a new format
$CurrentDate = Get-Date -format yyMMdd

#replace old format
Get-Content -ReadCount 500 -Path C:\PSFiles\Rec_File_001.txt | % {$_ -replace "(0[1-9]|1[012])\d{1,2}(0[1-9]|[12][0-9]|3[01])", "$CurrentDate"} | Set-Content -Path C:\PSFiles_output\Rec_File_001.txt

This takes regexp for date format Date(mmYYdd) and exchanges it for a new one. An option -ReadCount limits the number of lines that go via pipe at one time.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the code. The code is compiled without errors , but the dates are still not repalced physically in the text file. I think maybe im not storing the records correctly in Array or not writing to the file ?
@sunny238: can you post complete file so I can test it?
0
Import-CSV $Path -header text1, text2, text3, date, text5 | 
Select text1, text2, text3, @{Name="Date"; Expression={Get-Date -format yyMMdd}}, text5 |
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content $Path

Or:

$data = Import-CSV $Path -header text1, text2, text3, date, text5
$data | ForEach {"Date" = Get-Date -format yyMMdd}
$data | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content $Path

3 Comments

In original question he stated he did not want to use Import-CSV, now being edited away.
In the original question he stated he did not want to use Import-CSV because... which I think wasn't a valid reason. Header can be added with -header ..., and later suppressed with | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content $Path
yes I can agree. It would be educational for the one asking the question, why it is not a valid reason.
0

Hopefully this helps. Run the following commands in powershell:

$(Get-Item ath/filename.extensionfilename).creationtime=$(get-date "2019-10-15T15:45:12.2723844+01:00")

$(Get-Item C:\temp\log\txt.log).creationtime=$(get-date "2019-10-15T15:45:12.2723844+01:00")

Here is an article from Microsoft that explains how to modify the timestamps of a file https://devblogs.microsoft.com/scripting/use-powershell-to-modify-file-access-time-stamps/

Below is a table from that article showing the attributes that involve time that you can modify.

+----------------+----------+-----------------+---------------------------+
|      Name      |  Member  |      Type       |        Definition         |
+----------------+----------+-----------------+---------------------------+
| CreationTime   | Property | System.DateTime | CreationTime {get;set;}   |
| LastAccessTime | Property | System.DateTime | LastAccessTime {get;set;} |
| LastWriteTime  | Property | System.DateTime | LastWriteTime {get;set;}  |
+----------------+----------+-----------------+---------------------------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.