Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Get File Name using Batch  (Read 27512 times)

0 Members and 2 Guests are viewing this topic.

jack64

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 7
    Get File Name using Batch
    « 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

    Salmon Trout

    • Guest
    Re: Get File Name using Batch
    « Reply #1 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"?


    jack64

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Windows 7
      Re: Get File Name using Batch
      « Reply #2 on: June 04, 2013, 08:22:50 PM »
      Exactly!!!!!!!

      Salmon Trout

      • Guest
      Re: Get File Name using Batch
      « Reply #3 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.





      Lemonilla



        Apprentice

      • "Too sweet"
      • Thanked: 70
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Get File Name using Batch
      « Reply #4 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
      Quote from: patio
      God Bless the DOS Helpers...
      Quote
      If it compiles, send the files.

      Salmon Trout

      • Guest
      Re: Get File Name using Batch
      « Reply #5 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


      Salmon Trout

      • Guest
      Re: Get File Name using Batch
      « Reply #6 on: June 05, 2013, 03:46:04 PM »
      Note: in the above examples, the folder c:\Batch is on my PATH.