Ishwar Goudar
Ishwar Goudar

Reputation: 11

Unable to set file path and read file name in batch file recursively using %% notaton

I need to execute set of .txt files (test scripts written using robot framework) using python command. Each test scripts are placed in a folder and all these sub folders are placed under a main folder called TestFolder. The scripts files under each subfolders looks some thing like this-

   Test_Data.txt

   Sign_In.txt

   Registration.txt

I am using a batch file to trigger this test. My code is as below :-

@echo off

D:
cd .\TestFolder
Echo Current dir: "%CD%"

echo.***        Running Test suite in Chrome       ***

For /r %%i in (*.txt) do ( 

  set SuitePath=%%i (--- I would like to get full file name)

  echo Suite path : !SuitePath!

  set SuiteName=%%~ni    (-- to get name of file without ext)

  echo Suite name : !SuiteName!

CALL pybot --variable Browser:Chrome -d D:\SmokeTestResults\TestResults_%date:~10,4%%date:~4,2%%date:~7,2%... 
)

When I executed this from command line for initial testing, I got the below ERROR:

----

Current dir: D:\TestFolder

***        Running Test suite in Chrome       ***

Suite path : !SuitePath!

Suite name : !SuiteName!

[ ERROR ] Parsing '!SuitePath!' failed: Data source does not exist.

Try --help for usage information.

Suite path : !SuitePath!

Suite name : !SuiteName!

[ ERROR ] Parsing '!SuitePath!' failed: Data source does not exist.

 .... ..... .......  ......

Please Note that, the test script folder is in D drive and the batch file is able to print the current directory properly..BUT it is not able set the suite path or suite name

Thank You. Ishwar

Upvotes: 0

Views: 399

Answers (1)

MC ND
MC ND

Reputation: 70943

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "folder=d:\TestFolder"

    pushd "%folder%" && (
        for /r %%i in (*.txt) do (
            set "SuitePath=%%~fi"
            set "SuiteName=%%~ni"

            setlocal enabledelayedexpansion
                echo Suite Path = "!SuitePath!"
                echo Suite Name = "!SuiteName!"

                CALL pybot --variable Browser:Chrome -d D:\SmokeTestResults\TestResults_%date:~10,4%%date:~4,2%%date:~7,2%... 
            endlocal 
        )
        popd
    )

To use delayed expansion, you need to first enable it.

Note that when delayed expansion is active, the presence of exclamation points in the name or path of the files becomes a problem, as the batch parser will remove or reinterpret them in the set command. If you are sure your files will never include in its name/path a exclamation point, you can directly enable delayed expansion at the start of the script and remove the inner setlocal enabledelayedexpansion / endlocal lines that are included to handle the indicated problem.

Anyway, if the variables are not needed, that is, you only need its value not the value stored in a variable, don't use delayed expansion and directly use the for replaceable parameters where you are using the variable references.

Upvotes: 2

Related Questions