0

New to this -- I am trying to make a .bat file that will rename all files in a subfolder, (I want this to behave like %folder% rather than using the actual location), by adding a prefix, say ABS_ 123, as well as today's date, _20170818

Right now I have this (pseudo):

@echo off
%subfolder%
for (*) do ren "%%a" "pre %%a"
for (*) do ren "%%~a" "%%~na ("_%%DATE)%%~xa"
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    I'm certain that there are hundreds of examples of how to retrieve and append dates to names on this site alone. Please search, then implement what you've learned instead of the pseudo code. We're happy to help you with your coding issues, just not happy to do it all for you. – Compo Aug 18 '17 at 13:52
  • 2
    Possible duplicate of [How do I get current datetime on the Windows command line, in a suitable format for using in a filename?](https://stackoverflow.com/questions/203090/how-do-i-get-current-datetime-on-the-windows-command-line-in-a-suitable-format) – Mofi Aug 18 '17 at 13:55
  • Thanks, I totally sympathize with that. Trust me, posting the question was not my first resort. I just do not know enough to find what I am looking for and I have been stuck on this for hours. P.S. thanks for the formatting correction. – Wilson Smith Aug 18 '17 at 13:57

1 Answers1

0

I agree with Compos and Mofis comments, but IMO there are a lot of outdated variants proposed here on SO with:

  • locale/user settings dependent %date% and %time% variables or
  • the clumsy to parse wmic variant with it's cr/cr/lf sequences.
  • J-/vbscript solutions require either a temp file or a hybrid version difficult to understand for newbies.

Using/parsing PowerShell comes at a small delay, but as it is usually used only once in a script that shouldn't matter.

You may insert an day offset to get different dates.

@Echo off
For /f %%Y in (
    'powershell -NoP -C "(get-date).AddDays(-1).ToString(\"yyyy-MM-dd\")"'
) Do Set Yesterday=%%Y
Echo Yesterday = %Yesterday%

For /F %%D in (
    'powershell -NoP -C "(get-date).AddDays(0).ToString(\"MM\/dd\/yyyy\")"'
) Do Set "DT=%%D"
Echo Today mdy = %DT%

For /f %%T in (
    'powershell -NoP -C "(get-date).AddDays(+1).ToString(\"yyyy\/MM\/dd\")"'
) Do Set Tomorrow=%%T
Echo Tomorrow  = %Tomorrow%

For /f %%o in (
    'powershell -NoP -C "(get-date).AddDays(0).ToString(\"o\")"'
) Do Set ISO8601=%%o
Echo ISO8601   = %ISO8601%

For /f "delims=" %%U in (
    'powershell -NoP -C "(get-date).AddDays(0).ToString(\"U\")"'
) Do Set UTC=%%U
Echo UTC       = %UTC%

For /f "delims=" %%F in (
    'powershell -NoP -C "(get-date).AddDays(0).ToString(\"F\")"'
) Do Set FullDT=%%F
Echo FullDT    = %FullDT%

Output on my German locale system, day-/month names come in your locale.

Yesterday = 2017-08-17
Today mdy = 08/18/2017
Tomorrow  = 2017/08/19
ISO8601   = 2017-08-18T17:06:46.1050969+02:00
UTC       = Freitag, 18. August 2017 15:06:46
FullDT    = Freitag, 18. August 2017 17:06:46

See this link for more format specifiers

  • Totally agreed on the `wmic` weird CR\CR\LF sequence. How about using `PROMPT $D$T` to get a local independent time format? –  Aug 18 '17 at 15:10
  • @SteveFest Prompt $D **IS** locale/user settings dependent. $T contains fractional seconds (can be erased with $H but is clumsy). See prompt /? –  Aug 18 '17 at 15:20