0

I have the following code that export a CSV-file from a query. How do I add a "Get-date" (today) stamp in the end of the file in the filepath?

$ds.Tables[0] | Export-csv -delimiter ";" -path "C:\temp\TempWarehousingControl.csv" -NoTypeInformation -Force -Encoding UTF8 
Get-Content C:\temp\TempWarehousingControl.csv | %{ $_ -replace """""", "NULL"} |%{ $_ -replace """", ""} | out-file -FilePath C:\Batch\Output\WarehousingControl\WarehousingControl.csv -Force -Encoding utf8
1
  • 1
    What do you mean? Append a timestamp to the CSV file itself or its name? Commented Mar 29, 2017 at 8:02

2 Answers 2

1

Assuming that you meant "how do I add today's date to the end of the filepath" (vs the end of the file), this will do it:

"C:\Batch\Output\WarehousingControl\WarehousingControl-$(get-date -Format 'dd-MM-yyyy').csv"

Included in your full code:

$ds.Tables[0] | Export-csv -delimiter ";" -path "C:\temp\TempWarehousingControl.csv" -NoTypeInformation -Force -Encoding UTF8 
Get-Content C:\temp\TempWarehousingControl.csv | %{ $_ -replace """""", "NULL"} |%{ $_ -replace """", ""} | out-file -FilePath "C:\Batch\Output\WarehousingControl\WarehousingControl-$(get-date -Format 'dd-MM-yyyy').csv" -Force -Encoding utf8

Explanation:

This is a subexpression $() which uses get-date to get the current date and formats it as dd-MM-yyyy (note month is a capital M as lowercase m is the code for minute).

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

5 Comments

Thanks! I do though get a syntax error on the last line. I've wrote it like this: Get-Content C:\TEMP\TransactionsControl\TempLargeWithdrawals.csv | %{ $_ -replace """""", "NULL"} |%{ $_ -replace """", ""} | out-file -FilePath "C:\Batch\Output\WarehousingControl\LargeWithdrawals-$(get-date -Format "dd-MM-yyyy").csv" -Force -Encoding utf8
Sorry should have used single quotes around the dd-MM-yyyy part. Try this: "C:\Batch\Output\WarehousingControl\WarehousingControl-$(get-date -Format 'dd-MM-yyyy').csv"
Still syntax error :/. Something else that might be incorrect?
Hmm Where can I see that? It only says, "Syntax error on line 48", i.e. that line!
There should be several lines of error text including a line that points to where the error is. Perhaps add a screenshot to your question.
0
$path = "C:\Batch\Output\WarehousingControl\WarehousingControl.csv"
$path = $path.Replace(".csv", "_" + (Get-Date -f "yyy-MM-dd") + ".csv")

$ds.Tables[0] | 
    Export-csv -delimiter ";" -path "C:\temp\TempWarehousingControl.csv" -NoTypeInformation -Force -Encoding UTF8 

Get-Content C:\temp\TempWarehousingControl.csv | 
    %{ $_ -replace """""", "NULL"} |
    %{ $_ -replace """", ""} | 
    Out-File -FilePath $path -Force -Encoding utf8

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.