Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#1
Post
by tinfanide » 24 Oct 2011 05:49
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?
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#2
Post
by dbenham » 24 Oct 2011 06:43
Did you look at my answer to your
prior post
Dave Benham
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#3
Post
by tinfanide » 25 Oct 2011 09:26
dbenham wrote:Did you look at my answer to your
prior post
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
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#4
Post
by Ed Dyreen » 25 Oct 2011 10:56
'
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
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#5
Post
by tinfanide » 28 Oct 2011 07:04
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?
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#6
Post
by dbenham » 28 Oct 2011 09:20
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:
Dave Benham
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#7
Post
by tinfanide » 28 Oct 2011 20:49
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:
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
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#8
Post
by dbenham » 28 Oct 2011 21:33
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
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#9
Post
by tinfanide » 29 Oct 2011 05:17
Yes, I see why it is. Thanks very much for ya guidance.