"A humorous term in software development indicating that a proposed change or feature may seem simple but actually requires significant effort to implement. It often highlights the irony that what appears easy can be quite complex in reality."
or
"A piece of code, not yet written, whose anticipated length is significantly greater than its complexity. Used to refer to a program that could obviously be written, but is not worth the trouble. Also used ironically to imply that a difficult problem can be easily solved because a program can be written to do it; the irony is that it is very clear that writing such a program will be a great deal of work."
However these small programs have taken considerable time to test and improve. So as to present simple PoCs (Proof of Concepts).
[Beware when using redirection symbols the use of escapes or space after a # before may be critical]
Windows TAR is powerful but has a shortcoming. It can extract a file or whole folder from a zip file such as a folder.zip or comic.cbz or word.docX.
However although it can write a whole folder to a new file, currently it cannot -add or -update or -replace. All of which are roughly the same. So what we need is a Windows native zip injector call it zip-in or zip-it that does not matter. The aim is to add a file either add or replace.
Zip-it.cmd will write a high(ish) speed executable for providing that function. Run it once and then it will write the zip-it.exe.
echo using System;using System.IO.Compression;class Z{static void Main(string^[^] a){if(a.Length^<3){Console.WriteLine("Usage: zip-it <zipfile.ext> <entry> <file to add>");return;}using(var z=ZipFile.Open(a^[0^],ZipArchiveMode.Update)){var e=z.GetEntry(a^[1^]);if(e!=null)e.Delete();z.CreateEntryFromFile(a^[2^],a^[1^]);}}} > "%TMP%\zip-it.cs" && "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /nologo /platform:x86 /r:System.IO.Compression.dll /r:System.IO.Compression.FileSystem.dll /out:"%CD%\zip-it.exe" "%TMP%\zip-it.cs" && del "%TMP%\zip-it.cs"Usage: zip-it <zipfile.ext> <entry> <file to add>
For example zip-it word.docx word/document.xml x:\folder\document.xml
Did I mention it can write a new zip file?
At a console try echo hello world >hello.txt & zip-it new.zip hello.txt hello.txt & zip-it new.zip "a folder/hello.txt" hello.txt
>echo hello world >hello.txt & zip-it new.zip hello.txt hello.txt & zip-it new.zip "a folder/hello.txt" hello.txt
Check the new.zip has 2 files and one is in "a folder"
>tar -tvf new.zip
-rw-rw-r-- 0 0 0 15 Dec 23 13:01 hello.txt
-rw-rw-r-- 0 0 0 15 Dec 23 13:01 a folder/hello.txt
GZ.cmd
Files with a .gz extension are less common in Windows but can be easily Compressed and Decopressed using gz <c|d> <in> <out>
Thus gz.exe also can be written as a compiled cmd using class G (GZipStream) rather than class Z (ZipFile.Open)
echo using System;using System.IO;using System.IO.Compression;class G{static void Main(string^[^] a){if(a.Length^<3){Console.WriteLine("Usage: gz <c|d> <in> <out>");return;}if(a^[0^]=="c")using(var i=File.OpenRead(a^[1^]))using(var o=File.Create(a^[2^]))using(var g=new GZipStream(o,CompressionLevel.Optimal))i.CopyTo(g);else if(a^[0^]=="d")using(var i=File.OpenRead(a^[1^]))using(var g=new GZipStream(i,CompressionMode.Decompress))using(var o=File.Create(a[2]))g.CopyTo(o);}} > "%TMP%\gz.cs" && "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /nologo /platform:x86 /out:"%CD%\gz.exe" "%TMP%\gz.cs" && del "%TMP%\gz.cs"
To look inside or first test a DocX (or other [g]Zip gz or many installers )for a listing of content and thus check both above problem tasks
Is best handled with 7-Zip commands but often we want a simple one task command line helper or a more GUI approach so I wrote a simple "SendTo" command to check files are extractable or save a list of contentsm which too could have been a single line command. But as a GUI it is much larger than a simple one line one can be found as "ListArc.CMD" at https://github.com/GitHubRulesOK/MyNotes/blob/master/C%23/listarc.cmd
TXT
set "out=%tmp%\h.txt" & (echo. &echo Page 1 of 1&echo. &echo Hello, World!) > "%out%"&write /pt %out% "Microsoft Print to PDF" "Microsoft Print to PDF" %public%\documents\result.pdf&timeout 3 >nul&&del %out%&&start msedge --app=%public%\documents\result.pdfRTF
echo {\rtf1\ansi{\colortbl ;\red0\green0\blue255;}{\fonttbl{\f0\fnil Segoe Script;}{\f1\fnil Comic Sans MS;}}\qc\par\par\par\par\b\i\ul\cf1\f0\fs48 Hello, World!\b0\i0\ulnone\par} > %tmp%\h.rtf&write /pt %tmp%\h.rtf "Microsoft Print to PDF" "Microsoft Print to PDF" %public%\documents\result.pdf&timeout 3 >nul&del %tmp%\h.rtf&start msedge --app=%public%\documents\result.pdfNOTE current msedge version 141 is again having problems related to user folder so you may need a temporary user folder as shown in first example After all runs are done then delete that temp folder using RD.
cmd /r start msedge --headless=new --log-level=3 --user-data-dir="%cd%\temp" --no-pdf-header-footer --print-to-pdf="C:\Users\Public\Documents\out1.pdf" --generate-pdf-document-outline "https://example.com">nul 2>&1 &&(for /l %i in (1,1,9) do (@if exist "C:\Users\Public\Documents\out1.pdf" start msedge --app="C:\Users\Public\Documents\out1.pdf#view=fitV" &exit)&timeout /t 1 >nul)
cmd /r start msedge --headless=new --no-pdf-header-footer --print-to-pdf="C:\Users\Public\Documents\out2.pdf" --generate-pdf-document-outline "data:text/html,<title>Hello World PDF</title><style>@page{size:A4;} h1{font-size:12pt;font-family:Helvetica;margin:0;} h2 { font-family:Helvetica;font-size:36pt;font-weight:bold;text-align:center;}</style><body><h1>Page 1</h1><div><h2>Hello World!</h2><p style="font-family:webdings;font-size:200px;text-align:center;line-height:0;margin:130;">MTJ</p>Lora is sum thing else and then some more.</div><div style="page-break-before:always;"><h1>Page 2</h1></div></body>">nul 2>&1 &&(for /l %i in (1,1,9) do (@if exist "C:\Users\Public\Documents\out2.pdf" start msedge --app="C:\Users\Public\Documents\out2.pdf#view=fitV" &exit)&timeout /t 1 >nul)
This may seem at first a bit "niche" as my need is in SumatraPDF to use for Synctex Inverse Search. However if you replace "%f" with a filename and %l with a number it can be easily used for other methodologies.
Note:
fWrapcontrols line wrapping in Notepad, for "goto" line navigationfWrapmust be set to0first. As presented here there will be a "black box" for a few seconds but will auto close.
CMD.exe /r "reg add "HKCU\Software\Microsoft\Notepad" /v fWrap /t REG_DWORD /d 0 /f >nul&echo Set s=CreateObject("WScript.Shell"):s.Run("%windir%\notepad.exe " ^& WScript.Arguments(0)):WScript.Sleep 999:s.SendKeys("%EG" ^& WScript.Arguments(1) ^& "{enter}+{end}")>%tmp%\n.vbs&cscript //nologo %tmp%\n.vbs "sync.tex" 15&&timeout 2 >nul&del %tmp%\n.vbs"Note the 3 seconds console in the background can be reduced to a minimal flash by starting the command as:
InverseSearchCmdLine =cmd /c start "" /MIN CMD.exe /r "reg add....
But without a more dedicated shim.vbs is just a cosmetic workaround.
Did you know Windows has the ability to write complex graphics apps (like even replace itself with a faster leaner version)?
I digress, what is possible in one line is a graphics WinForm, but for an RTF Editor replacement for WritePad# we would not be able to use one line.
echo using System;using System.Media;using System.Windows.Forms;using System.Drawing;class X{static void Main(){int bounce = 0;var f=new Form(){Text="Titanic Encounter",Width=660,Height=400,BackColor=Color.Blue};var j=new Label(){Text="J",Font=new Font("Webdings",90),ForeColor=Color.Gold,AutoSize=true,Location=new Point(490,125)};var m=new Label(){Text="M",Font=new Font("Webdings",70),BackColor=Color.Blue,ForeColor=Color.Gray,AutoSize=true,Location=new Point(-10,160)};var t=new Label(){Text="T",Font=new Font("Webdings",70),ForeColor=Color.FromArgb(0,128,255),AutoSize=true,Location=new Point(500,150)};f.Controls.AddRange(new Control^[^]{j,t,m});var size=72;var x=500;var y=150;var timer=new Timer(){Interval=100};timer.Tick+=delegate{if(x^>110){x-=10;t.Location=new Point(x,y);}else if(size^>4){var player = new SoundPlayer(@"C:\Windows\Media\Windows Hardware Fail.wav");player.Play();size-=3;x-=2;y+=9;t.Font=new Font("Webdings",size);t.Location=new Point(x,y);bounce++;m.Location=new Point(-10,150+(int)(Math.Sin(bounce*0.5)*10));}else{timer.Stop();}};timer.Start();Application.Run(f);}} > x.cs && "%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe" x.cs & x.exeSometimes we simply want in cmd to pick a file or folder by exploring and write the chosen folder\filename back to the console.
echo using System;using System.Windows.Forms;class P{^[STAThread^]static void Main(){var o=new OpenFileDialog();o.InitialDirectory=Environment.CurrentDirectory;o.ReadOnlyChecked=true;o.ShowReadOnly=true;if(o.ShowDialog()==DialogResult.OK)Console.WriteLine(o.FileName);}} > "%TMP%\GetFilename.cs" && "%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe" /nologo /out:"%TMP%\GetFilename.exe" "%TMP%\GetFilename.cs" && del "%TMP%\GetFilename.cs"
echo using System;using System.Windows.Forms;class P{^[STAThread^]static void Main(){var o=new FolderBrowserDialog();if(o.ShowDialog()==DialogResult.OK)Console.WriteLine(o.SelectedPath);}} > "%TMP%\GetFolder.cs" && "%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe" /nologo /out:"%TMP%\GetFolder.exe" "%TMP%\GetFolder.cs" && del "%TMP%\GetFolder.cs"
This is the enhanced single line minified version of a simple version editor for https://superuser.com/a/1933379/1769247 Improvements are in errorlevel reporting and batch progress report. Each attempt should output: Status; message; filename and the errorlevel is either 0, 1 or 2.
usage: setPDF-1.X.exe <filename>
or
setPDF-1.X.exe <minor (0-9)> <filename>
!errorlevel! 0 = report mode | !errorlevel! 0 = change mode
---------------------------------+-----------------------------
Header; PDF 1.#; filename | Success; Updated to 1.#; filename
!errorlevel! 1 = report mode | !errorlevel! 1 = change mode
---------------------------------+-----------------------------
Error; No filename; (missing) | Error; Too many args;
Error; Too small; filename | Note for TEMPLATE usage you may change a file of OVER 8 bytes (i.e. at least one linefeed or white space)
Error; File not found; filename | Error; Minor must be a single digit between 0 and 9; filename
Error; Bad header; filename | Error; Minor is not in expected position; filename
!errorlevel! 2 = report mode | !errorlevel! 2 = change mode
---------------------------------+-----------------------------
Error; Access denied; filename | Error; Access denied; filename
Error; In use (read); filename | Error; In use (write); filename
Run the following by either paste to console or include in a cmd file. Both depend on "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
echo using System;using System.IO;using System.Text;class P{static int Main(string^[^] a){if(a.Length==0)return O("Error","No filename","(missing)\n usage: "+AppDomain.CurrentDomain.FriendlyName+" <filename>\n or\n "+AppDomain.CurrentDomain.FriendlyName+" <minor (0-9)> <filename>",1);if(a.Length==1){char m;int r=H(a^[0^],false,out m);if(r!=0)return r;return O("Header","PDF 1."+m,a^[0^],0);}if(a.Length==2){if(a^[0^].Length!=1^|^|a^[0^]^[0^]^<'0'^|^|a^[0^]^[0^]^>'9')return O("Error","Minor must be a single digit between 0 and 9",a^[1^],1);char m;int r=H(a^[1^],true,out m);if(r!=0)return r;using(var f=new FileStream(a^[1^],FileMode.Open,FileAccess.Write,FileShare.None)){f.Position=7;f.WriteByte((byte)a^[0^]^[0^]);}return O("Success","Updated to 1."+a^[0^],a^[1^],0);}return O("Error","Too many args","",1);}static int H(string p,bool w,out char m){m='0';try{using(var f=new FileStream(p,FileMode.Open,w?FileAccess.ReadWrite:FileAccess.Read,w?FileShare.None:FileShare.Read)){if(f.Length^<9)return O("Error","Too small",p,1);byte^[^] h=new byte^[8^];f.Read(h,0,8);string s=Encoding.ASCII.GetString(h);if(!s.StartsWith("%%PDF-1."))return O("Error","Bad header",p,1);char c=s^[7^];if(c^<'0'^|^|c^>'9')return O("Error","Minor is not in expected position",p,1);m=c;return 0;}}catch(FileNotFoundException){return O("Error","File not found",p,1);}catch(UnauthorizedAccessException){return O("Error","Access denied",p,2);}catch(IOException){return O("Error",w?"In use (write)":"In use (read)",p,2);}}static int O(string s,string d,string f,int c){Console.WriteLine(s+"; "+d+"; "+f);return c;}} >"%TMP%\temp.cs" && "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /nologo /platform:x86 /out:"%cd%\setPDF-1.x.exe" "%TMP%\temp.cs" && del "%TMP%\temp.cs"
With the introduction of /Filter/BrotliDecode (Which can be converted in a single line command) It may be desirable to change the initial %PDF- comment to 2.0 or 2.1 so here is a 1 to 2 version
echo using System;using System.IO;using System.Text;class P{static int Main(string^[^] a){if(a.Length==0)return O("Error","No filename","(missing)\n usage: "+AppDomain.CurrentDomain.FriendlyName+" <filename>\n or\n "+AppDomain.CurrentDomain.FriendlyName+" <minor (0-9)> <filename>",1);if(a.Length==1){char m;int r=H(a^[0^],false,out m);if(r!=0)return r;return O("Header","PDF 1."+m,a^[0^],0);}if(a.Length==2){if(a^[0^].Length!=1^|^|a^[0^]^[0^]^<'0'^|^|a^[0^]^[0^]^>'9')return O("Error","Minor must be a single digit between 0 and 9",a^[1^],1);char m;int r=H(a^[1^],true,out m);if(r!=0)return r;using(var f=new FileStream(a^[1^],FileMode.Open,FileAccess.Write,FileShare.None)){f.Position=5;f.WriteByte((byte)'2');f.Position=7;f.WriteByte((byte)a^[0^]^[0^]);}return O("Success","Updated to 2."+a^[0^],a^[1^],0);}return O("Error","Too many args","",1);}static int H(string p,bool w,out char m){m='0';try{using(var f=new FileStream(p,FileMode.Open,w?FileAccess.ReadWrite:FileAccess.Read,w?FileShare.None:FileShare.Read)){if(f.Length^<9)return O("Error","Too small",p,1);byte^[^] h=new byte^[8^];f.Read(h,0,8);string s=Encoding.ASCII.GetString(h);if(!s.StartsWith("%%PDF-1."))return O("Error","Not a PDF-1.x file",p,1);char c=s^[7^];if(c^<'0'^|^|c^>'9')return O("Error","Minor is not in expected position",p,1);m=c;return 0;}}catch(FileNotFoundException){return O("Error","File not found",p,1);}catch(UnauthorizedAccessException){return O("Error","Access denied",p,2);}catch(IOException){return O("Error",w?"In use (write)":"In use (read)",p,2);}}static int O(string s,string d,string f,int c){Console.WriteLine(s+"; "+d+"; "+f);return c;}} >"%TMP%\temp.cs" && "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /nologo /platform:x86 /out:"%cd%\setPDF-2.x.exe" "%TMP%\temp.cs" && del "%TMP%\temp.cs"
This is a collection of single line commands but some depend on a split line in a file.extension due to windows limitations.
- Ghostscript is NOT a "file compressor" it writes NEW optimised files using compression. For removing encryption and adjusting compression use a PDF transformer such as cpdf or qpdf.
- Ghostscript is NOT a "file merging" program it writes a collection of NEW pages from one or more files pages. For true merging use a PDF Transformer such as cpdf or qpdf.
Take for an example Ghostscript can collate many files as new pages but windows limits how many names may be allowed on a single line. We can avoid that by using:
gs -q -sDEVICE=pdfwrite -o pages.pdf @filelist.txt
Where the files are in the command file filelist.txt (ensure any filenames with spaces are "double quoted") you only need -f if there are commands before the listed files. The nice bonus is we can add commands to add say page numbers like this @filelist.txt
- /NimbusSans-Bold 12 is the style and height
- 72 780 is the X Y position Thus near top left corner (from the lower left origin) thus any position is possible but dont expect perfect centering.
- (Page ) is the text before the number there are no leading zer0's, no trailing text. So used as bates numbers
Exhibit A-25 - Pagewill be simple 1 to last or stamping ticket pages the numbers will expand rightwards from 1 to 10 to 100 to 1000
-c "globaldict begin currentglobal true setglobal /My_working_Dict 10 dict def setglobal globaldict /My_working_Dict get begin /PageCount 0 def end end
<< /EndPage { 2 ne { 0 0 0 setrgbcolor /NimbusSans-Bold 12 selectfont 72 780 moveto (Page ) show globaldict /My_working_Dict get dup /PageCount get 1 add /PageCount
exch put globaldict /My_working_Dict get /PageCount get 256 string globaldict /My_working_Dict get /PageCount get exch cvs show true } { pop false } ifelse }
>> setpagedevice" -f
my1.pdf
"number 2.pdf"
"d:\a folder\file3.pdf"
If you want to try that on a folder full of pdfs just use the one line
(for %f in (*.pdf) do @echo "%f") > filelist.txt && gs -sDEVICE=pdfwrite -o out.pdf -c "globaldict begin currentglobal true setglobal /My_working_Dict 10 dict def setglobal globaldict /My_working_Dict get begin /PageCount 0 def end end << /EndPage { 2 ne { 0 0 0 setrgbcolor /NimbusSans-Bold 12 selectfont 72 780 moveto (Page ) show globaldict /My_working_Dict get dup /PageCount get 1 add /PageCount exch put globaldict /My_working_Dict get /PageCount get 256 string globaldict /My_working_Dict get /PageCount get exch cvs show true } { pop false } ifelse } >> setpagedevice" -f @filelist.txt
For a single file do not write a list. So use a command file with just the section starting -c ... one line as above ending ... -f call it addNumbers.txt
gs -q -sDEVICE=pdfwrite -o pages.pdf @addNumbers.txt filename.pdf
You could include the -q and -sDEVICE in the @command file then it is as simple as gs -o output.pdf @addNums filename.pcl (note GS is case sensitive.)
Postscript is a powerful programming language and this is only a small demonstration of ability to place text as an overprint.

-sDEVICE=pdfwrite -c "globaldict begin currentglobal true setglobal /My_working_Dict 10 dict def setglobal globaldict /My_working_Dict get begin /PageCount 0 def end end
<< /EndPage { 2 ne {
1 0 0 setrgbcolor
/NimbusSans-Bold 12 selectfont
72 780 moveto
(Exhibit A Page ) show
globaldict /My_working_Dict get dup /PageCount get 1 add /PageCount exch put globaldict /My_working_Dict get /PageCount get 256 string globaldict /My_working_Dict get /PageCount get exch cvs show
( of 129) show
true } { pop false } ifelse }
>> setpagedevice" -f
Note the above assumes you know the total number first and GS could be used to combine and OR show number of pages for the second pass to write the text. However, there is a better way using an application like pdfcpu or coherent cpdf which at its most basic will look like:
cpdf -add-text "Page %Page of %EndPage" -topright 20pt -font "Helvetica" -font-size 14 in.pdf -o out.pdf



