-1

I have this simple file:

tags

home C:\Users\rodde
docs C:\Users\rodde\Documents
prev D:\
dt C:\Software\dt

The first column contains the tags, and the second column contains respective directories. Also, I have a program (dt.exe) that expects a tag and prints to std::cout a respective directory. For example, dt.exe docs will output C:\Users\rodde\Documents. Finally, I have a batch script dt.bat

@echo off

if [%*] == 1 (
    rem Once here, we have no arguments.
    rem Chdir to the previous directory.
    dt.exe prev > directory.tmp
    set DIR=<directory.tmp
    dt.exe --update-prev %cd%
    cd %DIR%
) else if [%1] == [-l] (
    dt.exe -l
) else if [%1] == [-s] (
    dt.exe -s
) else if [%1] == [-L] (
    dt.exe -L
) else if [%1] == [-S] (
    dt.exe -S
) else if [%1] == [-d] (
    dt.exe -d
) else (
    rem Once here, we have a tag, so chdir
    rem to respective directory
    dt.exe %1 > directory.tmp
    set DIR=<directory.tmp
    cd %DIR%
)

Unfortunately, this works only every 4th time or so.

What am I missing?

(The source code for dt.exe is here.)

double-beep
  • 3,889
  • 12
  • 24
  • 35
coderodde
  • 775
  • 3
  • 11
  • 22
  • `If Not "%~1"=="" dt.exe %~1` – CatCat Nov 22 '18 at 06:30
  • Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you! – aschipfl Nov 22 '18 at 08:39
  • 1
    `if [%*] == 1` will never be fulfilled; I guess you meant `if [%*] == []`. Anyway, the bracket syntax seems to be widely used but is very bad practice; use quotes instead like shown by CatCat: `if "%*"==""`; this protects white-spaces and other special characters. Nevertheless, the `else if` blocks could be avoided by just using `dt.exe %1`... – aschipfl Nov 22 '18 at 08:43
  • 1
    The main problem in your code is the fact that you assign a variable (`DIR`) and read it in the same block of code, which requires [delayed expansion](http://ss64.com/nt/delayedexpansion.html); otherwise, you actually read the value that was present before the whole block is read... – aschipfl Nov 22 '18 at 08:49

1 Answers1

1

I believe that your if statements are incorrectly defined, you don't need all the else statements either.. so as a start:

@echo off

if [%*] == [] (
    rem Once here, we have no arguments.
    rem Chdir to the previous directory.
    dt.exe prev > directory.tmp
    set DIR=<directory.tmp
    dt.exe --update-prev %cd%
    cd %DIR%
) else (
     if [%1] == [-l] dt.exe -l
     if [%1] == [-s] dt.exe -s
     if [%1] == [-L] dt.exe -L
     if [%1] == [-S] dt.exe -S
     if [%1] == [-d] dt.exe -d
) else (
    rem Once here, we have a tag, so chdir
    rem to respective directory
    dt.exe %1 > directory.tmp
    set DIR=<directory.tmp
    cd %DIR%
)

However, this is very tedious and unwanted. why not simply replace all of the if else with a single statement:

dt.exe %~1

So we can test by checking if %~1 is valid or not, if not, exit, if it is, execute:

if "%~1"=="" exit || dt.exe %~1

Does not matter what your input it, it will run it, unless empty. Obviously you can replace the exit with other commands.

Gerhard
  • 18,114
  • 5
  • 20
  • 38