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 !

update 1

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}

update 2

I solved this by correcting the last line like this :

Rename-Item $file -NewName ( "[$offset1-$offset2] " + $file.name )
  • For future questions, include the error output to help others troubleshoot with you. – root Oct 17 '16 at 13:35
  • @root The error codes were in french, hence why I did not include them. – GabrielB Nov 9 '16 at 17:41

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.

  • I solved it on my own, but indeed the "$file" component was missing in the last line. I tried to read the included help each time I was stuck but it was barely helpful most of the time, it seems to assume that the user already has a certain knowledge level and rarely provide ways to fix common errors. – GabrielB Nov 9 '16 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.