I have mixed music projects, where the software generates a txt file list of the songs in each project. The problem is that the list is generated with only the first 36 characters of the name of each song.
I need to correct the names inside the txt file, so that they are the same as the names of the original files and my script can copy the files using the list.
I created a script, in two blocks, where in the first block I read the original files and generate a variable with the same name as the name inside the txt file.
First block (what I managed to do):
Read the original files, in the Original Musics folder, and create the variable with the same name as the one inside the List.txt file.
Goal of the first block (what I couldn't do):
When the name in the variable in this block is the same as the name inside List.txt, the script will change the name inside List.txt to the full name of the original file to which the variable is related. Thus, the List.txt file can be read in the second block with the correct names inside it.
Note:
It may happen that there are two files with the same name in the Original Musics folder, but with different versions, in this situation, the two full names of these files must be in the List.txt file to be copied to the destination folder. eg.
Name in List.txt file:
Got To Be Real (CM_Gex Bootleg Exten
Original name (full name in the Original Musics folder):
Cheryl Lynn - Got To Be Real (CM_Gex Bootleg Extended SHORT Re Mix).mp3
Cheryl Lynn - Got To Be Real (CM_Gex Bootleg Extended LONG Re Mix).mp3
Variable $ShortOriginalSongName:
Got To Be Real (CM_Gex Bootleg Exten
Got To Be Real (CM_Gex Bootleg Exten
As it is the same file, the difference is that one is the long version and the other is the short version, both full names must be in the List.txt file, that is, in addition to changing the names in the list.txt, when the files are the same, the script must include the original full names in the List.txt
Script:
$files = "C:\Users\$env:username\Desktop\List.txt"
$location = "G:\Original Musics\"
$destination = "C:\Users\$env:username\Desktop\Copy Selected Musics\"
# First block:
Get-ChildItem -Recurse $location* -Include *.mp3 | % {
$ShortOriginalSongName = $_.Basename.Split("-")[1].Trim();
If ($ShortOriginalSongName.Length -gt 36) {$ShortOriginalSongName = $ShortOriginalSongName.Substring(0, 36)};
};
# Second Block:
Get-Content $files | % {
$result = gci -recurse "$location*$_" -Include *.mp3
If($result) {
Add-Content $destination"AddList.txt" -Value $result.Fullname
$musicName = $result.Name
$tot+=1
Write-Host -ForegroundColor Green "$musicName found on $location!"
Write-Host "$musicName copied to $destination..."
Copy-Item $result.FullName -Destination $destination\$($_.Name)
}
}
Second Block:
Read the List.txt file that was generated with the correct names of the songs in the first block, and copy the original files to the Copy Selected Musics folder.
$_.BaseName.Split('-')[1]
is unexplained,original name (full name in the Original Musics folder):
[
or]
) , dollar sign ($
)? If so, some file ops may require the-LiteralPath
parameter...$ShortOriginalSongName = -join (($_.Basename.Split("-")|select -Skip 1).Trim().toCharArray() | select -first 36)