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.

Mike Q
  • 6,716
  • 5
  • 55
  • 62
  • 1
    try this: `FOR /F "delims=" %%a in ('findstr /n "^" "temp_sorted_usb_history.txt"')`. – Endoro Jun 11 '13 at 17:06
  • possible duplicate of [Batch file variables initialized in a for loop](http://stackoverflow.com/questions/691047/batch-file-variables-initialized-in-a-for-loop) – Andriy M Jun 11 '13 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! – Mike Q Jun 12 '13 at 14:21

2 Answers2

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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 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: http://stackoverflow.com/questions/206114/batch-files-how-to-read-a-file. – Mike Q Jun 11 '13 at 20:29
  • `findstr` is mainly used to read empty lines or to enumerate lines, but your problem have not these points, isn't it? – Aacini Jun 11 '13 at 20:59
  • Thanks, btw, the script is running great now! – Mike Q Jun 12 '13 at 01:15
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
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68