home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch      Fun with MS-DOS batch files      42,547 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 40,726 of 42,547   
   gwhite@ti.com to All   
   Re: FOR /F and delims=\ string processin   
   14 Aug 13 11:39:00   
   
   > On Monday, August 12, 2013 6:14:25 PM UTC-7, foxidrive wrote:   
   >> On 13/08/2013 10:56, gwhite@ti.com wrote:    
   >> In a batch file, I want to assign a variable as a string with the current   
   directory stripped of the parent directories. > > Say I am "in" (batch file   
   doesn't know where it is run from):    
   >>    
   >> C:\a\b\c\d    
   >>    
   >> I want to set a variable to d.    
   >>    
   >> If I do    
   >>    
   >> set x=%cd%    
   >> echo %x%    
   >>    
   >> Then the command screen shows:    
   >>    
   >> C:\a\b\c\d    
   >   
   > Try this: @echo off for /f "delims=" %%a in ("%cd%\.") do set    
   folder=%%~nxa" -- foxi   
      
   Oh, thank you very much!  Fantastic.     
      
   I kind of see what it does.  With 20-20 hindsight, some of it almost appears   
   obvious.  Isn't that always the way?   
      
   To me, it seems that the parsing part of the FOR "engine" only sees it as a   
   string.  That means it does not know if "d" is a file or a folder, so one can   
   simply extract a folder name by using file syntax.  It would be the same if   
   the folder or file name    
   was "d.x".  (It has an extension.)   
      
      
   The part I don't quite get in this is the "\." suffix.  Once you got me on the   
   right track, I tried all these "in (...)" combos and got the same results for   
   all:   
      
   for /f "delims=" %%G in ("%cd%\.") do set "folder=%%~nxG"    
   echo.   
   echo    %%cd%%\.    %folder%   
   echo.   
   for /f "delims=" %%G in ("%cd%.") do set "folder=%%~nxG"    
   echo.   
   echo    %%cd%%.     %folder%   
   echo.   
   for /f "delims=" %%G in ("%cd%") do set "folder=%%~nxG"    
   echo.   
      
   The syntax for all of the above work whether the directory is "d" or "d.x"   
      
   For me trying to understand the command line, I ask what does the "\." suffix   
   do?   
      
   I also noticed I can eliminate "delims=" and get the same results for that   
   path.  But it breaks if there are spaces (like C:\a\b\c c\d), so I am I right   
   that "delims=" sets the delimiter to "nothing?"  (Meaning, there are no   
   delimiters, take the whole    
   string.)   
      
   Thanks again.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca