简体   繁体   中英

Windows: Renaming files within cmd

I am trying to come up with a script to do the following but I do not have and Windows Coding experience.

I am trying to achieve the following:

I have a folder with multiple images in it and I want to rename it with a sequence like this

2012.000.value

Were value is I would like it to be able to enter a value before the script is run and the first image will be that number and then it will count up renaming that folder of images with that value.

Some images may have a file name called Original and I would like to retain that value but also insert the above sequence into the file name.

The images will be .jpg

@echo off
setlocal enabledelayedexpansion
set count=0
for /d %%x in (%date:~-4%.000.???) do (
  for /f "tokens=3 delims=." %%y in ("%%x") do (
    set count=%%y
    for %%z in (%%x\*.jpg) do (
      ren %%~dpnxz !count!.jpg
      set /a count=count+1
    )
  )
)
endlocal

The above code assumes the directories will be in the format of %CurrentYear%.000.??? . If you want multiple years, you'll need different code.

EDIT: This code will NOT output 000.jpg or 001.jpg , it will output 0.jpg or 1.jpg . The code at the bottom will deal with that.

Also it will completely drop the original file name for ###.jpg and will work with any value. If you want the original filename preserved like so #-FileName.jpg change the ren line to:

      ren %%~dpnxz !count!-%%~nz.jpg

The code will not sort the files, but will deal with them in the order that they were created in. If you want them alphabetized, then change the for %%z in (%%x\\*.jpg) do ( line to:

  for /f "tokens=*" %%z in ('dir /o:n %%x\*.jpg') do (

EDIT: This code will output numbers of consistent length, limited by user input.

To use this code, you need to pass it how many characters you want the maximum number to be. So...

IMG 2

...can rename the files from 00.jpg to 99.jpg and...

IMG 4

...can rename the files from 0000.jpg to 9999.jpg .

IMG.BAT

@echo off
setlocal enabledelayedexpansion
:: Set how many characters the number can be. IE 00 = 2, 0000 = 4
set places=%1
set count=0
set zeros=
for /l %%x in (1,1,%places%) do set zeros=!zeros!0
for /d %%x in (%date:~-4%.000.???) do (
  for /f "tokens=3 delims=." %%y in ("%%x") do (
    set count=%%y
    for %%z in (%%x\*.jpg) do (
      :: Make give !count! leading 0's if nessicary
      set count=%zeros%!count!
      :: Trim !count! down to a 4 digit number
      set count=!count:~-%places%!
      ren %%~dpnxz !count!.jpg

      :: Trim ALL 0's on the left to avoid calculation problems.
      set /a n=places-1
      for /l %%a in (0,1,!places!) do if "!count:~0,1!"=="0" set count=!count:~1!
      set /a count=count+1
    )
  )
)
endlocal

I prefer this method, because Windows Explorer normally sorts files alphabetically, so this would be the file order of 1.jpg 2.jpg 3.jpg 10.jpg 20.jpg

1.jpg
10.jpg
2.jpg
20.jpg
3.jpg

But if those numbers are fixed at 3 characters width with leading 0's, the alphabetical order of them would be:

001.jpg
002.jpg
003.jpg
010.jpg
020.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM