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
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?
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
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
Summary: 1. yes, i think it's possible using find.exe, but it will impact on the performance severely when replacing string in large file. 2. use for loop in list of strings eg. set list=this is a list ...
Summary: Need to find and replace string in a file using Batch program. It is a text file (but with a different extension simple text and there are blanks in the file). I am a Mainframe programmer and new to B...
Summary: Hello, I want to generate a text-file that lists all folder and subfolders as: c:\foldername1\subfolder1\ and I want to proceed eacht folder line with a string: sting c:\foldername1\subfolder1\ sting...