0

So what I'm trying to do is to strip every character in a string except from the first one, in a parameter.

This is the closest I've found so far but it doesn't work the same way.

C:\vbackup.bat test e: c:\temp

@echo off

IF /I NOT "%2" == "c:" (
    :echo %%2:~0,1%     // I personally prefer this since it will only be used once

    :: Test
    SET var=%2
    SET var2=%var:~0,1%
    CALL :show "0,1"
    echo.
    echo Backing up entire %2 to %3\%1

    echo.
    pause
    exit
)

:show
echo Test : var=%var% var2=%var2%
GOTO :eof

Result:

Test : var=e: var2=~0,1

Community
  • 1
  • 1
Rick
  • 1
  • 1
  • 2
  • I suggest to open a command prompt window and run first `call /?`. The output help displayed on several window pages explains how to reference batch file arguments without or with modifiers like `%~d2` which expands to just drive letter with colon of second argument which is for your example `E:`. Next run in the command prompt window `set /?` and read the chapter about [delayed expansion](http://ss64.com/nt/delayedexpansion.html) which you would need here as you define an environment variable within a command block and want to access the value of this environment variable in same block. – Mofi May 18 '17 at 08:13
  • You need a normal environment variable to do [sub-string expansion](http://ss64.com/nt/syntax-substring.html), it cannot be used for argument references like `%2`. When modifying and reading a variable within a single block of code, you need [delayed expansion](http://ss64.com/nt/delayedexpansion.html). – aschipfl May 18 '17 at 08:59

1 Answers1

0

This should work:

something.bat:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
:LOOP
IF NOT "%~1"=="" (
    SET firstChar=%1
    SET firstChar=!firstChar:~0,1!
    ECHO !firstChar!
    SHIFT
    GOTO LOOP
)

Calling something.bat abc def ghi jkl will generate the output:

a
d
g
j

EDIT: Replaced [%1]==[] with "%~1"=="" as suggested in the comments.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • 1
    It's better to use `IF NOT "%~1"==""` – npocmaka May 18 '17 at 08:54
  • @npocmaka This works as well but why is `IF NOT "%~1"==""` better? – MichaelS May 18 '17 at 08:57
  • `[` and `]` have no meaning for Windows command interpreter. They are just two characters to compare, too. But a double quote has a meaning because it defines string which should be interpreted as one argument even when containing a space or ``&()[]{}^=;!'+,`~<>|``. The double quotes are also compared by __IF__. Create a batch file with first line `IF NOT "%~1"=="" (echo true) else (echo false)` and second line `IF NOT [%1]==[] (echo true) else (echo false)` and third line `echo Finished`. Next open a command prompt window and run the batch file with a single double quote as parameter. – Mofi May 18 '17 at 12:52
  • You see the error message `The syntax of the command is incorrect.` and second and third line not being executed anymore because of second line with your __IF__ condition expands to `IF NOT ["]==[] (echo true) else (echo false)` and everything after single double quote is interpreted as one argument for the __IF__ condition which means operator, second argument and command to execute are missing resulting in syntax error. – Mofi May 18 '17 at 12:57