0

I have a line in a batch file that renames a file with a date and time appended to it.

rename "C:\Program Files (x86)\File Directory\sub directory\logs\Backups\Client.txt" Client%date:~7,2%%date:~4,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%.txt

This works fine, except if the first time parameter (%time:~0,2%) is a single digit. It will error with an incorrect syntax command. I understand why it occurs (there's a similar post here) but can't seem to get the correct syntax to make the command run successfully when the hour parameter is a single digit (between 01-09).

What command syntax do I need to add to make sure the command works with single digits for the hour ?

Stephan
  • 53,940
  • 10
  • 58
  • 91
CrazyAwk
  • 41
  • 6
  • There are dozens of posts here on this topic, including one earlier today. However, this should help you [Stampme.cmd](https://ss64.com/nt/syntax-stampme.html) – Qwerty Jan 18 '23 at 15:41
  • Thanks. This is slightly more complex than I need. I ended up going with another solution which was to put a set command before the rename statement. Code now looks like this and works as required. set hour=%time: =0% rename "C:\Program Files (x86)\File Directory\sub directory\logs\Backups\Client.txt" Client%date:~7,2%%date:~4,2%%date:~10,4%%hour:~0,2%%time:~3,2%%time:~6,2%.txt – CrazyAwk Jan 18 '23 at 16:00
  • That is going to be locale dependent. The `%date%` variable is not reliable across systems. See [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – Qwerty Jan 18 '23 at 16:02
  • I'm not so fussed with the date format, it's the time piece that wasn't working properly. If at some stage the date format gives me a grief I'll certainly look at the link you provided. Thxs – CrazyAwk Jan 18 '23 at 16:04
  • The same limitations for the `%date%` variable also apply to the `%time%` variable. Here is a quick way to get the date and time as the variable `%_dateTime%`: `for /f "tokens=1-6 delims=/: " %%a in ('%__APPDIR__%Robocopy.exe "|" . /njh ^| find ":"') do set "_dateTime=%%a-%%b-%%c-%%d:%%e:%%f"` – Qwerty Jan 18 '23 at 16:21
  • You could then issue your `rename` command to make the file `Client%_dateTime%.txt` – Qwerty Jan 18 '23 at 16:23

1 Answers1

0
set hour=%time: =0%
rename "C:\Program Files (x86)\File Directory\sub directory\logs\Backups\Client.txt" Client%date:~7,2%%date:~4,2%%date:~10,4%%hour:~0,2%%time:~3,2%%time:~6,2%.txt
CrazyAwk
  • 41
  • 6
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 19 '23 at 02:19