Page 1 of 1

using usebackq

Posted: 09 Jul 2014 11:24
by booga73
in using usebackq, my attempt was to read the last token of each line, but tokens vary per line, so that's why I need to read the last token.

I interpreted that the command, usebackq, that it would reverse how the token will be processed / read, ie- as it will permit reading the last token.

usebackq - specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.


What would be the correct code? if I can get some assistance, please / thank you! v/r Booga73

Code: Select all


@ECHO OFF
for /f "usebackq tokens=1 delims==" %%a in (c:\mystring.txt) do (
   echo %%a >> c:\mystring.txt
)




contents of mystring.txt
---------------------------------------------------

    SystemRoot system32 drivers 1394ohci.sys
    C: Program Files CommonFiles ActivIdentity ac.sharedstore.exe
    SystemRoot system32 drivers accelern.sys
    SystemRoot system32 drivers Accelerometer.sys
    system32 drivers ACPI.sys
    SystemRoot system32 drivers acpipmi.sys
    system32 drivers ADIHdAud.sys
    %SystemRoot% system32 AEADISRV.EXE
    SystemRoot system32 drivers afd.sys
    SystemRoot system32 drivers agp440.sys


Re: using usebackq

Posted: 09 Jul 2014 13:46
by Squashman
No it does not work in reverse.
The most simple way to understand when to use the usebackq option is if you have a file path with spaces in it. Then you need to use usebackq otherwise the FOR command thinks it is parsing a string and not the actual contents of the file.

Re: using usebackq

Posted: 10 Jul 2014 05:19
by foxidrive
If you want the last space delimited token in a file then give this a run:

Code: Select all

type mystring.txt | repl ".* (.*)" "$1" >newfile.txt


This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Re: using usebackq

Posted: 10 Jul 2014 14:02
by Aacini
You may use this method:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (input.txt) do (
   for %%b in (%%a) do set lastToken=%%b
   echo !lastToken!
)

Antonio