2

I think I have a simple problem. I have a file where I'm trying to format the output into columns. I think I found a way to do it here but the string1 and string2 variables are not setting. Any ideas? I'm not familiar with Windows scripting and come from Linux where it's much easier.

Make this:

                 San Disk USB Drive, 10-12-2013
                 Superdrive Disk USB Drive, 10-11-2013

look like this:

                 San Disk USB Drive            10-12-2013
                 Superdrive Disk USB Drive     10-11-2013

Code I'm using to read the text file:

    FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ temp_sorted_usb_history.txt"`) do (
        set "var=%%a"
        SETLOCAL EnableDelayedExpansion
        set "var=!var:*:=!"
        echo(!var!
        for /F "tokens=1,2 delims=," %%b in ("!var!") do (
                echo %%b%%c
                set string1=%%b
                set string2=%%c
                set string1="%string1%                                        "
                set string2="%string2%                                        "
                echo !string1:~-40! !string2:~-40!
            )
        ping -n 4 -w 1 127.0.0.1 >NUL
        ENDLOCAL
    )   

The problem is that I can't get the string variables to set.

3
  • 1
    try this: FOR /F "delims=" %%a in ('findstr /n "^" "temp_sorted_usb_history.txt"'). Commented Jun 11, 2013 at 17:06
  • possible duplicate of Batch file variables initialized in a for loop Commented Jun 11, 2013 at 17:58
  • Thanks, it was actually correctly capturing the line, the vars in the script were not setting. I used this instead "or /F "tokens=1,2 delims=," %%b in (temp_sorted_usb_history.txt) do". Thanks for responding! Commented Jun 12, 2013 at 14:21

2 Answers 2

3

I don't understand why you are using findstr command. This is the way I would do that:

setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=," %%b in (temp_sorted_usb_history.txt) do (
   echo %%b%%c
   set "string1=%%b                                        "
   set "string2=%%c                                        "
   echo !string1:~0,40! !string2:~0,40!
)
ping -n 4 -w 1 127.0.0.1 >NUL
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I read that people were using findstr to get around some issue which is not obvious to me. Here is the link to the reasoning for using it: stackoverflow.com/questions/206114/….
findstr is mainly used to read empty lines or to enumerate lines, but your problem have not these points, isn't it?
Thanks, btw, the script is running great now!
1

With setlocal enabledelayedexpansion

variables in parentheses are referred to as !variable! instead of %variable%

You have two strings that do not use !variable! notation, and they should.

You could however use this style of syntax where the quotes are not actually part of the variable, and eliminate the two other lines.

            set "string1=%%b                            "
            set "string2=%%c                            "

Another option might be to have the date first, and sorting by date would be simple.

@echo off
for /F "tokens=1,2 delims=," %%b in (temp_sorted_usb_history.txt) do (
echo %%c - %%b
)

1 Comment

Thanks! I'm still trying to figure out the differences ... Linux it doesn't matter ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.