Computing.Net > Forums > Programming > batch replace array of files

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.

batch replace array of files

Reply to Message Icon

Name: maxbre
Date: June 8, 2009 at 08:38:15 Pacific
OS: Windows XP
CPU/Ram: 2 gb
Subcategory: Batch
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 8, 2009 at 16:49:28 Pacific
Reply:

so where are specifying the string to substitute? in other words, how do you know 1.met is suppose to use "abcde"?


0

Response Number 2
Name: maxbre
Date: June 8, 2009 at 23:38:41 Pacific
Reply:

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


0

Response Number 3
Name: ghostdog
Date: June 9, 2009 at 00:14:48 Pacific
Reply:

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.


0

Response Number 4
Name: maxbre
Date: June 9, 2009 at 00:44:46 Pacific
Reply:

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


0

Response Number 5
Name: maxbre
Date: June 9, 2009 at 01:56:49 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: June 9, 2009 at 02:27:11 Pacific
Reply:

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


0

Response Number 7
Name: maxbre
Date: June 9, 2009 at 03:10:38 Pacific
Reply:

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


0

Response Number 8
Name: ghostdog
Date: June 9, 2009 at 03:42:20 Pacific
Reply:

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.

0

Response Number 9
Name: maxbre
Date: June 9, 2009 at 03:58:47 Pacific
Reply:

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


0

Response Number 10
Name: ghostdog
Date: June 9, 2009 at 04:09:30 Pacific
Reply:

>> 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.


0

Response Number 11
Name: maxbre
Date: June 9, 2009 at 05:08:56 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Magic Messaging! String Compair Problems



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: batch replace array of files

writing to file array of numbers www.computing.net/answers/programming/writing-to-file-array-of-numbers/6942.html

C arrays of strings from a file www.computing.net/answers/programming/c-arrays-of-strings-from-a-file/14025.html

Batch, get folder of a found file www.computing.net/answers/programming/batch-get-folder-of-a-found-file/16126.html