tags:

views:

114

answers:

2

I need to know how to extract directory information from user inputted file, consider this code as example:


ECHO Drag and drop your .txt file here, after that press Enter:
SET txtfile=
SET /P txtfile=
ECHO.
CD %txtfile%
ofcourse that didn't work since i didn't extract filepath from %txtfile% and here the sample output i want:
C:\>Drag and drop your .txt file here, after that press Enter:
C:\somefolder\somesubfolder\somefile.txt
C:\>Press Enter to continue...

C:\somefolder\somesubfolder\>

notice it have change it working directory

+1  A: 

You can extract the full path as follows:

@echo off
setlocal
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.
for %%i in (%txtfile%) do set txtdir=%%~dpi
for %%i in (%txtfile%) do set txtfil=%%~nxi
cd /d %txtdir%
dir %txtfil%
endlocal

The first for statement gets the drive and path, the second gets the filename and extension. I've used cd /d to change the drive and directory and just used setlocal/endlocal to preserve my path outside the script (you can remove these if you don't care).

The full range of ~-modifiers can be found by running "for /?" in a command window. It really is a powerful command, and these modifiers aren't restricted to "for", they can be used on any %1-type arguments to scripts as well.

paxdiablo
great work :D thanks
Dels
+1  A: 
ECHO Drag and drop your .txt file here, after that press Enter:
SET txtfile=
SET /P txtfile=
ECHO.
CD %txtfile%\..

I don't really know why, but this works in XP, could work in NT also.

Stanislav Kniazev
It works because the brain-dead cd command in XP does simple text substitutions to remove "." and ".." entries. Who would have thunk Microsoft's mistakes would one day prove useful?
paxdiablo
work in XP thanks, dunno about NT
Dels