Page 1 of 1

Batch rename files under a directory to replace spaces

Posted: 24 Oct 2011 05:49
by tinfanide

Code: Select all


@echo off
dir /b /a-d *.* > c:\temp.txt
for /F "tokens=*" %%* in (c:\temp.txt) do call :sub %%*
del c:\temp.txt
goto :eof

:sub
ren "%*" %1%2%3%4%5%6%7%8%9



I want to replace the spaces in filenames with "_" (underlines)
And I alter the codes a bit like this:

Code: Select all

:sub
ren "%*" %1_%2_%3_%4_%5_%6_%7_%8_%9


But not all of my filenames count up to 9 tokens
I wonder how I can do it for all files with different filename lengths?

Re: Batch rename files under a directory to replace spaces

Posted: 24 Oct 2011 06:43
by dbenham
Did you look at my answer to your prior post :?:

Code: Select all

set name=%%*
set name=!name: =_!


Dave Benham

Re: Batch rename files under a directory to replace spaces

Posted: 25 Oct 2011 09:26
by tinfanide
dbenham wrote:Did you look at my answer to your prior post :?:

Code: Select all

set name=%%*
set name=!name: =_!


Dave Benham


Sorry, I didn't. I'm very green in BAT though.
Even after I'd looked at your codes.
This is the best I could work out with little sense of BAT. It doesn't work though (parameters problem)

Code: Select all

@echo off
setlocal disableDelayedExpansion
for /f "delims=" %%* in (test.txt) do (

set "name=%%*"

setlocal enabledDelayedExpansion

echo(!name: =_!
endlocal


)

pause

Re: Batch rename files under a directory to replace spaces

Posted: 25 Oct 2011 10:56
by Ed Dyreen
'
I took some errors out, maybe it works now:

Code: Select all

@echo off &setlocal DisableDelayedExpansion

for /f "delims=" %%! in (test.txt) do (
   set "name=%%!"
   setlocal enabledDelayedExpansion
      echo(!name: =_!
   endlocal
)

pause

Re: Batch rename files under a directory to replace spaces

Posted: 28 Oct 2011 07:04
by tinfanide
Well... the codes your guys helped me with only illustrates by echoing the desired file names:

file 1.jpg -> file_1.jpg

but I don't just want to echo the new file name in CMD.

I tried this one but am just stuck on the way to use REN

Code: Select all

@echo on
setlocal disableDelayedExpansion
for %%L in (*.*) do (
  set "ln=%%L"
  setlocal enableDelayedExpansion
  echo(!ln: =_!
  endlocal
:: here, I have no idea of how to use variables in REN
   ren ??? ???
)
pause


So could ya please help further a bit?

Re: Batch rename files under a directory to replace spaces

Posted: 28 Oct 2011 09:20
by dbenham

Code: Select all

@echo on
setlocal disableDelayedExpansion
for %%L in (*.*) do (
  set "ln=%%L"
  setlocal enableDelayedExpansion
  ren !ln! !ln: =_!
  endlocal
)
pause
Since this is new to you, it is important that you realize the string search and replace does not modify the content of the variable - it just modifies the result of the variable expansion.

If you wanted to modify the variable content you would need to do something like this:

Code: Select all

set "ln=!ln: =_!"


Dave Benham

Re: Batch rename files under a directory to replace spaces

Posted: 28 Oct 2011 20:49
by tinfanide
dbenham wrote:

Code: Select all

@echo on
setlocal disableDelayedExpansion
for %%L in (*.*) do (
  set "ln=%%L"
  setlocal enableDelayedExpansion
  ren !ln! !ln: =_!
  endlocal
)
pause
Since this is new to you, it is important that you realize the string search and replace does not modify the content of the variable - it just modifies the result of the variable expansion.

If you wanted to modify the variable content you would need to do something like this:

Code: Select all

set "ln=!ln: =_!"

Dave Benham


I did some research online and kinda adapted from some others' codes:

Code: Select all

@echo on

setlocal enabledelayedexpansion

for %%i in (*.*) do (
   set old_name=%%i
   set new_name=!old_name: =_!
   move "!old_name!" "!new_name!"
)

pause


And my codes turned out to be:

Code: Select all

@echo on
setlocal enableDelayedExpansion

for %%L in (*.*) do (
  set old=%%L
  set new=!old: =_!
  ren "!old!" "!new!"
 
)
pause


But I think I've got your point of set a variable = a variable expansion within delayedExpansion
but just in my newer codes I set the whole for loop within the delayedExpansion, the part which
I have few ideas of

Re: Batch rename files under a directory to replace spaces

Posted: 28 Oct 2011 21:33
by dbenham
tinfanide wrote:And my codes turned out to be:

Code: Select all

@echo on
setlocal enableDelayedExpansion

for %%L in (*.*) do (
  set old=%%L
  set new=!old: =_!
  ren "!old!" "!new!"
 
)
pause

That will work fine as long as none of your file names contain !

This line - set old=%%L - will corrupt the results if %%L contains ! and delayed expansion is enabled.

Dave Benham

Re: Batch rename files under a directory to replace spaces

Posted: 29 Oct 2011 05:17
by tinfanide
Yes, I see why it is. Thanks very much for ya guidance.