Opening these files in 7-zip shows that it’s a compressed zip files containing all the blog information under as a file structure. Worth mentioning aside the obvious is the ServerSupportingFileDir which contained the path on the server where these files were saved.
The subfolder SupportingFiles contains a folder per attached file where the file content and file name are stored.
Based on this, I quickly wrote two scripts:
- The first decompresses all the wpost files using 7-Zip
- The second parses the decompressed files, extracts all pictures and saves them in a file structure identical to that on the website.
The last operation is to upload these new files per FTP, so that we get a much better image resolution (Windows Live Writer saving all the picture at least with 1024x768px).
As you can see, there were a few issues along the way, such as converting the files from Unicode encoding to ASCII and some file extensions issues, but they were all resolved within the batch file.
First script - extractJPGs.bat
rem sets the proper working folder
cd /d %0\..
cd %0\..
rem Define the path to the 7-zip command line executable
set z="c:\program files\7-zip\7z.exe"
rem loops through all the *.wpost files and decompresses them, without extension, into the temp file
for /r . %%f in (*.wpost) do %z% x "%%f" -o"temp\%%~nf\"
pause
Second script - renameAndMoveJPGs.bat
@echo off
rem sets the proper working folder
cd /d %0\..
cd %0\..\temp
rem iterates through all the folders and calls handleFolder with the directory's name
for /d %%d in (*) do call :handleFolder "%%d"
goto :eof
:handleFolder
echo %1
cd %1
rem The standard file is not ASCII encoded. We need to make an ASCII compliant copy of it via type
type ServerSupportingFileDir > ServerSupportingFileDir_v2
rem We store the folder name of the server withint the uploadFolder variable
set /p uploadFolder=<ServerSupportingFileDir_v2
rem For each found file matching "SupportingFileName", we call the handleFiles with the file name and upload folder as argument
for /r . %%f in (SupportingFileName) do call :handleFiles "%%f" "%uploadFolder%"
echo %uploadFolder%
cd ..
rem pause
goto :eof
:handleFiles
if exist %1 call :handleFiles2 %1 %2
goto :eof
:handleFiles2
rem echo on
rem Stepping into the current folder
cd %1\..
rem The standard file is not ASCII encoded. We need to make an ASCII compliant copy of it via type
type %1 > %1_v2
rem We store the file name within the variable
set /p fileName=<%1_v2
rem We create the output folder
if not exist ..\..\..\..\forWeb\%2 mkdir ..\..\..\..\forWeb\%2
rem Quick trick - during the copy, we don't fully rely on the file extension found in the file.
rem I had quite a few .JPG files according to SupportingFileName, but on the server they were known as .jpg.
rem We therefore take all the filename but the last 3 characters and force our own file extension.
copy SupportingFileContents ..\..\..\..\forWeb\%2\%fileName:~0,-3%jpg
cd ..\..
goto :eof
:eof