Computing.Net > Forums > Programming > Replace String in .bat file

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.

Replace String in .bat file

Reply to Message Icon

Name: googlexx
Date: May 24, 2009 at 02:11:06 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

how can i replace every space in a file or variable with an underscore?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 24, 2009 at 03:23:54 Pacific
Reply:

@echo off & setLocal EnableDelayedExpansion

set /p str= string ?
set str=!str: =_!
echo !str!


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: googlexx
Date: May 24, 2009 at 03:40:53 Pacific
Reply:

do i replace my variable with string?


0

Response Number 3
Name: Mechanix2Go
Date: May 24, 2009 at 03:48:31 Pacific
Reply:

Not sure I'm with you. As written, it asks for a string. You could hardwire it.

Or you can have it process a file:

==============================
@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set str=%%a
set str=!str: =_!
echo !str!>> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: googlexx
Date: May 24, 2009 at 03:54:42 Pacific
Reply:

awesome got it working. thanks dude!

one more question. i have a file with a .w3g extension. How would i replace all the spaces in the file name with underscores and then save the file again?


0

Response Number 5
Name: Mechanix2Go
Date: May 24, 2009 at 04:39:58 Pacific
Reply:

@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b "my own.w3g"') do (
set str=%%~Na
set str=!str: =_!
ren "%%a" "!str!%%~Xa"
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Related Posts

See More



Response Number 6
Name: googlexx
Date: May 24, 2009 at 05:21:38 Pacific
Reply:

thanks, but for some reason when the name has a ! in it it doesn't work.. any1 way to fix this?


0

Response Number 7
Name: Mechanix2Go
Date: May 24, 2009 at 06:42:10 Pacific
Reply:

Some chars, including !, have special meaning in scripts. So we don't waste much more time, how about posting a few filenames.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 8
Name: Judago
Date: May 24, 2009 at 06:59:29 Pacific
Reply:

Here's a quick patch-up of M2's script to allow !'s.


@echo off
setlocal disabledelayedexpansion
for /f "tokens=* delims= " %%a in ('dir/b "my own.w3g"') do (
set str=%%~Na
setlocal enabledelayedexpansion
set str=!str: =_!
ren "%%a" "!str!%%~Xa"
endlocal
)
endlocal


0

Sponsored Link
Ads by Google
Reply to Message Icon

How to call one batch fil... C++ programing problem!



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Replace String in .bat file

nt batch to replace string in srt file www.computing.net/answers/programming/nt-batch-to-replace-string-in-srt-file/19260.html

find and replace string in a file www.computing.net/answers/programming/find-and-replace-string-in-a-file/19987.html

add or replace text in .txt file www.computing.net/answers/programming/add-or-replace-text-in-txt-file/18121.html