0

I'm trying to do this :

cd C:\Users\Public
%1 /s

Where %1 is the name of and executable that changes based on a where it's being executed, but is otherwise identical. The issue is that the /s is being seen as a command or program and not an argument to the executable passed in %1. Is what I'm trying to do possible? If so, what's the syntax?

Jonathan
  • 539
  • 4
  • 15
  • possible duplicate of [How to pass command line parameters to a batch file?](http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-to-a-batch-file) – SomethingDark Apr 02 '15 at 21:06

3 Answers3

0

If you save what you have as testit.bat and then when you go to run it add the parameter after your batch file name: "testit dir" it will run the dir command with the /s switch.

Eric
  • 242
  • 1
  • 2
  • 7
  • Let me make sure I understand this, I'm a Unix guy, so Windows batch files are completely foreign to me. If I have a batch file called runit.bat and I type "runit executable" /s the executable will be run with the /s switch? If I am understanding this correctly, what do I do if the argument passed to %1 contains spaces, which will be the case more often than not? – Jonathan Apr 02 '15 at 21:17
  • If you go to a dos prompt in windows (click your start button and in the "Search programs and files" type cmd and push enter). That should open a dos window. In that dos window change to the folder where your testit.bat file is located ( cd\users\public ). – Eric Apr 02 '15 at 22:09
  • Sorry first change your batch file to %~1 instead of just %1. Then when you run it type: testit "dir /p" – Eric Apr 02 '15 at 22:19
  • so when you want to run it with your executable name put quotes around the name: runit "executable" and it will but /s on the end. – Eric Apr 02 '15 at 22:30
0

Next example could help. Note:

The cliparsearg.bat script prints all command line arguments:

==>type cliparsearg.bat
@echo OFF >NUL
set /A "ii=0"
:loopfor
    echo param %%%ii% = %0
    SHIFT
    set /A "ii+=1"
if not [%0]==[] goto :loopfor
echo   all %%* = %*
goto :eof

==>cliparsearg.bat withoutspaces /S 3rdPar
param %0 = cliparsearg.bat
param %1 = withoutspaces
param %2 = /S
param %3 = 3rdPar
  all %* = withoutspaces /S 3rdPar

==>cliparsearg.bat with 2 spaces /S 3rdPar
param %0 = cliparsearg.bat
param %1 = with
param %2 = 2
param %3 = spaces
param %4 = /S
param %5 = 3rdPar
  all %* = with 2 spaces /S 3rdPar

==>cliparsearg.bat "with 2 spaces" /S 3rdPar
param %0 = cliparsearg.bat
param %1 = "with 2 spaces"
param %2 = /S
param %3 = 3rdPar
  all %* = "with 2 spaces" /S 3rdPar

Resources (basic required reading for Windows batch scripters):

JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

Thanks for all your help guys. I looked at the documentation and your comments and, though I'm still completely unsure of how this is actually supposed to work, I got it working sensibly. Here's the solution I came up with:

"%~1" /s
Jonathan
  • 539
  • 4
  • 15
  • If my or **any other** answer was helpful, please consider marking it as accepted. [See this page](http://meta.stackexchange.com/questions/5234/) for an explanation of why this is important. – JosefZ Feb 24 '16 at 12:25