If I have a CSV file..

Soldato
Joined
20 Oct 2002
Posts
17,854
Location
London
That is just one column with a list of filenames minus the extension, e.g.

File A
File B
File C

And I need to add an extension of my choice e.g.

File A.mov
File B.mov

(All extensions would be the same)
What's the easiest way of scripting this on a Windows machine? Likely to have hundreds of files in the list. One system exports the list without the extensions and we need to import the CSV into another - which needs the extensions to search properly. Extensions will almost always be .mov or .mp4 -- it'd be the same for the whole list.

Thanks!
 
Soldato
Joined
16 Apr 2007
Posts
23,414
Location
UK
1. Copy column into notepad
2. Type the filename on the first one and copy the ".___" bit
3. Paste then arrow down, paste then arrow down, paste then arrow down - until you finish
4. Copy and paste list into your CSV file again

I don't know if that would work, but that would be my first attempt :p

I'm certain there will be an easier method...

My first thought was "Find and Replace" but you will need to save the filename so that wouldn't work...
 
Associate
Joined
24 Jun 2008
Posts
1,168
Quick bat file save it as movie.bat or something and change the file names and extension in the text.

Code:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('type filename.txt') do (
set line=%%a
echo !line!.mov >> filename1.txt
)
 
Soldato
Joined
17 Aug 2012
Posts
6,595
Location
Tamworth, UK
Never scripted this but I just use Excel to do it with a bit of manual work.

In column B, type the extension you want. In column C, type "=A1&B1" which will put them together (drag down to include all rows) In Column D, copy contents of Column C and paste 'values'. Delete the first 3 columns and save.

Or run the above fancy script. :p
 
Man of Honour
Joined
19 Oct 2002
Posts
29,509
Location
Surrey
Sorry I'm not on Windows but this Python script will do it:

Code:
infile_name = 'myfile.csv'
outfile_name = 'myfile_with_extension.csv'
extension = '.mov'

with open(infile_name, 'r') as infile:
    with open(outfile_name, 'w') as outfile:

        for record_in in infile:
            record_out = record_in.rstrip() + extension + '\n'
            outfile.write(record_out)



This was the input file:
Code:
myfile1
myfile2
myfile3



This was the resulting output file:
Code:
myfile1.mov
myfile2.mov
myfile3.mov



I'm not sure what the relevance of the file being a CSV is though as from your description it doesn't seem to actually contain more than one column?
 
Last edited:
Soldato
Joined
16 Apr 2007
Posts
23,414
Location
UK
Never scripted this but I just use Excel to do it with a bit of manual work.

In column B, type the extension you want. In column C, type "=A1&B1" which will put them together (drag down to include all rows) In Column D, copy contents of Column C and paste 'values'. Delete the first 3 columns and save.

Or run the above fancy script. :p

I like this method :D
 
Man of Honour
Joined
19 Oct 2002
Posts
29,509
Location
Surrey
Never scripted this but I just use Excel to do it with a bit of manual work.

In column B, type the extension you want. In column C, type "=A1&B1" which will put them together (drag down to include all rows) In Column D, copy contents of Column C and paste 'values'. Delete the first 3 columns and save.

Or run the above fancy script. :p
I do this too if the file needs enriching as a one-off.
 
Soldato
Joined
18 Oct 2012
Posts
8,332
Never scripted this but I just use Excel to do it with a bit of manual work.

In column B, type the extension you want. In column C, type "=A1&B1" which will put them together (drag down to include all rows) In Column D, copy contents of Column C and paste 'values'. Delete the first 3 columns and save.

Or run the above fancy script. :p

same, but you can double click the formulae to make them autofill the whole way down the list rather than dragging.

you could also skip the second column and simple have the forumula as:
Code:
=A1&".mov"
 
Permabanned
Joined
9 Aug 2009
Posts
12,236
Location
UK
The right anser is to modify the system which creates the csv so that it includes filenames.
All these other suggestions are hacks, likely requiring human involvement every time.
If it's not automated it shouldn't be done.
 
Soldato
OP
Joined
20 Oct 2002
Posts
17,854
Location
London
The right anser is to modify the system which creates the csv so that it includes filenames.
All these other suggestions are hacks, likely requiring human involvement every time.
If it's not automated it shouldn't be done.
The first system is an Avid Media Composer which is looking at different proxies of the files (in fact the Avid media files are the 'originals' whereas the .mov are proxies). So the Avid doesn't know the .movs exist.

I like the batch file idea thanks @Simon. Can I make one of those where the user drops in the CSV and it adds .mov? That would be awesome. NB: haven't had time to look at it properly yet!
 
Associate
Joined
24 Jun 2008
Posts
1,168
It can be changed to prompt for a file name of the input file and the extension and then does the conversion, saving it to the same filename.

Someone still needs to run it every time though.
 
Man of Honour
Joined
19 Oct 2002
Posts
29,509
Location
Surrey
I thought I'd have a go in python. It checks at startup and then every 5 mins for any files called *.csv in c:\mydirectory. If it finds any files it will add the extension on the end of each row and then rename the file to the original name with _modified.txt on the end of it. So you can just drop the CSV file in that directory and come back a few minutes later to see it converted. It will run forever until stopped.

Code:
import os
import glob
import time

directory_name = 'c:\mydirectory\*.csv'
extension = '.mov'
sleep_seconds = 300

while True:

    # Get a list of all CSV files in a specific directory
    file_list = glob.glob(directory_name)

    # Iterate around the list of files
    for file in file_list:

        # Create a new filename based on the old one
        filename_left = file[:len(file) - 4]
        new_filename = filename_left + '_modified.txt'
        with open(new_filename, 'w') as outfile:

            # Loop through the old file. Add the extension onto any filenames inside it and write to the new file
            with open(file, 'r') as infile:

                for record_in in infile:
                    record_out = record_in.rstrip() + extension + '\n'
                    outfile.write(record_out)

        # Delete the old file so it's not picked up next time the script is run
        os.remove(file)

    time.sleep(sleep_seconds)
 
Soldato
OP
Joined
20 Oct 2002
Posts
17,854
Location
London
So, I turned up and found out they were running Macs. Woops :p

The batch file worked a treat @SimonCHere I'll file that! How easy would it be for it to add a first line as 'File Name'? :)

Sooo, for Macs then what can I use? How do you use those Python scripts @Hades ? :)
 
Soldato
Joined
22 Oct 2005
Posts
2,799
Location
Moving...
If you get notepad++ you can edit in column mode, so you can highlight all your rows, then edit them all at once. Won't work if your strings are different lengths so probably no good for your scenario, but certainly a neat trick I wish I knew about sooner.

Actually If there's a 'remove white space' option in notepad++ (no idea if there is), then this approach would work.
 
Man of Honour
Joined
19 Oct 2002
Posts
29,509
Location
Surrey
Python runs on Windows, MacOS and Linux. In fact I wrote it on Linux. A quick google shows me that Python 2 is installed by default on MacOS. So the above probably works fine without any more installation. But let me know if it doesn't.

Just save the script into a text file and call it something like convert.py Obviously change the directory name in the script to the one you want to monitor. Then to run it simply open a terminal on the Mac and type:

py convert.py

It should then run.
 
Back
Top Bottom