Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have images with filenames like 0034343.jpg and I want to remove the leading zeros, leaving 34343.jpg as the new filename.

How can I do this in the command prompt? I tried REN 00*.jpg *.jpg, but it didn't work.

share|improve this question

migrated from stackoverflow.com Sep 21 '10 at 8:17

This question came from our site for professional and enthusiast programmers.

    
Do you want ALL leading 0's removed or just 2? What if there are only 1 or no 0's? –  colithium Sep 20 '10 at 11:05
    
just the 00 on the start if no zero remain its original filename –  text Sep 20 '10 at 11:11
    
My usual method is to create a batch file using dir /b and excel formulae. There are probably better ways but I like my way since I understand it unlike batch programming. :) –  Chris Sep 20 '10 at 11:19
add comment

2 Answers

You should be able to start with:

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "delims=*" %%a IN ('dir /b 00*.jpg') do (
    set file=%%a
    set newfile=!file:~2!
    echo move !file! !newfile!
)
endlocal

This will, as it stands, simply echo the move command. Remove the echo once you've finished testing. And keep in mind this may not work as expected if your file names have spaces in them.

Stepping through it bit by bit:

  • The setlocal enables certain cmd.exe extensions, chief here being the ability to do delayed expansion of environment variables inside for loops.
  • The for loop runs the cmd dir /b 00*.jpg, doing one iteration per file (setting %%a to the file name).
  • The first set simply saves the filename into a variable. The second is a substring operation starting at the third character.
  • The move will rename the file.

You can examine the various commands by entering help commands at the command line:

setlocal /?
set /?
for /?
set /?
share|improve this answer
    
I can't say I understand batch scripting that much but I tested and this seems to do the job. :) You might want to mention the removal of "echo" to get it to actually move rather than just display and to put it in a .cmd or .bat file in order to run it for people who have never really done this kind of thing. I'm sure there are plenty of people like that out there... :) –  Chris Sep 20 '10 at 11:22
    
No probs, @Chris, I'm always up for some constructive criticism. Updated to (hopefully) make more useful. –  paxdiablo Sep 20 '10 at 12:07
add comment

Try this:

for %i in (*.jpg) do (
    set filename=%~nxi
    set newname=%filename:~2%
    ren !filename! !newname!
)

This will just strip two characters off the beginning of every file name. Delayed expansion must be enabled. Might need to do cmd.exe /v:on first on XP.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.