Leajian
Leajian

Reputation: 129

Command: for | It won't accept my variable in skip=%var% option

Well, I am kinda stuck on the following part of my code. This program will count the number of lines in the file usernames_list.txt. On each line there is a name of a folder I want to enter. I want that program to enter each folder in my list, create a file called test_1 and then go to it's parent folder. This should be reapeated until it reaches the end of the list. Am I doing it right? :/ For some reason the "skip" option won't accept my variable.

    for /f %%C in ('Find /V /C "" ^< usernames_list.txt') do set lines=%%C
    set times=0
    set /A skip_value=%lines%-(%lines%-%times%)
    :redo
    FOR /F "skip=%skip_value%" %%b IN (usernames_list.txt) DO (
    cd %%b
    echo > test_1
    cd ..
    set /A times=%times%+1
    if /i {%times%}=={%lines%} (goto continue)
    goto redo
    )
    :continue
    pause

Upvotes: 3

Views: 2232

Answers (1)

dbenham
dbenham

Reputation: 130849

As currently written, set /A skip_value=%lines%-(%lines%-%times%) will always evaluate to 0 because times = 0.

The SKIP value must be >= 1. Setting SKIP=0 results in a syntax error.

It seems to me your logic for computing skip_value is flawed. But even if you fix the logic, you still have to worry about values that are <= 0. I handle that situation by defining a SKIP variable with the entire option text only if the value is >= 1.

set "skip="
if %skip_value% geq 1 set "skip=skip=%skip_value%"
for /f "%skip%" %%b in (usernames_list.txt) ...

You might need additional FOR /F options. That is not a problem. For example:

for /f "%skip% delims=" ...

Upvotes: 6

Related Questions