using usebackq

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

using usebackq

#1 Post by booga73 » 09 Jul 2014 11:24

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


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: using usebackq

#2 Post by Squashman » 09 Jul 2014 13:46

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: using usebackq

#3 Post by foxidrive » 10 Jul 2014 05:19

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.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: using usebackq

#4 Post by Aacini » 10 Jul 2014 14:02

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

Post Reply