Reputation: 11
I am trying to get a setup where I can right click on files and select a context command. I've put the .bat into the registry and everything like that. My issue is that I want to remove the spaces of the file I right clicked on, because the command line of the other program I am using seems to be incompatible with spaces in the file names.
@echo off
SET rn="%1"
ren %rn% % rn: =%
SET input="%1"
SET outname="%~d1%~p1%~n1signed.pdf"
cd "C:\Program Files (x86)\PDFtk Server\bin"
pdftk %input: =% background Z:\Documents\docsig.pdf output %outname: =%
pause
I'm very new at batching. Maybe the solution is simple? The issue is that I do not want the batch to rename multiple files at a time. Everything I've found that works does the whole directory at once.
@echo off
setlocal enabledelayedexpansion
for %%a in (* *) do (
set file=%%a
ren "!file!" "!file: =!"
)
Above is one such example. Is there a way to tweak this to change just the file I right clicked on?
Thank you in advance!
Upvotes: 1
Views: 1913
Reputation: 51355
The solution is really a lot easier: Simply use the short (8.3) pathname. Short pathnames do not contain spaces. Added bonus: You don't need to rename any files at all.
The following batch script displays all files in a directory to illustrate the use of the %~s1
modifier. You can easily adapt it to your needs:
for %%a in (*.*) do (
@echo %%~sa
)
In your first script you can drop the two lines trying to rename the file, and use SET input="%%~s1"
instead.
The batch parameter modifiers are documented under Using batch parameters.
Upvotes: 2
Reputation: 79983
@ECHO OFF
SETLOCAL
@echo OFF
:: set rn to name and extension only of param1
SET "rn=%~nx1"
:: remove spaces
SET "rn=%rn: =%"
:: Note ren syntax - ren "full name" "filenameonly"
ECHO(ren "%~1" "%rn%"
:: set inputdir to drive and path from param1
SET "inputdir=%~dp1"
:: set outname to name-only of param 1, then string directory,name-sans-spaces+signed.pdf
SET "outname=%~n1%"
SET "outname=%~dp1%outname: =%signed.pdf"
REM cd "C:\Program Files (x86)\PDFtk Server\bin"
ECHO(pdftk "%inputdir%%rn%" background Z:\Documents\docsig.pdf output "%outname%"
pause
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO(REN
to REN
to actually rename the files.
DO the same with pdftk
I've commented-out the directory-change since that dir does not exist on my machine.
Documentation is comments in file.
The syntax SET "var=value"
(where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a
can safely be used "quoteless".
Upvotes: 0