

InterviewSolution
Saved Bookmarks
1. |
Solve : Data Manipulation? |
Answer» <html><body><p>I need this in DOS only:<br/><br/>I have a file which contains a list of paths with filenames, how do I retrieve everything up to the last backslash, and the filename into separate variables?<br/><br/>\\UBIX\RPTOUT\EXT\file001.txt<br/>\\UBIX\FIN\payments.csv<br/>\\UBIX\INT\EXT\ioweyou_bal_02242010.csv<br/>\\UBIX\commisiom_sum_YTD_20100224.pdf<br/><br/><br/>so I want VAR1 have the path<br/>and want VAR2 have the filenameHere's one method: (where Trial.txt is the file containing input data)<br/><br/> Code: <a>[Select]</a>echo off<br/>cls<br/>setlocal enabledelayedexpansion<br/><br/>for /<a href="https://interviewquestions.tuteehub.com/tag/f-236701" style="font-weight:bold;" target="_blank" title="Click to know more about F">F</a> "delims=*" %%1 in (trial.txt) do (<br/> set var1=%%~dp1<br/> set var2=%%~n1<br/>)<br/>echo.&echo.&echo.&echo.<br/>echo Example of output is: Var1=%var1% Var2=%var2%<br/><br/> Quote from: Dusty on February 24, 2010, 03:05:09 PM</p><blockquote><br/> Code: <a>[Select]</a> set var2=%%~n1<br/><br/></blockquote> <br/>%%~nx1 maybe?<br/> Quote from: S.Trout<blockquote>%%~nx1 maybe?</blockquote> <br/>Yes, maybe and perhaps, but the OP asked Quote from: OP<blockquote>how do I retrieve <strong>everything up to the last backslash</strong>, and the <strong>filename</strong> into separate variables?... I want VAR1 have the path<br/>and want VAR2 have the <strong>filename</strong></blockquote> so that's what was provided. <br/><br/>If the filename.extension was wanted as Var2 I'm sure the OP would have writ that in his/her specifications seeing as how he/she lists his/her experience level as Expert and as an expert would know how that a spec has to be correct. But then I'm just a Beginner and so am probably wrong - again.<br/><br/>Mein English readinks tells me zat der extenshun ist nicht part of der filename.<br/><br/>Thanks for dropping in DDV.<br/><br/>a filename without the extension is called the basename. I imagine they meant they wanted the filename (basename + extension) Code: <a>[Select]</a>Set objFS = CreateObject("Scripting.FileSystemObject")<br/>Set objArgs = WScript.Arguments<br/>strFile = objArgs(0)<br/>Set objFile = objFS.OpenTextFile(strFile)<br/>Do Until objFile.AtEndOfLine<br/> strLine=objFile.ReadLine<br/> s = Split(strLine,"\")<br/> filename=s(UBound(s))<br/> s(UBound(s)) = "" <br/> WScript.Echo filename & " "& Join(s,"\")<br/>Loop<br/> Quote from: et_phonehome_2 on February 24, 2010, 10:43:42 AM<blockquote>"So I want VAR1 have the path<br/>And want VAR2 have the filename"<br/></blockquote> <br/>( see dusty, reply 1 this thread. )<br/><br/>C:\>type dusty.bat<br/><br/> Code: <a>[Select]</a>echo off<br/>rem cls<br/>setlocal enabledelayedexpansion<br/><br/>for /f "delims=*" %%1 in (trial.txt) do (<br/> set var1=%%~dp1<br/> set var2=%%~n1<br/>)<br/>echo.&echo.&echo.&echo.<br/>echo Example of output is: Var1=%var1% Var2=%var2%<br/><br/>C:\>type trial.txt<br/><br/>C:\WINDOWS\system32\rstrui.exe<br/>C:\><br/><br/><strong>Output:</strong><br/>C:\>dusty.bat<br/><br/> Example of output is: <strong>Var1=C:\WINDOWS\system32\ Var2=rstrui</strong><br/><br/>C:\> Quote from: et_phonehome_2 on February 24, 2010, 10:43:42 AM<blockquote>"So I want VAR1 have the path<br/>And want VAR2 have the filename."<br/></blockquote> <br/><br/>C:\batch>type dusty.bat<br/> Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/><br/>for /f %%1 in (trial.txt) do (<br/> set var1=%%~dp1<br/> set var2=%%~nx1<br/>echo.&echo.&echo.&echo.<br/>echo Var1=!var1! Var2=!var2!)<br/><strong>Input:</strong><br/>C:\batch>type trial.txt<br/><br/>C:\WINDOWS\system32\rstrui.exe<br/>C:\WINDOWS\system32\notepad.exe<br/>C:\batch><br/><br/><strong>Output:</strong><br/>C:\batch>dusty.bat<br/><br/>Var1=C:\WINDOWS\system32\ Var2=rstrui.exe<br/>Var1=C:\WINDOWS\system32\ Var2=notepad.exe<br/><br/>C:\batch> Quote from: Dusty on February 25, 2010, 01:28:14 AM<blockquote>Yes, maybe and perhaps, but the OP asked so that's what was provided. <br/><br/>If the filename.extension was wanted as Var2 I'm sure the OP would have writ that in his/her specifications seeing as how he/she lists his/her experience level as Expert and as an expert would know how that a spec has to be correct. But then I'm just a Beginner and so am probably wrong - again.<br/><br/>Mein English readinks tells me zat der extenshun ist nicht part of der filename.<br/><br/>Thanks for dropping in DDV.<br/><br/><br/></blockquote> <br/>Technically, the extension is not part of the filename, but most people seem to think of them as one thing. Also, the OP mentioned he wanted to split the full path + name string into 2 <a href="https://interviewquestions.tuteehub.com/tag/parts-239415" style="font-weight:bold;" target="_blank" title="Click to know more about PARTS">PARTS</a>, not three, so I assumed he wanted the extension kept.<br/><br/> Quote<blockquote>Thanks for dropping in DDV.</blockquote> <br/>Is that intended to be sarcasm?<br/><br/> Quote from: Dusty on February 25, 2010, 01:28:14 AM<blockquote><br/>"Mein English readinks tells me zat der extenshun ist nicht part of der filename."<br/></blockquote> <br/><br/>"Mein Englisch erzählt mir, dass der extention nicht Teil vom Dateinamen ist"<br/><br/><a href="http://www.freetranslation.com/">http://www.freetranslation.com/</a><br/> Quote from: BC_Programmer on February 25, 2010, 01:54:29 AM<blockquote>a filename without the extension is called the basename. I imagine they meant they wanted the filename (basename + extension)<br/></blockquote> <br/>BC_P thank you. In For/? MS states that:<br/> Quote<blockquote><br/> %~nI - expands %I to a <strong>file name</strong> only<br/> <br/></blockquote> not a basename so it seems that file name and extension are to be treated as separate entities. The var is expanded without an extension. <br/><br/> Quote from: Salmon Trout on February 25, 2010, 11:57:07 AM<blockquote>Is that intended to be sarcasm?<br/></blockquote> <br/>Most certainly not.<br/><br/>On re-reading the OP's post I realize that the request was for a solution "in DOS only" although the DOS version was not mentioned and I gave a possible solution based on the XP/NT command shell so probably got that wrong. Apologies to the OP.<br/><br/><br/>In my opinion, <em>basename.ext</em> is a filename, despite what the FOR help says. <br/><br/> Quote<blockquote>The exact definition, giving the criteria for deciding what part of the file name is its extension, belongs to the rules of the specific filesystem used; usually the extension is the substring which follows the last occurrence, if any, of the dot character. </blockquote> <br/><a href="https://en.wikipedia.org/wiki/Filename_extension">http://en.wikipedia.org/wiki/Filename_extension</a><br/><br/> Quote<blockquote>Many operating systems, including MS-DOS, <a href="https://interviewquestions.tuteehub.com/tag/microsoft-12847" style="font-weight:bold;" target="_blank" title="Click to know more about MICROSOFT">MICROSOFT</a> Windows, and VMS systems, allow a filename extension that consists of one or more characters following the last period in the filename, thus dividing the filename into two parts: the basename (the <a href="https://interviewquestions.tuteehub.com/tag/primary-1165403" style="font-weight:bold;" target="_blank" title="Click to know more about PRIMARY">PRIMARY</a> filename) and the extension (usually indicating the file type associated with a certain file format). On these systems the extension is considered part of the filename, and on systems which allow (for example) an eight character basename followed by a three character extension, a filename with an extension of "" or " " (nothing, or three spaces) will still be 11 characters long (since the "." is supplied, but not considered as part of the name, by the OS). On Unix-like systems, files will often have an extension (for example prog.c, denoting the C-language source code of a program called "prog"); but since the extension is not considered a separate part of the filename, a file on a Unix system which allows 14-character filenames, and with a filename which uses "." as an "extension separator" or "delimiter", could possibly have a filename such as a.longxtension</blockquote> <br/><a href="https://en.wikipedia.org/wiki/Filename">http://en.wikipedia.org/wiki/Filename</a><br/><br/><br/><br/><br/>How do we split hairs? Quote from: Dusty on February 25, 2010, 01:27:11 PM<blockquote>On re-reading the OP's post I realize that the request was for a solution "in DOS only" although the DOS version was not mentioned and I gave a possible solution based on the XP/NT command shell so probably got that wrong. Apologies to the OP.</blockquote> <br/>This looks distinctly un DOS-like<br/><br/> Quote<blockquote>\\UBIX\INT\EXT\ioweyou_bal_02242010.csv</blockquote> <br/>So I expect the OP has <a href="https://interviewquestions.tuteehub.com/tag/made-1082584" style="font-weight:bold;" target="_blank" title="Click to know more about MADE">MADE</a> the tiresomely frequent error of calling Windows NT family command script language "DOS".<br/> Quote from: greg on February 25, 2010, 01:47:08 PM<blockquote>How do we split hairs?<br/></blockquote> <br/>With Occam's razor.<br/></body></html> | |