Computing.Net > Forums > Programming > Rename file with folder name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Rename file with folder name

Reply to Message Icon

Name: CMarini
Date: September 12, 2009 at 22:37:10 Pacific
OS: Windows Vista
Subcategory: Batch
Comment:

Maybe it's my lack of knowledge of batch
syntax, but I need some help figuring this out:
I have a folder with a bunch of subdirectories
and this batch file. I want to rename each file
within those subdirectories to their respective
folder names, plus a sequential number if there
is more than one file in that subdirectory, and
then their proper file extension.

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: CMarini
Date: September 13, 2009 at 00:00:35 Pacific
Reply:

It appears that I was able to get everything working except the
sequential numbers. I can't figure out why my counter variable
isn't counting! :(

@echo off

for /d %%x in (*.*) do (
call :Rename %%x
)
goto End

:Rename %*
set DirName=%*
set Count=1
echo.
echo Initial Count: %Count%
for %%f in ("%DirName%\*") do (
ren "%%f" "%DirName%_%Count%%%~xf"
set /a Count+=1
echo Incremented Count: %Count%
)
exit /b

:End
pause


0

Response Number 2
Name: Mechanix2Go
Date: September 13, 2009 at 03:17:17 Pacific
Reply:

You probably need:

@echo off & setLocal EnableDELAYedExpansion

Then use !count!

[not tested]


=====================================
Helping others achieve escape felicity

M2


0

Response Number 3
Name: Mechanix2Go
Date: September 13, 2009 at 04:09:54 Pacific
Reply:

@echo off & setLocal EnableDELAYedExpansion

for /f "tokens=* delims= " %%a in ('dir/s/b/ad') do (
  set N=
  pushd %%a
  set str=%%a
  call :sub1

for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^>nul') do (
  set /a N+=1
  echo ren "%%f" "!str!_!N!%%~Xf"
)
)

goto :eof

:sub1 get last tok
  :loop
    for /f "tokens=1* delims=\" %%i in ("!str!") do (
      if "%%j" neq "" (
      set str=%%j
      goto :loop
      )
    )
goto :eof


=====================================
Helping others achieve escape felicity

M2


0

Response Number 4
Name: CMarini
Date: September 14, 2009 at 21:58:57 Pacific
Reply:

Okay, I was able to get it working except for one problem, If
there is an exclamation point (!) in the subdirectory or file
name, it will not work. This is my current code:

@echo off
setLocal EnableDELAYedExpansion

for /d %%x in (*.*) do (
call :Rename %%x
)
goto End

:Rename %*
set DirName=%*
set count=1
echo.
echo Initial count: %count%
for %%f in ("%DirName%\*") do (
if !count! equ 1 ren "%%f" "%DirName%%%~xf"
if !count! gtr 1 ren "%%f" "%DirName% (!count!)%%~xf" 
set /a count+=1
echo Incremented count: %count%
)
exit /b

:End


0

Response Number 5
Name: Mechanix2Go
Date: September 14, 2009 at 23:12:06 Pacific
Reply:

Yep, some chars have 'special meaning'. I would just get rid if the ! in the filenames.

not elegant


=====================================
Helping others achieve escape felicity

M2


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Rename file with folder name

rename files and folder to lowercase with b www.computing.net/answers/programming/rename-files-and-folder-to-lowercase-with-b/19161.html

Renaming files using name of subdirectories www.computing.net/answers/programming/renaming-files-using-name-of-subdirectories/19880.html

rename file with batch www.computing.net/answers/programming/rename-file-with-batch/16851.html