Computer Hope

Microsoft => Microsoft DOS => Topic started by: jack64 on June 04, 2013, 11:41:29 AM

Title: Get File Name using Batch
Post by: jack64 on June 04, 2013, 11:41:29 AM
Scenario:- A new right click option "Get File Name" is implemented for a particular extension ..lets say for .txt   
The option "Get File Name' is added for the txt file using Regedit and the action for "Get File Name" is pointed to a batch script.
So when the Get File Name option is selected , the filename which called the script along with the path has to be copied into the batch.
Please provide your ideas and suggestions how to implement the above..Thanks
Title: Re: Get File Name using Batch
Post by: Salmon Trout on June 04, 2013, 12:53:29 PM
when the Get File Name option is selected , the filename which called the script along with the path has to be copied into the batch.

By "the filename which called the script" do you mean the "name of the file which was right clicked by the user"?

Title: Re: Get File Name using Batch
Post by: jack64 on June 04, 2013, 08:22:50 PM
Exactly!!!!!!!
Title: Re: Get File Name using Batch
Post by: Salmon Trout on June 05, 2013, 11:32:38 AM
I bet it's held in %1. In a batch script, the replaceable parameter %1 contains the first (or only) parameter passed to the script. If it contains a valid file name, it can be modified with the standard variable modifiers (see the FOR documentation for details - type FOR /? at the prompt) ~d is drive, ~p is path, ~n is name, ~x is extension.

%1 is the raw parameter
If it is a file name, it may already hold the full drive and path, but you can check with simple tests

echo %1
echo %~d1
echo %~p1
echo %~n1
echo %~x1


They can be combined:

echo %~dpnx1

Passed to a variable:

set parameter=%1
echo Parameter is: %parameter%


(etc)

So try that & report results if you need further help.




Title: Re: Get File Name using Batch
Post by: Lemonilla on June 05, 2013, 01:38:43 PM
%0 is the name of the batch file that is being executed. %1 is the first argument from the calling program. Same modifications apply to %0 as to %1
Code: [Select]
C:\users\lemonilla>batchFile.bat argument1 argument_2 argument-3 "Argument 4"
If you entered this into command prompt,

%0 = "C:\users\lemonilla\batchFile.bat"
%1 = argument1
%2 = argument_2
%3 = argument-3
%4 = "argument 4"
%5 = Undefined
%6 = Undefined
%7 = Undefined
%8 = Undefined
%9 = Undefined
Title: Re: Get File Name using Batch
Post by: Salmon Trout on June 05, 2013, 01:58:47 PM
%0 is the name of the batch file that is being executed.

%0 unmodified is the command line that was used to call the batch.

paramtest.bat (in folder c:\Batch)

Code: [Select]
@echo parameter 0 is %0
Code: [Select]
c:\>paramtest
parameter 0 is paramtest
c:\>paramtest.bat
parameter 0 is paramtest.bat
c:\>c:\Batch\paramtest.bat
parameter 0 is C:\Batch\paramtest.bat

However...

paramtest2.bat (in c:\Batch)

Code: [Select]
@echo this script is %~dpnx0
Code: [Select]
c:\>paramtest2
this script is c:\Batch\paramtest2.bat

Title: Re: Get File Name using Batch
Post by: Salmon Trout on June 05, 2013, 03:46:04 PM
Note: in the above examples, the folder c:\Batch is on my PATH.