i need help
i need help

Reputation: 2386

batch rename multiple files in windows?

I wonder what's wrong with my coding, because it's not working

I want to rename all the png files inside Chris.

but it failed

for /f  in ('C:/Users/Chris/Downloads/images/*.png')
 do ren "C:\Users\Chris\Downloads\images\*.png" "%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%-img.png"

Upvotes: 1

Views: 277

Answers (2)

SomethingDark
SomethingDark

Reputation: 14304

If order to use a for loop, you need to specify a variable to use (even if you don't use a variable in the loop at all), otherwise you'll get a syntax error. While variables can only be one letter, this is pretty much the only time in batch that variables are case-sensitive, so you've got 52 letters, plus a few additional characters that I've seen used, like #. Additionally, do must always be on the same line as the ).

A for /F loop can process strings, text files, and other batch commands.

  • To process strings, use double quotes: for /F %%A in ("hello world") do echo %%A
  • To process batch commands, use single quotes: for /F %%A in ('dir /b') do echo %%A
  • To process text files, do not use any quotes at all: for /F %%A in (C:\Users\Chris\image_list.txt) do echo %%A

You may also want to go into the directory that you're processing just to make things easier.

pushd C:\Users\Chris\Downloads\images
for /F %%A in ('dir /b *.png') do (
    REM I'm not sure what the %HR% variable is supposed to be, so I'm ignoring it.
    ren "%%A" "%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%-img.png"
)

Upvotes: 2

Dlanod Kcud
Dlanod Kcud

Reputation: 537

No need for /f in argument, no need for quotes but your missing a variable declaration

The variable should be used in the do-part otherwise the for is not realy helpful

the for will enumerate the full path so you need to strip the filename using ~n

the do-part must be directly behind the for-statement or it needs to be inside round brackets

here's the complete code:

for %%i  in (C:/Users/Chris/Downloads/images/*.png) do (
   ren "%%i" "%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%-%%~niimg.png"
)

Upvotes: 2

Related Questions