Cannot cd to a directory returned by an exe program












0














I have this simple file:



tags



home C:Usersrodde
docs C:UsersroddeDocuments
prev D:
dt C:Softwaredt


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:UsersroddeDocuments. 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.)










share|improve this question
























  • If Not "%~1"=="" dt.exe %~1
    – CatCat
    Nov 22 at 6:30










  • Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you!
    – aschipfl
    Nov 22 at 8: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 at 8: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; otherwise, you actually read the value that was present before the whole block is read...
    – aschipfl
    Nov 22 at 8:49
















0














I have this simple file:



tags



home C:Usersrodde
docs C:UsersroddeDocuments
prev D:
dt C:Softwaredt


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:UsersroddeDocuments. 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.)










share|improve this question
























  • If Not "%~1"=="" dt.exe %~1
    – CatCat
    Nov 22 at 6:30










  • Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you!
    – aschipfl
    Nov 22 at 8: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 at 8: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; otherwise, you actually read the value that was present before the whole block is read...
    – aschipfl
    Nov 22 at 8:49














0












0








0







I have this simple file:



tags



home C:Usersrodde
docs C:UsersroddeDocuments
prev D:
dt C:Softwaredt


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:UsersroddeDocuments. 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.)










share|improve this question















I have this simple file:



tags



home C:Usersrodde
docs C:UsersroddeDocuments
prev D:
dt C:Softwaredt


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:UsersroddeDocuments. 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.)







windows batch-file cd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 6:12









double-beep

1,298722




1,298722










asked Nov 22 at 6:25









coderodde

6372919




6372919












  • If Not "%~1"=="" dt.exe %~1
    – CatCat
    Nov 22 at 6:30










  • Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you!
    – aschipfl
    Nov 22 at 8: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 at 8: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; otherwise, you actually read the value that was present before the whole block is read...
    – aschipfl
    Nov 22 at 8:49


















  • If Not "%~1"=="" dt.exe %~1
    – CatCat
    Nov 22 at 6:30










  • Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you!
    – aschipfl
    Nov 22 at 8: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 at 8: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; otherwise, you actually read the value that was present before the whole block is read...
    – aschipfl
    Nov 22 at 8:49
















If Not "%~1"=="" dt.exe %~1
– CatCat
Nov 22 at 6:30




If Not "%~1"=="" dt.exe %~1
– CatCat
Nov 22 at 6:30












Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you!
– aschipfl
Nov 22 at 8:39






Please provide a more specific title of your question! the current one is nothing but totally useless! Thank you!
– aschipfl
Nov 22 at 8:39






1




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 at 8:43






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 at 8:43






1




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; otherwise, you actually read the value that was present before the whole block is read...
– aschipfl
Nov 22 at 8:49




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; otherwise, you actually read the value that was present before the whole block is read...
– aschipfl
Nov 22 at 8:49












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425000%2fcannot-cd-to-a-directory-returned-by-an-exe-program%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer




























      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.






      share|improve this answer


























        1












        1








        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 8:52

























        answered Nov 22 at 6:34









        Gerhard Barnard

        6,91631131




        6,91631131






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425000%2fcannot-cd-to-a-directory-returned-by-an-exe-program%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)