How to append output file name using input file name without extention while using ghostscript? - windows

I'm using GS to "compress" PDF with 2 clicks. I've added a context menu in windows register with abovementioned code.
For instance if I use it on test.pdf the output file will be test.pdf-compressed.pdf. It works, but I would like to get rid of extention in the filename. Is there any way to do so?
I've tryied to use cmd arguments, but it does not seem to work with the postscript.
C:\Program Files\gs\gs9.27\bin\gswin64c.exe -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dCompatibilityLevel=1.4 -sOutputFile=%1%-compressed.pdf -c .setpdfwrite -f %1

This isn't a Ghostscript question. If you get the arguments correct then the parameters passed to Ghostscript will be correct and the output file will be what you want.
You haven't said what you've tried, so that makes it pretty hard to make suggestions. However you should be able to use %~dp1 and/or %~n1 instead of simply %1 to expand to just a path or file. There are other variations, typing "help for" at the Windows command line will give you more details.
Note as always that Ghostscript does not compress PDF files, by using -dPDFSETTINSG=/ebook you are producing a brand-new PDF file which has altered the content from the original (image will be downsampled for example).
Also the sequence -c .setpdfwrite -f has been redundant for years, you don't need it.
[EDIT]
This batch file demonstrates the use of the command shell variable expansion in a batch file
#ECHO OFF
ECHO Input file is %1
ECHO Input directory is %~dp1
\ghostpdl\debugbin\gswin32c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=%~dp1\new.pdf %1
When saved as test.bat and then test d:\temp\input.ps the following output is generated:
Input file is d:\temp\input.ps
Input directory is d:\temp\
GPL Ghostscript GIT PRERELEASE 9.28 (2019-04-04)
Copyright (C) 2019 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
And a file new.pdf is created in the directory d:\temp
So the variable expansion works as expected, because it happens before the command line is executed.
If it still doesn't work for you, then you're going to have to provide more information. In your place I would start by removing the -dNOPAUSE and -dBATCH switches from the command line, at least that way you'll be able to see if Ghostscript is trying to tell you something.

I've clearly stumbled upon this posting a bit late.
However, I wanted to post my answer in case someone comes looking for a solution to a similar issue, in the future.
I started by creating a new Folder on my Desktop, titled "PDF", in which I placed the "test.pdf" File.
I then created a .BAT File, titled "CompressPDF.bat", to which I added the Script below.
This Script will Loop through and Compress Any/All .PDF Files, that are placed in the "PDF" Folder.
It then correctly appends the "-compressed.pdf" string to the File Name, thereby saving the "test.pdf" File as "test-compressed.pdf", per the request of the OP.
As you will Notice, I have Added the "PAUSE" Command at the very end of the Script.
This will keep the Window from Automatically Closing until you Press Any Key, which will allow you to review any Errors that may have arisen, during the compression process.
#echo off
cd "%USERPROFILE%\Desktop\PDF"
for %%f in (*.pdf) do (
gswin64c.exe -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="%%~nf-compressed.pdf" "%%f"
)
PAUSE
I hope this post is able to help others, who may be looking for an answer to a similar issue.
Please, feel free to response or to reach out to me, if anyone has any questions, as I am always happy to help.

Related

Delete the input file after GhostScript finishes converting to PDF

Can someone show me how to use the PostScript deletefile operator to delete the input file after GhostScript finishes converting the input file to a PDF file.
This appears to work for me, first creating the PDF file, then setting the permissions on the input file, and finally deleting the input file.
"C:/Program Files/gs/gs9.55.0/bin/gswin64c.exe" -q -sDEVICE#pdfwrite
-o "C:/Temp/Temp_0001.pdf"
-f "C:/Temp/Temp_0001.ps"
--permit-file-all=C:/Temp/Temp_0001.ps
-c (C:/Temp/Temp_0001.ps) deletefile
NOTE: Since I had to switch to Unix-style path separators (even though I am running this on Windows) for the permit-file-all and the deletefile, I decided to use the same convention for both the output and input files as well. Windows seems to be OK with that, and the convention was uniformly used for all paths/files.

complete noob, How do I make a script to convert individual pages of a PDF as images AND save them in folders with the same name as the PDF?

For example, if I have book1.pdf and book2.pdf, I would like to create a script where the pages of the pdfs are converted to images and are saveed in their separate folders: book1 folder and book2 folder.
It's something this program does but I do not want to pay 27 bucks just for this.
I'm a complete noob when it comes to coding. I installed Ghostscript and added a printer that runs ghostscript, so now I do have the option of opening a PDF (or any document), and print using the Ghostscript printer, and it outputs the resulting images to a folder.
This is the code for printer properties->ports->arguments for this program:
-sDEVICE=jpeg -r300 -dJPEGQ=100 -o -dSAFER -sOutputFile="C:\IMAGEfiles\image%%03d.jpg" -
My goal now is to automate the system so that I can have a list of PDFs and convert their pages into images and sorted into folders based on the same name as the PDFs. Thank you
This isn't a Ghostscript question really, this is a shell script programming problem.
Since you are using C: I'm assuming you are on Windows. I'm also going to assume you have created the folders in advance.
If you then open a command shell and do :
for %s in (*.pdf) do "c:\program files\gs\gs9.52\bin\gswin64c" -sDEVICE=jpeg -r300 -dJPEGQ=100 -dBATCH -dNOPAUSE -sOutputFile=c:/%~ns/image%03d.jpg %s
That will find all the files with names of the form *.pdf, execute Ghostscript (you may have to alter the paths and executable name, it depends on the version you installed) and output the resulting JPG files to a folder whose names is the '*' part of the input filename.
Note that your original command line has both -o and -sOutputFile, you should modify it to remove one or the other. -o is supposed to be followed by the name of the output file and includes -dBATCH and -dNOPAUSE all wrapped up as one. Whereas -sOutputFile= just sets the output filename. Using both is a bad idea, if it works I'm surprised, and it certainly wouldn't surprise me if it stopped working at some point, or had unexpected side effects.

How to use Windows Batch File to mass unzip files and saving them in specific folders?

I got the code to mass unzip from this link. But it unzips everything in the folder where the batchfile exists. I want it to unzip it to specific folders or its individual folders.
Note: my bz2 files are in various folders outside the folder where the batchfile exists.
Here's the script that i used :
for /R "C:\Users\victor\Desktop\MASTERS\color feret\disc 1\data\images" %%I in ("*.bz2")
do ("%ProgramFiles%\WinRAR\WinRAR.exe" x -y -o"%%~dpnI" "%%~fI")
Can someone enlighten me how to do it?
And also if possible, can anyone explain to me what are the arguments for? "x , -y -o %%dpnI " etc. Thanks
You do not need a batch file for this process at all. Start WinRAR, select all the archives you want to extract, click on Extract To in toolbar, select the base destination folder, check the option Extract archives to subfolders in Miscellaneous group and press button OK. That's it.
From a command line in a console window with current working directory being the directory containing all the *.bz2 files to extract:
"%ProgramFiles%\WinRAR\WinRar.exe" x -ad -y *.bz2 C:\Temp\
There is no need for a for loop as WinRAR supports wildcards for archive file names.
And with option -ad the archive file First.bz2 is extracted to folder C:\Temp\First, archive file Other.bz2 is extracted to C:\Temp\Other, and so on. With checking option Extract archives to subfolders in GUI, you use option -ad.
Help of WinRAR contains the page Switch -AD - append archive name to destination path. Click in menu Help on Help topics. On tab Contents open Command line mode and open Commands and Switches. Also text file Rar.txt in program files folder of WinRAR contains a description for command x and the options -ad and -y and all other commands and options for console version Rar.exe.
But if you want to use nevertheless a for loop and want to know what %%~dpnI and %%~fI mean, open a command prompt window, enter either help for or for /? and read.

Ghostscript: how to merge PDFs with wild card on Windows

I'm trying to merge all *.pdf in directory :
gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=Total_Files.pdf -dBATCH *.pdf
This is perfectly work for me on Linux, but when I run it on Windows
I'm getting **Error: /undefinedfilename in *.pdf.**.
Please could some one help me with this.
(My problem was practically exactly the same). My solution (with the help of previous answers) answers the original question slightly better (an actual MS-DOS example is below)
del filename.lst
for %%s in (C:\somefolder\some?wildcards*.pdf) do ECHO %%s >> filename.lst
gswin64c.... #filename.lst
To explain;
'>>' means append in MS-DOS - so firstly we delete file filename.lst
I read (just now in some other place) that %%s in MS-DOS batch files works (instead of %s). Obviously - one day the filenames may contain spaces (as mine did already) so better be safe and quote the filenames. So the better batch file is;
del filename.lst
for %%s in (C:\somefolder\some?wildcards*.pdf) do ECHO "%%s" >> filename.lst
gswin64c.... #filename.lst
Right now I just used this for inputting many EPS files - but many PDF files work fine too; as in the above example - I actually tested it with both - my result is a PDF with many EPS files in it - and many pages from multiple PDF files in one PDF (as per the question).
There was a previous question on this topic, the answer is the same, Ghostscript does not allow wildcards in the input filename, you must specify each file you want to have as input.
Why does it work on Linux ? Because the shell you are using expands '*.ps' to a full list of files before passing the command line to Ghostscript.
To do this in Windows you will need to execute a shell script, pipe the filenames to a file, then supply the file as an argument to GS.
EG, something like
for %s in (*.ps) do ECHO %s >> filename.lst
gswin64c.... #filename.lst
As alternative to the for loop
dir /b /o:n *.ps > filename.lst
gets the job done (/b to get the files only, /o:n to sort by name).
To solve the sorting problem completely, you could rename the first 9 files to 01->09, or open the output file in notepad and handle these few cases manually. But if you will have more than 100 files, this could be bothersome.
Sorry to stir up this one year old thread, which helped me with my problem, but I thought using 'dir' is easier, more flexible and doesn't need you to delete the file list before starting.

Ghostscript: ps2pdf doesn't work with Win 7 32-bit

I have the latest GPL Ghostscript v9.05 and I am running it on Win 32 bit systems. On my XP machine, both commands
ps2pdf -v -
and
rungs -v (used internally by TeXLive)
report of Ghostscript 9.05 being available as follows:
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
On another Win 7 computer, the command
ps2pdf -v -
at the command line is not recognised as being a valid ps2pdfsyntax but rungs -v works fine.
I have read on the internet about setting temporary directories for Ghostscript here:
http://schlingel.bplaced.net/?p=54
and it says basically to edit the gssetgs.bat file in the lib folder of Ghostscript and add the lines:
set path=%ProgramFiles%\gs\gs9.02\lib;%ProgramFiles%\gs\gs9.02\bin;%windir%\SysWOW64
set TMP=%YOUR_TEMP%
set TEMP=%TMP%
This needs to be modified appropriately by replacing 9.02 with 9.05 in my case. Now on Win 7, how should the last two lines regarding the temporary folder be? Can I have for Set TMP as follows:
set TMP=%"C:\Users\hihi\AppData\Local\Temp"%
Is it the right syntax?
I have also put the tmp and temp variables in my Environment variables.
Update
A. Using ps2pdf, here is how I convert a PS to a PDF file on my Win 7 machine and I get an error:
C:\work\misc>ps2pdf -dNOSAFER -sDEVICE=pdfwrite -r720 -dCompatibilityLevel=1.5 -dUseFlateCompression=true -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -dEPSCrop "%1.ps" "%1.pdf"
Unknown device:
Unrecoverable error: undefined in .uninstallpagedevice
Operand stack:
defaultdevice
Note: the above command works fine on my Win XP machine!
B. Using gswin32c here is how I convert a PS to a PDF file on my Win 7 machine and this works:
C:\work\misc>gswin32c.exe -o "%1.pdf" -dNOSAFER -sDEVICE=pdfwrite -r720 dCompatibilityLevel=1.5 -dUseFlateCompression=true -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -dNumRenderingThreads=2 -c "60000000 setvmthreshold" -f -dEPSCrop "%1.ps"
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Loading NimbusSanL-Regu font from %rom%Resource/Font/NimbusSanL-Regu... 2837152
1454727 4413848 3026018 1 done.
Loading Dingbats font from %rom%Resource/Font/Dingbats... 2837152 1510862 457461
6 3180865 1 done.
Loading NimbusSanL-Bold font from %rom%Resource/Font/NimbusSanL-Bold... 2857248
1553499 4655000 3251960 1 done.
Note: the above command also works fine on my Win XP machine
To summarise: I have problem with ps2pdf on my Win 7 machine.
New (May 09, 2012)
This is what I get when I rem the last two lines as Ken suggested:
C:\work\misc>ps2pdf -dNOSAFER -r720 -dCompatibilityLevel=1.5 -dUseFlateCompression=true -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -dEPSCrop "%1.ps" "%1.pdf"
Unrecoverable error: typecheck in .putdeviceprops
avoiding clean up
The temp folder has those temporary files you mentioned.
The contents of _.at:
-dCompatibilityLevel#1.4
-dNOSAFER
-r720
-dCompatibilityLevel
The contents of _.at2:
-q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE#pdfwrite
-sOutputFile#-dUseFlateCompression
-dCompatibilityLevel#1.4
-dNOSAFER
-r720
-dCompatibilityLevel
-c .setpdfwrite -f1.5
Update May 11
Note: This is working fine now on my win 7 machine
C:\work\misc>ps2pdf -dNOSAFER -r720 -dCompatibilityLevel#1.5 -dUseFlateCompression#true -dMaxSubsetPct#100 -dSubsetFonts#true -dEmbedAllFonts#true -dEPSCrop "defense.ps" "defense.pdf"
avoiding clean up
The contents of _.at:
-dCompatibilityLevel#1.4
-dNOSAFER
-r720
-dCompatibilityLevel#1.5
-dUseFlateCompression#true
-dMaxSubsetPct#100
-dSubsetFonts#true
-dEmbedAllFonts#true
-dEPSCrop
The contents of _.at2:
-q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE#pdfwrite
-sOutputFile#"defense.pdf"
-dCompatibilityLevel#1.4
-dNOSAFER
-r720
-dCompatibilityLevel#1.5
-dUseFlateCompression#true
-dMaxSubsetPct#100
-dSubsetFonts#true
-dEmbedAllFonts#true
-dEPSCrop
-c .setpdfwrite -f"defense.ps"
Thanks.
Far more likely than a temporary file problem is permissions on the directory where you are trying to write the destination file. The error message you quote occurs long before any temporary files are used, but is a very common error if you try to write to a directory which does not exist, or which the process has no write permission for.
First thing to do is post the actual gswin32 command line you are using.
The syntax you are querying is incorrect. %value% is a Windows scripting operation which says 'replace the stuff between the % signs by the named value'. So if I declare 'set VAL=c:/temp' Then I can say 'set NEWVAL=%VAL%/New' which will make NEWVAL 'c:/temp/new'. You can find more about windows scripting in the Windows help, or by a quick Google.
Given that 'ps2pdf' (which is a Windows script) can't be found on your Windows 7 machine (at least I assume that's what you mean by 'is not recognised as being a valid ps2pdf command') it does seem like you need to add the Ghostscript paths to your environment. Simply altering gssetgs.bat on its own will do nothing, you need to ensure that this script file is called from your autoexec.bat script, so that the additional environment settings are applied.
If you aren't sure what autoexec.bat is, or how to modify it, then again Google should help you pretty quickly.
Did you actually install Ghostscript, or simply copy it ?
Given that Ghostscript works correctly, the problem must be in the shell script 'ps2pdf', or more accurately some change in Windows 7 is causing the old script not to work.
This script is (unfortunately) rather more complex than I would like (I didn't write it). it actually uses about three different scripts to do the work. You really need to find out what is being sent to GS.
Probably the simplest way to do this is to edit 'ps2pdfxx.bat'. At the :end label you'll see 'rem Clean up' followed by two lines beginning 'if exist'. Put rem in front of those two. Add a line which says 'echo avoiding clean up' The end of the file should look like this:
:end
rem Clean up.
rem if exist "%TEMP%_.at" erase "%TEMP%_.at"
rem if exist "%TEMP%_.at"2 erase "%TEMP%_.at"2
echo avoiding clean up
Now run your command line (by the way you really don't need to put -sDEVICE= when using ps2pdf.....)
In your TEMP directory you should have files called _.at and possibly _.at2 which will contain the actual commands being sent to GS.
OK the file _.at is copied into the file .at2, and then.at2 is used as the list of arguments to Ghostscript. Commenting up the file you got:
---This line added by the batch file ps2pdfxx.bat
-q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE#pdfwrite
---These lines come from _.at
-sOutputFile#-dUseFlateCompression
-dCompatibilityLevel#1.4
-dNOSAFER
-r720
-dCompatibilityLevel
--This line added by the batch file ps2pdfxx.bat
-c .setpdfwrite -f1.5
There are a number of problems with this:
-sOutputFile#-dUseFlateCompression
This in effect sets the output file to '-dUseFlateCompression'
-c .setpdfwrite -f1.5
I'm not completely sure what this will do. Either it will handle the -f properly and terminate the PostScript input, or it will ignore it as an unrecognised switch (probably the latter). The '1.5' ought to be the input filename, without that Ghostscript doesn't know which file to use..... Even if it did, it will try to write the output to a bogus filename.
To be honest I would suggest that, if you want to set all these switches, you simply invoke Ghostscript directly rather than trying to use the script. In fact I'd recommend that anyway, every time I look at these scripts I shudder more.
Almost everything that the ps2pdf script is doing is being overridden by your command line, or is not required in the first place.

Resources