Eric
Eric

Reputation: 368

%~1 and %1 Automatically Removing Everything after the first Space

So I am creating a batch file to store a selected folder's path into a text file to refer to it later on using another batch script...

I created registry entries to include a right-click context menu for any folder which triggers this specific batch file.

Basically When you right click a folder and click "Send to Script" it is supposed to copy the whole path / location of the right clicked folder.

To do so I am using the following command:

SET TargetDir=%~1

I also tried using %1and I also tried using the following code with delimiters

FOR /f "delims=;" %%a in ("%~1") do (
    @echo %%a
)

The problem is that CMD is automatically trimming everything after the first space and since this is a path I am copying, I want to keep all the spaces and the path as is

Ex. If I use the command on a Folder such as "C:/folder/subfolder" the copyng is done correctly But If I use the command on a Folder such as "C:/folder/sub folder" the copying is done incorrectly and will only give me "C:/folder/sub" removing all the rest found after the first space detected.

Registry Entries

[HKEY_CLASSES_ROOT\Directory\shell\send-to-script]
"MUIVerb"="Send To Script"
"SubCommands"="sendscript"


[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\sendscript]
@="Send To Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\sendscript\command]
@="C:\\scripts\\pathtotext.bat %1"

Thanks for your help

Upvotes: 1

Views: 1065

Answers (3)

Mofi
Mofi

Reputation: 49127

The batch file is not correct registered. For example look with regedit on value of registry key:

HKEY_CLASSES_ROOT\rtffile\shell\open\command

The value is displayed as:

"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" "%1"

So there are two arguments which are both enclosed in double quotes:

  1. Argument 0 is the application to run with full path, file name and file extension enclosed in double quotes as path contains a space character.

  2. Argument 1 is the name of the RTF file referenced with %1 passed by Windows Explorer with full path, file name and file extension which of course can also contain a space or any of the characters &()[]{}^=;!'+,`~ which require also enclosing entire argument in double quotes as output by cmd.exe on running it in a command prompt window with cmd /? on last help page.

So you need in your *.reg file used to import into Windows registry:

@="\"C:\\scripts\\pathtotext.bat\" \"%1\""

This string value is displayed in registry editor as:

"C:\scripts\pathtotext.bat" "%1"

And then you can use %1 or %~1 in your batch file as explained by help of command CALL output on running in a command prompt window on execution of call /?.

Upvotes: 2

Magoo
Magoo

Reputation: 80113

SET "TargetDir=%*"

Since the parameter apparently being supplied is C:\folder\sub folder then %~1 selects only the first supplied parameter of what cmd sees as two parameters.

%* means "the whole tail"

and echoing %* should show you exactly what cmd is seeing.

Upvotes: 1

jeb
jeb

Reputation: 82337

You should change your registry key [...\sendscript\command] to

@="C:\\temp\\blob.bat \"%1\""

And in your batch you should use

REM The DisableDelayedExpansion is for preserve "!" in path names
setlocal DisableDelayedExpansion
SET "TargetDir=%~1"

setlocal EnableDelayedExpansion
(
    echo target is !TargetDir!
) > C:\scripts\target.log

if "reseverdFolder" == "!TargetDir!" echo This folder is reserved

Upvotes: 2

Related Questions