1

I have a search for an easy answer but not found it. I have a script that works fine however i would like to add time but it wont give me the result i would like .

copy "C:\Temp\ok.txt" "C:\Temp\%date:/=_%_ok.txt"

This gives me "19.02.2018_ok.txt"

I have tried this:

copy "C:\Temp\ok.txt" "C:\Temp\%date~"YYYY-MM-DD":%time~"HH:MM"/=_%_ok.txt"

If possible i would like "2018-02-19 09:59_ok.txt"

Nils
  • 444
  • 3
  • 8
  • 22
  • 1
    `:` is illegal in a filename. Is your date/time format precisely "dd.mm.yyyy" and time "hh:mm" ? These formats are variable at the user's whim. – Magoo Feb 19 '18 at 09:19
  • https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script – CristiFati Feb 19 '18 at 10:21
  • in case you are interested in a [solution independent of locale settings](https://stackoverflow.com/a/18024049/2152082) to get a valid Date-Time string. – Stephan Feb 19 '18 at 10:38
  • @Magoo in my first exemple my output gives me what i want, however i would like to add time. "09-58" or "09.58" is ok for me aswell. "19.02.2018 09.59_ok.txt" would be fine – Nils Feb 19 '18 at 11:44

1 Answers1

0

This is easy to format and copy with Get-Date in PowerShell. When you are confident that the new filename is correct, remove the -WhatIf.

powershell -NoLogo -NoProfile Copy-Item "C:\temp\ok.txt" "C:\temp\$(Get-Date -UFormat '%Y-%m-%d %H:%M_ok.txt')" -WhatIf

Line continuation can be used to make it fit better horizontally.

powershell -NoLogo -NoProfile Copy-Item ^
    -Path "C:\temp\ok.txt" ^
    -Destination "C:\temp\$(Get-Date -UFormat '%Y-%m-%d %H:%M_ok.txt')" ^
    -WhatIf
lit
  • 10,936
  • 7
  • 49
  • 80