sábado, 22 de enero de 2022

Joining PDFs into a single file


One of the easiest ways to join a number of PDFs into a single file is to use the pdftk command.

A pdftk command to join a number of PDFs into a single file might look like this:


$ pdftk recipe-1.pdf recipe-2.pdf recipe-3.pdf recipe-4.pdf recipe-5.pdf cat output recipes.pdf

You can use a wildcard if your files follow a regular pattern like this:

$ pdftk recipe-*.pdf cat output recipes.pdf 
 
https://www.networkworld.com/article/3609508/creating-and-merging-pdfs-on-linux.html 
 workaround
sudo ln -s /snap/pdftk/current/usr/bin/pdftk /usr/bin/pdftk 

miércoles, 5 de enero de 2022

Visual Studio Crashes and How to Recover a Corrupted .cs File

https://www.samnoble.co.uk/2014/11/30/visual-studio-crashes-and-a-corrupted-cs-file/

Visual Studio Crashes and How to Recover a Corrupted .cs File

In spite of using source control, sometimes it’s still possible to lose work due to the quirks of computers. At least you can mitigate this and only suffer losing a few hours of work, but still, when those few hours were a stroke of genius, the likes of which cannot be reproduced, you can’t help but shed a tear when all is lost. But fear not, here are 4 techniques that you can try to recover your code with before crying.

Lately I’ve used VirtualBox to run Windows 8.1 on my Mac Book Air under Mavericks. My Parallels license expired and I wanted to check out the alternatives. So far it’s been a pleasure to use but recently I’ve experienced it falling over, taking OSX down with it. I’ve probably seen the Grey Screen of Death twice over the last 6 years of using a Mac but have seen it twice in the last week.

It’s generally not a problem, just a minor inconvenience, until last night. Just as I’m about to wrap up for the day after making some great progress, everything comes crashing down. Once everything is back up and running, I couldn’t open one of my .cs files. Visual Studio would launch Notepad.exe which would display a seemingly empty file, although I could still make text selections:

1. Empty Notepad

However Explorer reported the file as being 30Kb…

2. Filesize

This gave me hope, the data is still there! It’s probably just a case of a dodgy character somewhere in the file as a result of the crash. What does Notepad++ say?

4. Notepad++

Ok this is looking really bad. Back to Visual Studio, right-click the file in Solution Explorer -> Open With… -> Binary Editor.

3. Binary Content

Oh dear. This doesn’t look good. Apparently it’s a common problem and most likely due the crash occurring whilst that file was being written to on the disk, although in the 10 years that I’ve been working with Visual Studio this is the first time I’ve experienced it. There’s not much one can do to guard against this, other than diagnosing and fixing the cause for Virtualbox crashing, but that’s a whole new problem.

No point crying over spilt milk, let’s just get this fixed! First thing’s first, do not attempt to build your project again as we may need to use the output of the last build to recover your files!

Solution 1 – Decompile

If you have a recent build of your project, you can recover the code from the generated binaries using something like ILSpy (open source) or Dotpeek (free).

https://www.jetbrains.com/decompiler/

If you search around the web for ‘Visual Studio corrupted file’, this is most likely the first solution you’ll find and is often touted as the only solution. Whilst it’s a perfectly practical solution and perhaps your only option, for me it’s a last resort for the following reasons:

  1. All comments and regions are removed.
  2. The code structure will have been lost.
  3. Depending on your build configuration, some optimisation may have taken place.
  4. Doesn’t really help if the code has been obfuscated.

I’m sure you’ll agree it’s better than nothing. If you don’t have any assemblies or you want to try to restore the file to its original awesomeness, continue on.

Solution 2 – AutoRecovery

If you have AutoRecovery enabled, you might be lucky enough to find a backup of the file. Chances are that Visual Studio would’ve prompted you to restore the corrupted file when launching it after the crash. Anyway it’s still worth checking the backup folder for your project, in:

%USERPROFILE%DocumentsVisual Studio [Version]Backup Files[Solution Name]

No dice for me, this directory was empty for me, on to the next solution.

Solution 3 – CHKDSK

Depending on the particulars of the corruption, trusty old chkdsk might help. It might be wise to attempt Solution 4 first to minimise the chances of any data being overwritten.

From an elevated command prompt execute:

chkdsk /f

You’ll receive a message that the drive is locked and the operation will need to be performed on the next restart. Just say ok and restart your machine. Depending on your setup, it could take a while for this to run so be patient.

Solution 4

This was the solution that worked for me and allowed me to recover the full content of my file! I used the free recovery software Recuva. Either use this or whatever you prefer, although instructions here are for Recuva.

Whilst Recuva is primarily for recovering deleted files and this isn’t technically a deleted file, we’re facing a similar challenge. The file is corrupt and pointing to the wrong clusters on the HDD, thus when we load our file it’s loading garbage instead of the correct content.

My first process was:

  1. Launched Recuva and used the Wizard to scan the folder containing my source files. After a few minutes it showed me an empty results window. Not looking good…
  2. Click on the ‘Options…’ button in the top right hand corner of the window.
  3. In the ‘Actions’ tab select the following: 5. Recuva Options
  4. Hit the ‘Ok’ button.

At this point Recuva started updating the results view, but it still remained empty! Sad face. In a last-ditch effort I quit Recuva, navigated to the source directory in Explorer, right-click, ‘Scan for deleted files’.

Some more dialogs popped up, scanning the drive again until boom, a results view that displayed the contents of the folder, including all the sub-directories. This looks good!

I scrolled down to the file in question and read across the row: ‘Not deleted, Not overwritten’ etc. On the right of the screen there’s a tabbed preview pane. There was no preview available, the ‘Info’ tab looked normal but the ‘Header’ tab was where the magic was!

6. Recovered File

Right click the file in the results pane, ‘Recover Highlighted…’, save to desktop and bingo, fully recovered file. Thanks Recuva!

If you’re ever unlucky to encounter this issue, probably just as you’re about to pack up to go home or preparing for a big tech demo at an investor pitch, one of these solutions will get you unstuck.

If you have any other tips, share them in the comments and I’ll add them to the post.

Edit: Reader ‘bizh’ provided an additional step to Solution 4 if it doesn’t work out fully. I haven’t tested this so proceed at your own risk:

If Recuva is empty, you can delete the corrupted file and then run Recuva. In that case Recuva will definitely find the file and recover the clean version of it

Bizh