1.

Solve : Data Manipulation?

Answer»

I need this in DOS only:

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?

\\UBIX\RPTOUT\EXT\file001.txt
\\UBIX\FIN\payments.csv
\\UBIX\INT\EXT\ioweyou_bal_02242010.csv
\\UBIX\commisiom_sum_YTD_20100224.pdf


so I want VAR1 have the path
and want VAR2 have the filenameHere's one method:  (where Trial.txt is the file containing input data)

Code: [Select]echo off
cls
setlocal enabledelayedexpansion

for /F "delims=*" %%1 in (trial.txt) do (
    set var1=%%~dp1
    set var2=%%~n1
)
echo.&echo.&echo.&echo.
echo    Example of output is:  Var1=%var1%  Var2=%var2%

Quote from: Dusty on February 24, 2010, 03:05:09 PM


Code: [Select]    set var2=%%~n1


%%~nx1 maybe?
Quote from: S.Trout
%%~nx1 maybe?

Yes, maybe and perhaps, but the OP asked Quote from: OP
how do I retrieve everything up to the last backslash, and the filename into separate variables?...   I want VAR1 have the path
and want VAR2 have the filename
so that's what was provided.   

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.

Mein English readinks tells me zat der extenshun ist nicht part of der filename.

Thanks for dropping in DDV.

a filename without the extension is called the basename. I imagine they meant they wanted the filename (basename + extension) Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
strLine=objFile.ReadLine
s = Split(strLine,"\")
filename=s(UBound(s))
s(UBound(s)) = ""
WScript.Echo filename & " "& Join(s,"\")
Loop
Quote from: et_phonehome_2 on February 24, 2010, 10:43:42 AM
"So I want VAR1 have the path
And want VAR2 have the filename"

( see dusty, reply 1 this thread. )

C:\>type  dusty.bat

Code: [Select]echo off
rem cls
setlocal enabledelayedexpansion

for /f "delims=*" %%1 in (trial.txt) do (
    set var1=%%~dp1
    set var2=%%~n1
)
echo.&echo.&echo.&echo.
echo    Example of output is:  Var1=%var1%  Var2=%var2%

C:\>type trial.txt

C:\WINDOWS\system32\rstrui.exe
C:\>

Output:
C:\>dusty.bat

   Example of output is:  Var1=C:\WINDOWS\system32\  Var2=rstrui

C:\> Quote from: et_phonehome_2 on February 24, 2010, 10:43:42 AM
"So I want VAR1 have the path
And want VAR2 have the filename."


C:\batch>type dusty.bat
Code: [Select]echo off
setlocal enabledelayedexpansion

for /f  %%1 in (trial.txt) do (
    set var1=%%~dp1
    set var2=%%~nx1
echo.&echo.&echo.&echo.
echo Var1=!var1!  Var2=!var2!)
Input:
C:\batch>type trial.txt

C:\WINDOWS\system32\rstrui.exe
C:\WINDOWS\system32\notepad.exe
C:\batch>

Output:
C:\batch>dusty.bat

Var1=C:\WINDOWS\system32\  Var2=rstrui.exe
Var1=C:\WINDOWS\system32\  Var2=notepad.exe

C:\batch> Quote from: Dusty on February 25, 2010, 01:28:14 AM
Yes, maybe and perhaps, but the OP asked  so that's what was provided.   

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.

Mein English readinks tells me zat der extenshun ist nicht part of der filename.

Thanks for dropping in DDV.



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 PARTS, not three, so I assumed he wanted the extension kept.

Quote
Thanks for dropping in DDV.

Is that intended to be sarcasm?

Quote from: Dusty on February 25, 2010, 01:28:14 AM

"Mein English readinks tells me zat der extenshun ist nicht part of der filename."


"Mein Englisch erzählt mir, dass der extention nicht Teil vom Dateinamen ist"

http://www.freetranslation.com/
Quote from: BC_Programmer on February 25, 2010, 01:54:29 AM
a filename without the extension is called the basename. I imagine they meant they wanted the filename (basename + extension)

BC_P thank you.  In For/? MS states that:
Quote

    %~nI        - expands %I to a file name only
   
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.

Quote from: Salmon Trout on February 25, 2010, 11:57:07 AM
Is that intended to be sarcasm?
 
Most certainly not.

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.


In my opinion, basename.ext is a filename, despite what the FOR help says.

Quote
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.

http://en.wikipedia.org/wiki/Filename_extension

Quote
Many operating systems, including MS-DOS, MICROSOFT 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 PRIMARY 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

http://en.wikipedia.org/wiki/Filename




How do we split hairs? Quote from: Dusty on February 25, 2010, 01:27:11 PM
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.

This looks distinctly un DOS-like

Quote
\\UBIX\INT\EXT\ioweyou_bal_02242010.csv

So I expect the OP has MADE the tiresomely frequent error of calling Windows NT family command script language "DOS".
Quote from: greg on February 25, 2010, 01:47:08 PM
How do we split hairs?

With Occam's razor.


Discussion

No Comment Found