Search code examples
powershellcsvtimestamparchive

"Archiving" csv into archive folder


I have a script that runs a sql query and then output the data to

C:\audit\results.csv

If this was run today, I'd like the file to be saved like the following in addition to the location above.

C:\audit\archive\results_20130313_0243.csv

In my current script I have the following, however it seems cumbersome and one too many steps.

$archiveName = "results $(get-date -f yyyy-MM-dd).csv"
Copy-Item "C:\audit\results.csv" "C:\audit\archive\results.csv"
Rename-item "C:\audit\archive\results.csv" $archiveName

Solution

  • Indeed. Something like this should suffice:

    cp "C:\audit\results.csv" "C:\audit\archive\results_$(get-date -f yyyyMMdd_HHmm).csv"