Wednesday, February 18, 2015

windows - Batch rename files recovered by Photorec with their offset values


I recovered about 300 MKV files from a 4TB HDD, using Photorec. Those files are named according to their first sector, for instance "f123456789.mkv". Now, I want to rename them all by adding their corresponding begining and ending offset values, like this : "[63209875968-70209875967] f123456789.mkv" (for a file size of 7000000000 bytes in this example). I tried this batch script :


for %%F in (f*.mkv) do (
SET name=%%~nF
SET sectors=%name:~1%
SET /a offset1=%sectors%*512
SET /a offset2=%offset1%+%%~zF-1
RENAME "%%F" "[%offset1%-%offset1%] %%F"
)

But it doesn't work, although the individual commands do work within CMD ; the variables are not expanded correctly. I tried with "setlocal enabledelayedexpansion", and !variable! instead of %variable%, to no avail. Besides, I read that the calculations in CMD were limited to a 32-bit accuracy, so if I get it right, using batch command set /a I could only calculate offsets up to 4294967296 anyway (even for the first file that's not enough). Which means I should use another scripting language, but which one would be appropriate, and how could I quickly find the corresponding command to do what I want to do ? I spent most of the afternoon trying to get the basics of PowerShell and adapt the above script, but so far I was only able to print the filenames and remove the first character... (It's quite frustrating, considering that with the same amount of time I could have done this task manually and it may be finished by now ! It's more prone to error though, another reason why I'd like to automate it...) I just need to :



  • set a variable with the first sector number, obtained from the file name,

  • set a variable for the first offset value, which is the sector number multiplied by 512,

  • set a variable for the last offset value, which is the first offset plus the file size minus 1,

  • rename the files using the aforementioned pattern.


Any idea ? Thanks !




So, after another night of clumsy fiddling with Powershell, I think I got a few things right. The first steps seem to work fine, but I get undecipherable error after undecipherable error when it comes to actually renaming the files. What is wrong with the Rename-Item command below ? And are there ways to do this more simply / elegantly ?


foreach ($file in gci *.mkv) {
$sectors = [string]($file.BaseName).substring(1)
$offset1 = [int64]$sectors * 512
$offset2 = [int64]$offset1 + [int64]($file.Length) - 1
Rename-Item -NewName {"[$offset1-$offset2]" $file}
}



I solved this by correcting the last line like this :


Rename-Item $file -NewName ( "[$offset1-$offset2] " + $file.name )

Answer



The ForEach loop you posted looks correct in that it achieves your goals, all but the Rename-Item line. It should be:


Rename-Item $file -NewName $offset1"-"$offset2".mkv"

To find more info on a cmdlet and its syntax, use Get-Help Remove-Item -full. You'll find extended details along with helpful examples.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...