Delete everything left of second backslash - regex

My music collection is organized by artist with separate sub directories for each of the artist's albums
My car doesn't support music in sub directories. I want create a usb stick with music all in root. I would also like to bring over playlists, but they refer to the sub directory structure.
Basically all I need to do is delete everything left of the second backslash.
Before:
The Pretenders\Pretenders II (Disc 1) [2006 Remaster]\The Pretenders - Pretenders II (Disc 1) [2006 Remaster] - 03 - Message Of Love.m4a
After:
The Pretenders - Pretenders II (Disc 1) [2006 Remaster] - 03 - Message Of Love.m4a
I was hoping to do this in notepad++ with a find/replace.

This should be all you need. Find and replace the following with an empty string:
.*\\

Related

Auto serial number and text in Google Sheets: "Number - Text" by dragging

For my data project, column A is Name of "number-text".
Google Sheets can automatically create "text-number" by dragging.
ABC - 01
ABC - 02
ABC - 03
But it doesn’t work when I use: "number - text" .
Example:
01 - ABC
02 - ABC
03 - ABC
How can I create a serial number and text in Google Sheets by dragging or another better solution (arrayformula)?
If you really wish for a dragging solution, you can try:
=TEXT(ROW(A1), "00")&" - ABC"
Though, the proper way would be to use arrayformula bound to another column, like:
=ARRAYFORMULA(IF(B2:B="",,TEXT(COUNTIFS(B2:B, "<>",
ROW(B2:B), "<="&ROW(B2:B)), "00")&" - ABC"))

Python recursive to 5 levels for dirs and files

I am looking for a more elegant solution to an issue I am facing when trying to recurse to multiple levels to list out dirs and files.
os.walk(folder) does sub and sub-sub levels, but I need to go to at least 5 deep.
I have come up with the following to traverse multiple directories, however, is there a better, or more elegant way that I am missing?
rootPath = '/path/to/my/folder/test'
print '###### using os.walk ######'
for root, dirs, files in os.walk(rootPath):
print 'directory - ' + " ".join(dirs)
for d in dirs:
for f in files:
if not f.startswith('.'):
print 'directory - ' + d + ' file - ' + f
print '\n\n\n###### using isdir ######'
for f in os.listdir(rootPath):
print '-' + f
if os.path.isdir(os.path.join(rootPath,f)):
for fo in os.listdir(os.path.join(rootPath,f)):
print '--' + fo
if os.path.isdir(os.path.join(rootPath,f,fo)):
for fol in os.listdir(os.path.join(rootPath,f,fo)):
print '---' + fol
if os.path.isdir(os.path.join(rootPath,f,fo,fol)):
for fold in os.listdir(os.path.join(rootPath,f,fo,fol)):
print '----' + fold
if os.path.isdir(os.path.join(rootPath,f,fo,fol,fold)):
for folde in os.listdir(os.path.join(rootPath,f,fo,fol,fold)):
print '-----' + folde
if os.path.isdir(os.path.join(rootPath,f,fo,fol,fold,folde)):
for folder in os.listdir(os.path.join(rootPath,f,fo,fol,fold,folde)):
print '------' + folder
Output:
###### using os.walk ######
directory - first
directory - second
directory - third
directory - fourth
directory - fourth file - in_third.txt
directory - fifth
directory - fifth file - in_fourth.txt
directory -
###### using isdir ######
-.DS_Store
-first
---.DS_Store
---second
-----.DS_Store
-----third
------.DS_Store
------fourth
-------.DS_Store
-------in_fourth.txt
-------fifth
---------.DS_Store
---------in_fifth.txt
------in_third.txt
It seems as though os.walk isn't going into the 'fifth' folder to see in_fifth.txt, however the isidr() solution does.
Thanks
So part of this was my misunderstanding about how os.walk worked, I believed you were required to iterate through ever directory with for d in dirs. however, it seems it does it already with for f in files
I got round this by letting files do its thing, then replacing root with the rootPath I initially provided, as I only wanted the directory names after this entire path string.
rootPath = '/path/to/my/folder/test'
for root, dirs, files in os.walk(rootPath):
for f in files:
if not f.startswith('.'):
print 'file - ' + os.path.join(os.path.join(root.replace(rootPath,''), f))
Output:
file - /test2/first/foobar/files1.txt
file - /test2/first/foo.txt
file - /extra/test/bar.bin

What's the best way to store a list of data and auto assign to a structure

Edit: I didn't explain myself properly. Try again.
I am writing a save game editor. The editor loads in the save game, checks the version. The version information was held in various "Data" statements, once the version was checked the correct offsets were loaded. The offsets were then assigned to variables.
Below is an example of offsets for two versions of the game (Sensible World of Soccer):
Swos_Beta_09 = { # _update: 95/96
'num_players_pos': 56717, # - Number of players in the team
'team_name_pos': 55429, # - Team Name
'mgr_forename_pos': 54987, # - Managers forename
'mgr_surname_pos': 54996, # - Managers Surname
'mgr_fullname_pos': 55460, # - Managers Full Name (Yes it is stored twice!!)
'plr1_Position_pos': 55500, # - Position of the first player in the file. This is then 38 bytes of data
'money_pos': 54742, # - Money
'tact_pos': 88374, # - First Tactic
'first_team': 90635, # - First team in the file
'CJOffset': 90614, # - File position of the version number
'CJ': "CJ281112", # - This is the version we are expecting
'version_pos': "Sensible World of Soccer v0.09 - 28/11/1994 11.00am" # - And this is the version
Swos_Release_v10 = # _update: 95/96
'num_players_pos': 56719, # - Number of players in the team
'team_name_pos': 55431, # - Team Name
'mgr_forename_pos': 54989, # - Managers forename
'mgr_surname_pos': 54998, # - Managers Surname
'mgr_fullname_pos': 55462, # - Managers Full Name (Yes it is stored twice!!)
In python I used dictionaries, and then once checked the version (CJ031223 for example) it copied the correct offsets into another dictionary (or list) allowing me to access the data.
note: I will be using classes for the teams, and the players, I am not concerned with that here. I am looking for the best way to do this in c++ please.
Have a look at unordered_map for a cpp "dictionary" equivalent.
For a json parser, you could do worse than boost's json parser.

Rename file names with accents in shell

I have had an error while migrating my files from another server.
Now, all wordpress files are like this:
Tendência-Moda-Feminina-2014
I need to have this name: Tendência-Moda-Feminina-2014
I would like to know if someone has a regex to replace those "codes" to the normal portuguese accents.
There are others codes that I need to exchange to my accented letters.
My foldes are like this:
2014
- 01
- 02
...
- 12
2015
- 01
- 02
...
- 12
Its the uploads foldes of wordpress structure.

How to write a program to rename mp4 files to match the name of srt files?

So I have downloaded the mp4 and srt files for "Introduction to computer networks" course from Coursera. But there is slight discrepancy between the names of mp4 and srt files.
The file name samples are following:
1 - 1 - 1-1 Goals and Motivation (1253).mp4
1 - 1 - 1-1 Goals and Motivation (12_53).srt
1 - 2 - 1-2 Uses of Networks (1316).mp4
1 - 2 - 1-2 Uses of Networks (13_16).srt
1 - 3 - 1-3 Network Components (1330).mp4
1 - 3 - 1-3 Network Components (13_30).srt
1 - 4 - 1-4 Sockets (1407).mp4
1 - 4 - 1-4 Sockets (14_07).srt
1 - 5 - 1-5 Traceroute (0736).mp4
1 - 5 - 1-5 Traceroute (07_36).srt
1 - 6 - 1-6 Protocol Layers (2225).mp4
1 - 6 - 1-6 Protocol Layers (22_25).srt
1 - 7 - 1-7 Reference Models (1409).mp4
1 - 7 - 1-7 Reference Models (14_09).srt
1 - 8 - 1-8 Internet History (1239).mp4
1 - 8 - 1-8 Internet History (12_39).srt
1 - 9 - 1-9 Lecture Outline (0407).mp4
1 - 9 - 1-9 Lecture Outline (04_07).srt
2 - 1 - 2-1 Physical Layer Overview (09_27).mp4
2 - 1 - 2-1 Physical Layer Overview (09_27).srt
2 - 2 - 2-2 Media (856).mp4
2 - 2 - 2-2 Media (8_56).srt
2 - 3 - 2-3 Signals (1758).mp4
2 - 3 - 2-3 Signals (17_58).srt
2 - 4 - 2-4 Modulation (1100).mp4
2 - 4 - 2-4 Modulation (11_00).srt
2 - 5 - 2-5 Limits (1243).mp4
2 - 5 - 2-5 Limits (12_43).srt
2 - 6 - 2-6 Link Layer Overview (0414).mp4
2 - 6 - 2-6 Link Layer Overview (04_14).srt
2 - 7 - 2-7 Framing (1126).mp4
2 - 7 - 2-7 Framing (11_26).srt
2 - 8 - 2-8 Error Overview (1745).mp4
2 - 8 - 2-8 Error Overview (17_45).srt
2 - 9 - 2-9 Error Detection (2317).mp4
2 - 9 - 2-9 Error Detection (23_17).srt
2 - 10 - 2-10 Error Correction (1928).mp4
2 - 10 - 2-10 Error Correction (19_28).srt
I want to rename the mp4 files to match srt files so that vlc can automatically load the subtitles when I play the videos. What someone discuss be algorithms to do this? You can also provide solution code in any language as I am familiar with many programming languages. But python and c++ are preferable.
Edit:
Thanks to everyone who replied. I know it is easier to rename the srt files than the other way around. But I think it will be more interesting to rename the mp4 files. Any suggestions?
Here's a quick solution in python.
The job is simple if you make the following assumptions:
all files are in the same folder
you have the same number of srt and mp4 files in the directory
all srt are ordered alphabetically, all mp4 are ordered alphabetically
Note I do not assume anything about the actual names (e.g. that you only need to remove underscores).
So you don't need any special logic for matching the files, just go one-by-one.
import os, sys, re
from glob import glob
def mv(src, dest):
print 'mv "%s" "%s"' % (src, dest)
#os.rename(src, dest) # uncomment this to actually rename the files
dir = sys.argv[1]
vid_files = sorted(glob(os.path.join(dir, '*.mp4')))
sub_files = sorted(glob(os.path.join(dir, '*.srt')))
assert len(sub_files) == len(vid_files), "lists of different lengths"
for vidf, subf in zip(vid_files, sub_files):
new_vidf = re.sub(r'\.srt$', '.mp4', subf)
if vidf == new_vidf:
print '%s OK' % ( vidf, )
continue
mv(vidf, new_vidf)
Again, this is just a quick script. Suggested improvements:
support different file extensions
use a better cli, e.g. argparse
support taking multiple directories
support test-mode (don't actually rename the files)
better error reporting (instead of using assert)
more advanced: support undoing
If all those files really follow this scheme the Python implementation is almost trivial:
import glob, os
for subfile in glob.glob("*.srt")
os.rename(subfile, subfile.replace("_",""))
If your mp4 also contain underscores you want to add an additional loop for them.
for f in *.srt; do mv $f ${f%_}; done
This is just an implementation of what Zeta said :)
import os;
from path import path;
for filename in os.listdir('.'):
extension = os.path.splitext(path(filename).abspath())[1][1:]
if extension == 'srt':
newName = filename.replace('_','');
print 'Changing\t' + filename + ' to\t' + newName;
os.rename(filename,newName);
print 'Done!'