-1

I found the below code which works in :

FOR /R "C:\Folder" %Z IN (*.tif) DO @( FOR /F "Tokens=1-6 delims=:-\/. " %A IN ("%~tZ") DO @( ren "%~dpnxZ" "%~A_%~B%~C_%~D%~E_%~nZ%~xZ") )

However, I have got two problems.

  1. I need to run it in
  2. I cannot get seconds or even milliseconds as filename
Compo
  • 36,585
  • 5
  • 27
  • 39
user2306468
  • 157
  • 1
  • 1
  • 5

2 Answers2

0

Inside a the % characters require doubling:

@For /R "C:\Folder" %%G In (*.tif)Do @For /F "Tokens=1-5Delims=:-\/. " %%H In ("%%~tG")Do @Ren "%%G" "%%~H_%%~I%%~J_%%~K%%~L_%%~nxG"

Although, in a batch file, I'd suggest that you do not use a single line, (limiting line lengths within 80 characters to match the default cmd.exe window width and to assist reading):

@For /R "C:\Folder" %%G In (*.tif)Do @For /F "Tokens=1-5Delims=:-\/. " %%H In (
    "%%~tG")Do @Ren "%%G" "%%~H_%%~I%%~J_%%~K%%~L_%%~nxG"

BTW, your initial example could have looked more like this:

For /R "C:\Folder" %G In (*.tif)Do @For /F "Tokens=1-5Delims=:-\/. " %H In ("%~tG")Do @Ren "%G" "%~H_%~I%~J_%~K%~L_%~nxG"

You had some unnecessary parentheses and an unused token and could have merged the name and extension into a single modifier instead of two. You could also have removed the unnecessary expansion and modifiers from the filename to be renamed.


[Edit /]
BTW, you'll note that I didn't answer your second question, because the %~t modifier doesn't output seconds and milliseconds, (you could therefore probably remove the . delimiter), so you cannot retrieve it from that code. You would therefore need to use an alternative method, and this site does not provide a free code writing service, so you'd need to post a new question once you have that code. Also problems/issues should be limited to only one per post.
Compo
  • 36,585
  • 5
  • 27
  • 39
  • @user2306468: you can find a method [here](https://stackoverflow.com/a/18024049/2152082) (replace `LastModified` with `CreationDate`) – Stephan Jan 22 '20 at 12:14
  • But @user2306468, please read [this Super User question](https://superuser.com/q/937380) and answers first. – Compo Jan 22 '20 at 12:36
0

I have used powershell to rename the file at the end

dir $args[0] | foreach {
  $newname = "$($_.CreationTime.toString('yyyyMMdd-HHmmssfff'))-$($_.Name)"
  if (test-path $newname) {
    "Cannot rename $_"
  } else {
    ren $_ $newname 
  }
}
user2306468
  • 157
  • 1
  • 1
  • 5