I
I
Ivan Semenov2016-09-09 12:25:21
Python
Ivan Semenov, 2016-09-09 12:25:21

How to write the name of the same file into a text file?

Good afternoon.
Recently, they transferred me the work of loading registers from banks into 1C. And there was a problem with one bank. a lot of registries come in txt format, and moreover, in one day there may be several of them the same, for example, with this name (40104161) 6 files, since the mail program, when saving all files, can add a counter to the repeated file names and as a result I get this such a set of files (40104161; 40104161(1); 40104161(2); 40104161(3), etc.). in the file itself, the first line contains the file name, but for those to which the counter is added to the bracket, it does not match and 1C does not accept it, so you have to manually open each file and rewrite the first line.
The question is how to write a script that would read the filename and overwrite the first line in the file itself?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2016-09-11
@kokman_semenov

Hold on. Create a file with this code, put it in your files folder and enjoy (well, or tweak the path with the code)

for f in listdir('.'):
    if not f.endswith('.py'):
        replace = f
        source = f.split('(')[0]
            
        file = open(f, 'r')
        text = file.read()
        file.close()

        file = open(f, 'w')
        file.write(text.replace(source, replace))
        file.close

R
res2001, 2016-09-09
@res2001

Batnik can be easily implemented.
Here is a code sketch:

for %%a in (c:\temp\*) do (
 echo.%%~nxa - Это имя файла
 echo.Следующая команда выводит текстовый файл на экран, пропуская первую строку.
 for /f "usebackq tokens=* skip=1 delims=" %%b in ("%%~a") do echo.%%b
)

The idea must be clear. Then you will bring yourself to mind.

M
Mercury13, 2016-09-09
@Mercury13

The only way to do something like this with a text file is to write to a temporary file, then replace the old one with this temporary one. So it will be something like (I write in pseudo-C ++)

открыть исходный и временный файлы
if (std::getline(исходный, s)) {
  подкорректировать s
  временный << s << std::endl;
  while (std::getline(исходный, s) {
     временный << s << std::endl;
  }
  закрыть оба файла
  уничтожить исходный
  переименовать временный
}

The main problem is Unicode filenames, which I hope you don't have.
And, of course, do not forget about the byte order mark, if the files have it.

A
abcd0x00, 2016-09-09
@abcd0x00

You would replace the brackets with underlining 40104161(1) -> 40104161_1.
And then he rewrote the first line of the file, copying his name into it.
To do this, you need two scripts (in python, shell or whatever): the first script replaces this bracketed suffix of the file transferred to it with an underlined suffix; the second script simply takes the file and writes its name on its first line.
And above them is the third script, which runs the first script for the file, and then the second one.
And when you have this third script, you run it in a loop for a list of files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question