batch replace array of files

June 8, 2009 at 08:38:15
Specs: Windows XP, 2 gb
I have two lists of text files:

List met(s)
*1.met
*2.met
…..
*n.met

List upl(s)
*1.upl
*2.upl
…..
*n.upl

The two lists of files (arrays) are of the same length: i.e. the same number of files.
I want to use each names of file in list A (*.met) to substitute a fixed string in each corresponding file in list B: in other words for file *1.met I want to store its complete name in a variable (dynamic?) and use it to find and replace a string in file *1.upl (let’s say that the string to be substituted in the recipient file - *1.upl - is “abcde”); for file *2.met and *2.upl I want to do the same and so on. I have in total 366 files with one occurrence – string substitution - per file (the string to be substituted in files *.upl is always the same i.e. “abcde”)
I kwon somehow by the help of this great forum how to deal with the string substitution in one file

@echo off
setLocal EnableDelayedExpansion

set str=oldsting
for /f "tokens=*" %%a in (file) do (
set row=%%a
set row=!row:%str%=newstring!
echo.!row!>>newfile
)

But I’m really in trouble in the case above mentioned because there is the need to combine string substitution of many files and variable assignement

Any help much appreciated



See More: batch replace array of files

Report •


#1
June 8, 2009 at 16:49:28
so where are specifying the string to substitute? in other words, how do you know 1.met is suppose to use "abcde"?

Report •

#2
June 8, 2009 at 23:38:41
sorry ghostdog

I do not completely understand your questions but I'll try to answer and make myself clearer (it is certainly my fault)

the *.upl files are a sort of template calling the *.met files in a specific procedure I'm not mentioning here for the sake of coinciseness;

each *.upl file has a general string which is supposed to address the path of each corresponding *.met file;
my need is to change the general string "abcde" in each single *.upl file so that is pointing to the correct *.met file; and to do that I just need to change the final part of the path string i.e. the name of corresponding *.met file

for example:
*1.upl has a string like c:\....\....\....\abcde.met
that I need to change into c:\....\....\....\*1.met

the same for *2.upl and so on...

in genral terms the substituting string is the corresponding name of the file *.met and the string to be sostituted in the *.upl files is always the same fixed string "abcde"

hope this clarifies


Report •

#3
June 9, 2009 at 00:14:48
what does the asterix stand for? do you know * is an illegal character and no way you can name your file with an asterix???
anyway, i take it that it doesn't matter , what you want is all .upl files.
here's a Python script, if you can use Python on windows.
import glob,os,fileinput
for files in glob.glob("*.upl"):
    name,ext=os.path.splitext(files)    
    for line in fileinput.FileInput(files,inplace=1):
        line=list(os.path.split(line.strip()))        
        line[-1]=name+".met"
        print os.sep.join(line)

output:

C:\test>more 1.upl
c:\....\....\....\abcde.met

C:\test>more 2.upl
c:\....\....\....\...\...\abcde.met

C:\test>python test.py

C:\test>more 2.upl
c:\....\....\....\...\...\2.met

C:\test>more 1.upl
c:\....\....\....\1.met

here's also an executable, if you don't want to install Python.


Report •

Related Solutions

#4
June 9, 2009 at 00:44:46
yes, certainly I know about the asterisk limitation of file naming but it was just for simplifying the problem (in fact all files have a real proper name)

thank you for the python code but unfortunately I can not install it on the machine I'm actually working on; I can rely just on "native" resources like batch scripting!

anyway it's a good piece of code for my future python education; one day or the other I'll seriously switch to the study of a proper programming language but for now I have to wait (I'm hoping) for a batch tip...

cheers


Report •

#5
June 9, 2009 at 01:56:49
hi ghostdog

I've been trying your executable but it not seems working fine because it is substituting also all other lines of the upl files;
I just want one single line to be substituted: i.e. where is the occurence of the string abcde.met

thanks anyway


Report •

#6
June 9, 2009 at 02:27:11
then why didn't you provide a good sample of your file contents? describe again what you want the output to be, properly and clearly

Report •

#7
June 9, 2009 at 03:10:38
This is an example (just an extract because in fact it is much longer) of a upl file named “gst_0001.upl”

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ADMS_HEADER
Comment = 'This is an ADMS Urban Model parameter file '
omissis……
/
&ADMS_PARAMETERS_SUP
SupRoughness = 5.000000000000000e-001
omissis……
/
&ADMS_PARAMETERS_MET
MetDataFileWellFormedPath = 'D:\..\...\....\...\abcde.met'
MetLateralSpreadStdDev = 0.000000000000000e+000
omissis……
/
&ADMS_PARAMETERS_BLD
BldNumBuildings = 0
omissis……
/
&ADMS_PARAMETERS_HIL
1.734000000000000e+006
5.086250000000000e+006
GrdRegularNumPoints =
omissis……
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

That I want to become like the following: pay attention to the line starting with “MetDataFileWellFormedPath”

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ADMS_HEADER
Comment = 'This is an ADMS Urban Model parameter file '
omissis……
/
&ADMS_PARAMETERS_SUP
SupRoughness = 5.000000000000000e-001
omissis……
/
&ADMS_PARAMETERS_MET
MetDataFileWellFormedPath = 'D:\..\...\....\...\met_0001.met'
MetLateralSpreadStdDev = 0.000000000000000e+000
omissis……
/
&ADMS_PARAMETERS_BLD
BldNumBuildings = 0
omissis……
/
&ADMS_PARAMETERS_HIL
1.734000000000000e+006
5.086250000000000e+006
GrdRegularNumPoints =
omissis……
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Number of lines in upl files are varying so do not rely on the position of the line to be substituted; “input string” is always the same (some_generic_name.met) while “output string” is taken from the corresponding met file, in this case “met_0001.met”; I want to generalise the code so that it is working with the two array of files I was talking in my previous posts;
Beyond this example I usually deal with hundreds of such files so I need a generic procedure (code)
I hope this clarifies; I really appreciate your help, sorry for wasting your time


Report •

#8
June 9, 2009 at 03:42:20
there is a lot of differences now, so next time remember to spell out what you want to solve clearly with samples(accurate as possible) and expected output.

here's the amended code:

import glob,os,fileinput
for files in glob.glob("*.upl"):
    name,ext=os.path.splitext(files)
    n,num = name.split("_")
    for line in fileinput.FileInput(files,inplace=1):
        line=line.strip()
        if "MetDataFileWellFormedPath" in line:
            line=list(os.path.split(line.strip()))
            line[-1] = "met_"+num+".met"
            print os.sep.join(line)
        else:
            print line

and here's the executable. NB, i provide you exe out of convenience..anymore requirement changes, i may not want to do it again. Since you can download things from the internet, why don't you download Python and give it a go. It will definitely help you alot when it comes to programming tasks in windows.

Report •

#9
June 9, 2009 at 03:58:47
thanks ghostdog

I will use your code as a starting point for learning seriously python; by the way it works! thanks for that!
I'm not asking you any other modification but I was originally posting for a batch solution because in the machine I need to operate it is forbidden to run external executables...and now I'm a bit curious to know if it possible to accomplish such a task by plain batch code... may be someboby out there can do it?
thanks


Report •

#10
June 9, 2009 at 04:09:30
>> because in the machine I need to operate it is forbidden to run external executables

that is often the excuse isn't it ? To me, if a tool or a programming language helps in your work and increase productivity, both saving you time in development and giving you maintainable code, you should use it. A computer is there to help to lessen the burden , not increase it.


Report •

#11
June 9, 2009 at 05:08:56
yes, I do completely agree with you: it's an excuse (but what a great excuse!)
...and that's why I necessarly have to stick on batch scripting

bye


Report •


Ask Question