i've searched high , low days figure away read directory edit text file based off file names in directory each file name replacing text on different line. have came code below problem changes text on lines first file name.
directoryinfo dinfo1 = new directoryinfo(path); fileinfo[] files1 = dinfo1.getfiles("*.*"); string text = file.readalltext("path/text.txt"); foreach (fileinfo file in files1) { text = text.replace("oldtext1", "path" + file.name); text = text.replace("oldtext2", "path" + file.name); text = text.replace("oldtext3", "path" + file.name); } file.writealltext("path/text.txt", text); note: have 100 files in folder , want add 100 files text in first last order or alphabetical order new files added , want keep order.
if goal replace oldtext1 first filename , oldtext2 second , forth, should pretty simple:
for (var = 0; < files1.length; i++) { text = text.replace("oldtext" + (i+1), "path" + files1[i].name); } we using regular for loop because want have index files1 array. build string replaced concatenating oldtext i+1 , replace current filename in array.
so first time through loop, replace:
oldtext1 => filename1 the second time:
oldtext2 => filename2 and no:
oldtextn => filenamen note: have 100 files in folder , want add 100 files text in first last order or alphabetical order new files added , want keep order.
note order of files returned directoryinfo.getfiles not guaranteed in particular order. should use array.sort sort them before run above loop.
Comments
Post a Comment