0

The problem: I want to iterate over folder in search of certain file type, then execute it with a program and the name.ext as argument, and then run python script that changes the output name of the first program.

I know there is probably a better way to do the above, but the way I thought of was this:

[BAT]

for /R "C:\..\folder" %%a IN (*.extension) do ( SET name=%%a "C:\...\first_program.exe" "%%a" "C:\...\script.py" "%name%" )

[PY]

import io
import sys
def rename(i):
    name = i
    with open('my_file.txt', 'r') as file:
        data = file.readlines()
    data[40] ='"C:\\\\Users\\\\UserName\\\\Desktop\\\\folder\\\\folder\\\\' + name  + '"\n'
    with open('my_file.txt', 'w') as file:
        file.writelines( data )

if __name__ == "__main__":
    rename(sys.argv[1])

Expected result: I wish the python file changed the name, but after putting it once into the console it seems to stay with the script. The BAT does not change it and it bothers me.

PS. If there is a better way, I'll be glad to get to know it.

  • can you explain more what you're expecting and what happens.?because it's not clear – Jean-François Fabre Sep 27 '18 at 09:16
  • First you could use [`os.walk`](https://www.tutorialspoint.com/python/os_walk.htm) to iterate over the folder inside of python then make the call to your exe from python as well using [`subprocess.call`](https://stackoverflow.com/a/89243/803359) – mikuszefski Sep 27 '18 at 09:17
  • @Jean-FrançoisFabre 1st program executes, does everything just fine and second(python script) does not change the name of the file processed but stays the same as if I just ran it before to check if it does work (it's holding the name I have put through the script and executes with said name everytime). – Łukasz Formela Sep 27 '18 at 09:20
  • alternatively you could pipe the output through `awk` and change row 40 and not use python at all – mikuszefski Sep 27 '18 at 09:21
  • ... so do you want `with open(name, 'r' ) as myFile`? (...don't use `file` as name for your "file handle")... as you have the name hard coded right now. – mikuszefski Sep 27 '18 at 09:24
  • ...a workaround for piping in windows is shown [here](https://stackoverflow.com/a/14574661/803359) – mikuszefski Sep 27 '18 at 09:26
  • @mikuszefski This works for a single line. Can I use it for line [40] of the "msg.txt" from the example? – Łukasz Formela Sep 27 '18 at 09:31
  • I don't see why not. in awk you can also play with RS and NR – mikuszefski Sep 27 '18 at 10:52
  • you may think bout using cygwin to have something like bash – mikuszefski Sep 27 '18 at 10:54
  • Well, AWK is straight new for me, I don't know syntax nor the functions. can you provide anything more? I am in need of the working code ASAP for my work, I can get to getting AWK later on. – Łukasz Formela Sep 27 '18 at 10:54
  • Awk is well ducumeted but e.g. to prepend "Hello" to line 17 of a file you can write `awk '{if(NR==17) print "Hello" $0; else print $0 }' infile.txt > outfile.txt`. This is on linux and the changed file is written to outfile.txt – mikuszefski Sep 27 '18 at 11:04
  • So I gess it will be a simple loop with, e.g. a counting variable. You loop over all files. call your `.exe` with the file. I assume the output has always the same name. You use `awk` to change line 40. You can pass additional variables to `awk` via `-v` you pipe the output in a file with unique name by using the increasing counter or modifying the input file name. – mikuszefski Sep 27 '18 at 11:09
  • passing text variable would be `awk -v myText="SeeYa" '{if(NR==17) print myText $0; else print $0 }' infile.txt > outfile.txt ` – mikuszefski Sep 27 '18 at 11:16

1 Answers1

0

This is the linux bash version, I am sure you can change the loop etc to make it work as batch file instead of your *.exe I use cat as a generic input output example

#! /bin/sh
for f in *.txt
do
    suffix=".txt"
    name=${f%$suffix}
    cat $f > tmp.dat
    awk -v myName=$f  '{if(NR==5)  print $0 myName; else print $0 }' tmp.dat > $name.dat
done

This produces "unique" output *.dat files named after the input *.txt files. The files are treated by cat (virtually your *.exe) and the output is put into a temorary file. Eventually, this is handled by awk changing line 5 here. with the output placed in the unique file, as mentioned above.

mikuszefski
  • 3,943
  • 1
  • 25
  • 38