

InterviewSolution
Saved Bookmarks
1. |
Solve : Remove the last part of a string? |
Answer» <html><body><p>Is there a way to remove the last section of a <a href="https://interviewquestions.tuteehub.com/tag/string-11290" style="font-weight:bold;" target="_blank" title="Click to know more about STRING">STRING</a>, divided by \? For example:<br/><br/>C:\Documents and Settings\User\My Documents\folder_a\b.txt<br/>would become<br/>C:\Documents and Settings\User\My Documents\folder_a\<br/>But the problem is, I don't know how long the wanted part is, or the unwanted (the file name).Type FOR /? at the prompt and study the part about about variable modifiers. (Hint: %%~dpI)<br/> Quote from: Salmon Trout on February 20, 2010, 05:36:13 PM</p><blockquote>Type FOR /? at the prompt and study the part about about variable modifiers. (Hint: %%~dpI)<br/><br/></blockquote> Ahh...thank you. So how would I use that with %1? %~dp1?<br/><br/>Pre-post edit: I was right. Thanks salmon, I wasn't quite sure how to implement those. But how would I do that with <a href="https://interviewquestions.tuteehub.com/tag/regular-1182473" style="font-weight:bold;" target="_blank" title="Click to know more about REGULAR">REGULAR</a> variables? Quote from: Helpmeh on February 20, 2010, 05:41:46 PM<blockquote>how would I do that with regular variables?<br/></blockquote> <br/>Essentially you are splitting a string into two parts: (1) From the start to the final token delimiter, (2) the rest. Since <a href="https://interviewquestions.tuteehub.com/tag/dealing-2566191" style="font-weight:bold;" target="_blank" title="Click to know more about DEALING">DEALING</a> with drives, paths and filenames is a pretty common task, cmd.exe has this built in.<br/><br/>One way is to get the ordinary variable into a FOR metavariable...<br/><br/> Code: <a>[Select]</a>echo off<br/>set var=C:\Documents and Settings\User\My Documents\folder_a\b.txt<br/>for /f "tokens=*" %%A in ("%var%") do set DriveAndPath=%%~dpA<br/>echo %DriveAndPath%<br/><br/>...or pass it to a called label "subroutine"<br/><br/> Code: <a>[Select]</a>echo off<br/>goto start<br/><br/>:getpath<br/>set DriveAndPath=%~dp1<br/>goto :eof<br/><br/>:start<br/>set var=C:\Documents and Settings\User\My Documents\folder_a\b.txt<br/>call :getpath "%var%"<br/>echo %DriveAndPath% <br/>You can have the "subroutine" anywhere in the script e.g. at the end but you need to be alert to ensure, with gotos or some other method, that you don't arrive at it by mistake.<br/><br/>The %~<a href="https://interviewquestions.tuteehub.com/tag/n-236724" style="font-weight:bold;" target="_blank" title="Click to know more about N">N</a> and %~x modifiers work on URLs as well. (The others do not give very useful values)<br/><br/>Note that the string can be a fictitious one. Neither the file "C:\Documents and Settings\User\My Documents\folder_a\b.txt" nor its drive, path, folder, name, extension etc, have to actually exist. Using the date and size modifiers on a nonexistent file will return null (blank) results.<br/><br/> Quote from: Helpmeh on February 20, 2010, 05:16:46 PM<blockquote>Is there a way to remove the last section of a string, divided by \? For example:<br/><br/>C:\Documents and Settings\User\My Documents\folder_a\b.txt<br/>would become<br/>C:\Documents and Settings\User\My Documents\folder_a\<br/>But the problem is, I don't know how long the wanted part is, or the unwanted (the file name).<br/></blockquote> <br/>vbscript,<br/> Code: <a>[Select]</a>Set objFS = CreateObject("Scripting.FileSystemObject")<br/>Set objArgs = WScript.Arguments<br/>strPath = objArgs(0)<br/>WScript.Echo objFS.GetParentFolderName(strPath)<br/><br/>on command line, pass your path as arguments to the script<br/> Code: <a>[Select]</a>C:\test>cscript //nologo getpath.vbs "c:\Documents and Settings\Administrator\Desktop\shortcutlnk"<br/>c:\Documents and Settings\Administrator\Desktop<br/>More general method which can be used for strings which are not seen by cmd.exe as representing a path. Illustrates some things you can do with tokens, and you will see clues on how to get at various things, like the first, last, nth, penultimate tokens, count the tokens & so on...<br/><br/> Code: <a>[Select]</a>echo off<br/>REM get all but last token<br/>set string=C:\Documents and Settings\User\My Documents\folder_a\b.txt<br/>REM <a href="https://interviewquestions.tuteehub.com/tag/may-557248" style="font-weight:bold;" target="_blank" title="Click to know more about MAY">MAY</a> be useful<br/>set tokencount=1<br/>REM this format can handle white space delimiter<br/>set "delimiter=\"<br/>REM If delim is a space do this<br/>REM set "delimiter= "<br/>REM Create temp string for result<br/>set ResultString=<br/>:startloop<br/> REM get the first token in %%A and the remainder in %%B<br/> for /f "tokens=1* delims=%delimiter%" %%A in ("%string%") do (<br/> REM no trailing spaces!<br/> set "firsttoken=%%A"<br/> REM if remainder is blank then<br/> REM we have got to the end<br/> REM so exit<br/> if "%%B"=="" goto exitloop<br/> REM otherwise collect the remainder<br/> set restofstring=%%B<br/> )<br/> REM add first token and delimiter to result string<br/> set ResultString=%ResultString%%firsttoken%%delimiter%<br/> REM Chop off first token<br/> set string=%restofstring%<br/> REM may be useful<br/> set /a tokencount+=1<br/> REM go round again<br/> goto startloop<br/>:exitloop<br/>REM this is the last (unwanted) token<br/>set lastttoken=%restofstring%<br/>echo Result=%ResultString%<br/></body></html> | |