1.

Solve : Manipulating file/folder location in BAT files?

Answer»

All,

I hope this isn't a full-on noob question, but here goes.

I use BAT files in my "SendTo" right-click MENU to process files on our server. This LOCATION is stored in the %1 value as (example) X:\folder1\folder2\longfoldername3\file.ext. I WANT to separate the folder location of the file and the filename itself. How can I grab X:\folder1\folder2\longfoldername3 as one variable and file.ext as another variable?

THANK you for any help.

Sincerely,
Cj.Quote from: cjplay on August 28, 2007, 09:48:55 AM

This location is stored in the %1 value as (example) X:\folder1\folder2\longfoldername3\file.ext. I want to separate the folder location of the file and the filename itself. How can I grab X:\folder1\folder2\longfoldername3 as one variable and file.ext as another variable?

See FOR /?

If %1 is a filename, whether just the filename e.g. afile.ext or a path and filename eg d:\folder\subfolder\afile.ext then...

%~1 - expands %1 removing any surrounding quotes (")
%~f1 - expands %1 to a fully qualified path name
%~d1 - expands %1 to a drive letter only
%~p1 - expands %1 to a path only
%~n1 - expands %1 to a file name only
%~x1 - expands %1 to a file extension only

%~s1 - expanded path contains short names only
%~a1 - expands %1 to file attributes of file

%~t1 - expands %1 to date/time of file
%~z1 - expands %1 to size of file

These can be combined...

N.B. The file need not exist, an imaginary filename is still processed just the same, although items impossible to resolve, such as size, date/time, etc expand to blanks.

This is a folder...

Quote
S:\Backup\Test\stringlen>dir
Volume in drive S is USB_HDD
Volume Serial Number is 6C1A-995E

Directory of S:\Backup\Test\stringlen

28/08/2007 18:13 <DIR> ..
28/08/2007 18:13 <DIR> .
16/08/2007 21:38 476 sfor.cmd
16/08/2007 21:24 436 slen101.cmd
16/08/2007 21:34 43 slenFB.bas
16/08/2007 21:34 16,384 slenFB.exe
16/08/2007 21:34 16,384 stringlength.exe
28/08/2007 18:15 49 filestuff.bat
6 File(s) 33,772 bytes
2 Dir(s) 277,761,826,816 bytes free


This is filestuff.bat

Quote
@echo off
REM this is filestuff.bat
echo parameter passed %%1 is %1
echo remove quotes if needed %%~1 is %~1
echo fully qualified path %%~f1 is %~f1
echo file name %%~n1 is %~n1
echo extension %%~x1 is %~x1
echo drive letter and colon %%~d1 is %~d1
echo path only %%~p1 is %~p1
echo.
echo you want...
echo.
echo drive letter, path %%~dp1 is %~dp1
echo filename and extension %%~nx1 is %~nx1

This is the output of the command:-

filestuff "sfor.cmd" ...

Quote
parameter passed %1 is "sfor.cmd"
remove quotes if needed %~1 is sfor.cmd
fully qualified path %~f1 is S:\Backup\Test\stringlen\sfor.cmd
file name %~n1 is sfor
extension %~x1 is .cmd
drive letter and colon %~d1 is S:
path only %~p1 is \Backup\Test\stringlen\

you want...

drive letter, path %~dp1 is S:\Backup\Test\stringlen\
filename and extension %~nx1 is sfor.cmd


Holy crap thank you...


Discussion

No Comment Found