1.

Solve : Sring handling problem?

Answer»

Hello,
I have a file with lines of text such as:
"C:\PROGRAM Files\something\app.exe" int.ini

I have been able to extract each line and place the text of each line into a variable.
Now comes my problem. I would like to split the two 'words' of text "C:\Program Files\something\app.exe" and int.ini into SEPARATE variables.
I've looked on the internet and found solutions that don't work. an example is:

 for /f "tokens=1,2 delims=;" %%a in ("%name%") do (
   set first=%%a&set second=%%b
   echo %first% and %second%
 )
where %name% contains my string with a ';' delimiter INSTEAD of a space.
This doesn't seem to work.
COULD someone offer an alternative with a space for the delimiter that does work.
Thanking youOne of the issues is that there is no ; in the string you provided.  the other is that delayed expansion is not on, so the %first% and %second% will be processed before they are assigned a value. You may have to manually replace the separation with a (.  If you do this should work.

Code: [Select]setlocal EnableDelayedExpansion
for /f "tokens=1,2 delims=;" %%a in ("%name%") do (
   set first=%%a
   set second=%%b
   echo !first! and !second!
)
You can't split on a space because your file path has spaces. So you need to use a double quote as your delimiter.
Code: [Select]echo off

for /f usebackq^ tokens^=1^,2^ delims^=^" %%a in ("file.txt") do (
set first=%%a
set second=%%b
)
echo %first%
echo %second%
pauseoutput
Code: [Select]C:\JFW\batch files\quote_delim>type file.txt
"C:\Program Files\something\app.exe" int.ini

C:\JFW\batch files\quote_delim>quote.bat
C:\Program Files\something\app.exe
 int.ini
Press any key to continue . . .Beat me to it by a matter of seconds 

You might want to strip leading spaces. One way is to use another FOR /F
Code: [Select]for /f usebackq^ tokens^=^ 1* %%A in ("file.txt") do for /f "tokens=*" %%C in ("%%B") do (
  set "first=%%A"
  set "second=%%B"
)
echo %first%
echo %second%
Quote from: dbenham on December 17, 2014, 05:15:07 PM

Beat me to it by a matter of seconds 
 
Gotta be quick over here Dave. Thanks for addition.  I saw the space in the 2nd token but figured they could figure out how to strip it.Shouldn't that be second=%%C.
Code: [Select]echo off
for /f usebackq^ tokens^=1^*^ delims^=^" %%A in ("file.txt") do for /f "tokens=*" %%C in ("%%B") do (
  set "first=%%A"
  set "second=%%C"
)
echo %first%
echo %second%
pauseLemonilla, thank you for fixing my example. I would like to use a space for the delimiter. Unfortunately, this may be too difficult.

Squashman, Thank you for offering another way. That is, going back one step and extracting the text form the file and splitting the text in one go. However, I notice that it only works if there is a space in the first path. There may be, in my case, where there is no space or more than one space in the first path and the script fails. As mentioned above, I might need to change my delimiter.

I am not understanding you. It works with the example you provided. If it is not working then provide a non obfuscated example of your data that it is not working with.OK, yes it does work with the example I supplied. But, as I said if there are more or less spaces in the path it fails.
I intended to enter more than one line in the file.txt file and process each line in turn.
A couple of real examples:

D:\MyProgs\DriveGleam\drivegleam.exe /startup
"C:\Program Files (X86)\DropMyRights\DropMyRights.exe" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"

Just to show a couple.Shouldn't have any problems with spaces as the code is using the double quote as the delimiter. But, you need to be consistent with your input. One example you just showed has no quotes.Thank you. I thought that script was using 2 spaces as the delimiter. Goes to show how well I understand batch scripting.
I'll use double quotes. Quote from: Frank on December 17, 2014, 08:34:24 PM
Thank you. I thought that script was using 2 spaces as the delimiter. Goes to show how well I understand batch scripting.
I'll use double quotes.
I did specifically say in my first post I was using a double quote as the delimiter. Quote from: Frank on December 17, 2014, 08:34:24 PM
Thank you. I thought that script was using 2 spaces as the delimiter.
I'll use double quotes.

if you can control the delimiter and are using filename\paths then using a pipe character is a good choice as a delimiter. 
or a * or ? or < or > as none of them can be in a path\filename.

Quotes are a poison character in batch scripts in many ways.

Quote from: foxidrive on December 17, 2014, 11:26:04 PM
if you can control the delimiter and are using filename\paths then using a pipe character is a good choice as a delimiter. 
I am in 100% agreement with you on that. It works flawlessly for us where I work.  I do data automation for a living and we have all of our customers send in their data PIPE delimited.


Discussion

No Comment Found