1

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!

5
  • Won't pdftk work if you put the filename in quotes on its command line? Commented Apr 13, 2016 at 22:38
  • Please read the tag info for dos -- you will note that it does not apply here, so consider to remove it... Commented Apr 13, 2016 at 22:42
  • 2
    And also the title of the question needs to have "MS-DOS" removed. Commented Apr 13, 2016 at 22:42
  • 2
    I removed MS DOS from your title. There is no version of MS DOS that had C:\Program Files (x86) available, so it's extremely unlikely you're using MS DOS. This appears to be a Windows batch file. Commented Apr 13, 2016 at 22:51
  • ren %rn% % rn: =% this is incorrect, because % rn% will find a variable with space in the name, which doesn't exist Commented Apr 14, 2016 at 15:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

short file names are only available only if 8.3 file name generation hasn't been disabled before. I always have mine disabled
@LưuVĩnhPhúc: 8.3 file name creation is enabled by default. If you did disable 8.3 file name creation, then obviously this won't work. If you are like me, and do care about a system that functions properly, you won't turn off 8.3 file name creation. The performance improvements are negligibly small, except maybe for the most pathological use case of creating millions of tiny files.
You can't make sure that on the system that the script will run there'll be 8.3 names. There's absolutely no reason to leave it enabled. I've never face a problem for a decade.
In any case, I'm wondering if this is a solution to the wrong problem. I would be very surprised if you couldn't simply pass the long filenames (containing spaces) to the command if you enclose them in quotes - and then the problem goes away. Typically it's just the old 16-bit applications that don't accept spaces in filenames. But, given this is a 64-bit OS, it can't be a 16-bit application.
@IInspectable the OP's code shows that the command he's trying to use is under the "C:\Program Files (x86)" directory tree and that is usually only present on a 64-bit Windows system.
|
0
@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 ECHOed 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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.