Solved add characters to front of a file name

October 31, 2011 at 07:38:32
Specs: Windows XP
I'm working on a batch file to "0" to the font of video file, that all varying in name length. but after my file has ran I would like the them to be 11 characters not counting the file extension.


See More: add characters to front of a file name

Report •

October 31, 2011 at 08:58:41
Hi alynch

This assumes the extension length is 3 charactors
I,m remmed out the "Ren" command for safety, Try on dummy files first.

@echo off
SetLocal EnableDelayedExpansion
type nul>~Temp.txt
dir /b *.vid >>~Temp.txt
rem type ~temp.txt
for /f "tokens=* delims=" %%a in (~Temp.txt) do (
call :AddLeadZeros %%a 15
rem ren %%a !Num!
del ~Temp.txt
exit /b

:AddLeadZeros %1 %2
set Num=
call :StrLen %1
set NumLen=%Len%
set StrLen=%2
set /a NoZeros=%StrLen%-%NumLen%
for /L %%a in (1,1,!NoZeros!) do (
set Num=!Num!0
set Num=!Num!%1
exit /b

:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" (
set /a Len=%%a+1
) else (exit /b)
exit /b

Report •

October 31, 2011 at 09:12:33
This did not work for me. I un-Rem the commands and replace *.vid with *.mpg.

thanks for your help.

Report •

October 31, 2011 at 22:34:12
✔ Best Answer
Please test this other option.

@echo off
setlocal enabledelayedexpansion

set zeros=0000000000

pushd Path\to\files\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /b *.mpg') do (
    set newname=!zeros!%%1&set newname=!newname:~-15!
    echo !newname!
    REM ren "%%1" "!newname!"

Please come back & tell us if your problem is resolved.

Report •

Related Solutions

Ask Question