Results 1 to 2 of 2

Thread: Set a variable to batch result/output

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    11

    Set a variable to batch result/output

    Hi all,
    With the below script, I get what I need. The result is a folder name. I need to set e variable to that output/result, and use it in a path to copy a folder from the same batch/script:

    "@echo off
    setlocal EnableDelayedExpansion
    set line=
    < file.txt (for /F "delims=:" %%a in (
    'findstr /N /C:"Word-to-find" file.txt') do (
    set /A skip=%%a-lastLine-1, lastLine=
    for /L %%i in (1,1,!skip!) do set /P line=
    for /L %%i in (1,1,1) do set /P "line=!line:~14!" & echo/
    echo/
    ))"

    Need to use the result/output like this:
    "chdir /D %~dp0
    xcopy /Y file "%systemdrive%\test\%var%" /E
    pause"

    Have not been able to set a variable to the above output/result.

    If fact what I'm trying to do is:
    1) find a string/word in a txt file
    2) go 1 line above it
    3) get that string minus 14 characters
    4) use the output in a path, as it is a folder name. It changes every time

    Any help would be much appreciated. Thank you
    Last edited by bertikul; Jan 2nd, 2018 at 10:35 AM. Reason: explain more what I'm trying to do

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    11

    Re: Set a variable to batch result/output

    I solved it by changing the script a little. It works now.

    @echo off
    setlocal EnableDelayedExpansion
    set line=
    < file.txt (for /F "delims=:" %%a in (
    'findstr /N /C:"Word-to-find" file.txt') do (
    set /A skip=%%a-lastLine-1, lastLine=%%a
    for /L %%i in (1,1,!skip!) do set /P line=
    for /L %%i in (1,1,1) do set /P "output=!line:~14!" & echo/
    echo/
    ))
    xcopy /Y file "%systemdrive%\test\%line:~14%" /E
    pause

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width