|
Answer» Hi there, I can't really work good with the command FOR under windows xp so if sombody could help me with the following thing I would try to reach (or if somebody have a link to a place where I can learn the command). I want to make a batch file, that search in every file, of the same folder where the batch file is placed in, to the word: "boom" and REPLACE it by "aplle". Can anybody help me pleas? P.S. the extentions and file names doesn't mather, so you can use *.*)This may help you out. If batch language gets any more cryptic than this, I have yet to see it :-/
Code: [Select]@echo off setlocal enableextensions setlocal enabledelayedexpansion for /f %%X in ('dir /a:-d /s /b') do ( for /f "tokens=* delims=" %%i in (%%x) do ( set input=%%i set input=!input:boom=aplle! echo !input! > %%x.chg ) ) endlocal
As far as documentation for the FOR command, type for /? at a command prompt. It's as good as any.
Good luck. 8-)
Note: To keep it simple, I just appended a .chg extension to the file name. You may want to change this.First of all, thanks for the help and the script so far yet. I appreciate your help.
Second, could it be that there is a fault in the batch script. Because when I use the script, the following thing happens (with only one file to replace in the foulder):
The computer makes two extra files, one with as name: filename of the replaced file + extention. (so i get test.txt.txt) and one, text file, with the same name as my batch file.
I'm still trying to learn the command, but if I may ask you why is the command "setlocal enabledelayedexpansion" in the script.
Anyhow, thanks for the help so far
greetz
Blackberry 8-)The setlocal enabledelayedexpansion allows the user to set an environment variable inside of a block of code and then REFERENCE it inside of the block with the set value.
I tried to keep the solution simple. In the real world the batch file would not be in the same directory (otherwise it gets caught up in the DIR collection of filenames; you would have to reference a path to the directory where the files live) as the files it's acting on. It is also possible to parse the filenames and addon WHATEVER extension you like.
Text files cannot be opened as input output which explains why you end up with separate output files. The original files remain unchanged.
Good luck. 8-)Sidewinder's script snippet will replace boom with apple but will also remove all empty LINES from the files. If this side effect needs to be avoided then replace
for /f "tokens=* delims=" %%i in (%%x) do (
with
for /f "tokens=1,* delims=]" %%h in ('"find /n /v "" "%%x""') do (
Hope this information is useful
|