Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: split a text string in DOS  (Read 33144 times)

0 Members and 1 Guest are viewing this topic.

cydaps

  • Guest
split a text string in DOS
« on: November 13, 2007, 10:12:38 AM »
Hi, I hope someone can help as I'm at the limit of my limited knowledge of DOS and .bat files...  :'(

Basically, on a Windows 2000 server, I am limited to receiving the following command from an application:

ftpupload.bat [filename.jpg]

So I may well receive the following as an example:

ftpupload.bat articles\new_logo.gif

If I was receiving the filename on it's own I'd be fine, but I need to try and split the string received at the \ character so I have "articles" and "new_logo.gif" as two separate "variables" in the DOS program.

I'm sort of getting there, I've been researching and have found the following:

   ::Deletes all characters prior to the \
   SET _completeString=articles\testing.jpg
   REM above will be replaced with %1 to take a parameter to _completeString
   SET _filename=%_completeString:*\=%
   ECHO %_filename%

This gives me the file name: testing.jpg assigned to the variable _filename however I'm confused as to how to get the test before the \ out to a variable called _directory .... :-(

The idea is FTP the file up to the server, but I need to know which directory it's to be uploaded to (in this instance "articles")... so the big question, can I split the string I'm receiving in DOS and assign this to variables or similar (excuse the terms, I tend to program VB on the whole). I could receive ftpupload.bat menu\home_button.gif or ftpupload.bat thumbnails\new_product.jpg etc... so it's not as if the directory is fixed.

Many thanks for any help!  ;D

Sorry for what may be a silly question,





« Last Edit: November 13, 2007, 10:41:59 AM by cydaps »

yperron



    Rookie
    Re: split a text string in DOS
    « Reply #1 on: November 13, 2007, 11:21:33 AM »
    in http://www.computerhope.com/forhlp.htm check the following command to tokenize your string:
    Code: [Select]
    FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
    yp
    yp

    cydaps

    • Guest
    Re: split a text string in DOS
    « Reply #2 on: November 14, 2007, 04:15:08 AM »
    yperron, thanks for your response.

    I think the fact that I'm talking about filenames may have been slightly misleading... whilst this is the string I'm having to work with, we're not actually manipulating data in the file..... I'm just simply trying to edit the string of data I'm being sent so I can work with it, and in this instance it happens to be a directly and filename, but it could equally well be something like part_one\part_two...

    So in effect, all I'm trying to do is to write "part_one" to a variable and "part_two" to another variable in the .bat file split (delimited) by the \ character.

    So far I've got:

      :Delete the character string 'ab' and everything before it
       :SET _test=%1
       :: commented out above as we're forcing the string in this example.....
       SET _test=part_one\part_two
       SET _lastbit=%_test:*\=%
       ECHO %_lastbit%
       :: will produce "part_two" in this example...

       :: To delete the string '\testing.jpg' and everything after it
       :: First delete everything before
       SET _endbit=%_test:*\=%
       ::Now remove this from the original string
       CALL SET _result=%%_test:%_endbit%=%%
       echo %_result%
       :: will produce "part_one\" in this example..


       ::Now remove the \ from the string...
       SET _finalresult=%_result:*\=%
       echo %_finalresult% 
       ::This doesn't appear to be working...


    So from looking at the help page you suggested and using FOR, I'm not really following how this works. Am I correct that this only works for files, so it will find information within a file, or can I use this to simply split a string of data? I don't really understand DOS at all, everything I've got so far is from hours of internet research, so a bit lost I'm afraid.

    Thanks again :-)




    yperron



      Rookie
      Re: split a text string in DOS
      « Reply #3 on: November 14, 2007, 07:48:03 AM »
      Hi cydaps,

      Yes a FOR loop could be used on strings, file lines and even on command line results.  shown are the difference below (see http://www.computerhope.com/forhlp.htm for all the options):
      Code: [Select]
      FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
      FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
      FOR /F ["options"] %variable IN ('command') DO command [command-parameters]


      here is a short code that does what you want:

      Code: [Select]
      @echo off
      SET _test=part_one\part_two

      for /f "tokens=1* delims=\" %%a in ("%_test%") do (
        set part1=%%a
        set part2=%%b
      )

      echo part1 = %part1%
      echo part2 = %part2%

      the output is:
      Code: [Select]
      part1 = part_one
      part2 = part_two
      yp
      yp

      cydaps

      • Guest
      Re: split a text string in DOS
      « Reply #4 on: November 14, 2007, 08:52:04 AM »
      Hey yp!!

      Thanks so much, I see now, sorry for not getting it, but seeing it written here is now quite clear, I just didn't understand the tokens and the %variable, but now it's as clear as day!

      I can see I've got a huge amount to learn with DOS!

      Thanks so much again!  :D