1194

Batch File to read 'N' characters from a text file

Question:

I have searched this across the net and found many codes for retrieving the entire line from a text or replacing the text with another but not for what i was looking for.

Using the For loop with the tokens would return on the set (word) separated with spaces.

I want to pull only a few characters from the line.

Eg: 12345qwerty67890

If this on in a text file i want to pull only '12345' and assign it to a variable.

Any help is greatly appreciated.

Answer1:

set HELP SET and try the following to get you started

@echo off setlocal enabledelayedexpansion for /f "tokens=*" %%a in (sample.txt) do ( set line=%%a set chars=!line:~0,5! echo !chars! --- !line! )

Answer2:

At the command prompt, do help set. There you will find more information about the set command than you would ever want to know. The part you are interested in says:

May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified.

Of course, in order to use that kind of stuff with the for loop variable, you need to first get acquainted with the very peculiar way in which variables are expanded in Windows NT batch files. Let me know if you have problems with that, and I can add more information.

Recommend

  • Apprequest of Android Facebook sdk 3.0
  • Docker container not starting a second time (iptables)
  • CSS button not styling
  • Control access to WebDav/Apache using Python
  • C# Create Control From a String Value
  • Split an Array into 3 arrays [duplicate]
  • Can Azure AD ADAL (ios) refresh token be revoked from the client?
  • Rails 3 Full_Calendar
  • Search for text in a string, copy & paste rows to new sheet
  • limit python input to int
  • How to add tag during runtime in C#
  • Is there a way to remove a script from a doc (using the new doc embedded script)
  • AngularJS Dynamic Directives inside ng-repeat
  • How to resume hash slots of a particular node in redis cluster in case of hard failure?
  • Querying combinations of JSON returns odd results
  • Linking with 32bit libraries under linux 64bit
  • Forward slash vs backward slash for file path in git bash
  • Can't access web service when connected to the network :: HTTP 407
  • Getting specific color for specific value using JSON data
  • Background beacon detection and Notification (Both iOS and Android) for Eddystone beacon?
  • JavaFX Embed Custom Font Not Working
  • Eric5: The OK button of 'new project' dialog is disable
  • android check if file exists with case sensitivity?
  • Using loops in Jasmine (with injected service)
  • How to convert days into months using datetime in Python3?
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Magento-Change Attribute of All Products
  • Set SelectedIndex of ListView in FlipView_SelectionChanged event
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Thumbnails for mxml components in Flex
  • PHPMailer return to AJAX
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • Android: Unable to detect vertical plane
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Debug `Unexpected end of JSON input Error` on content script
  • Apple Mach-O Linker error (“duplicate symbol”)
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • media foundation H264 decoder not working properly