View unanswered posts | View active topics It is currently 17 Apr 2014 15:50



Post new topic Reply to topic  [ 5 posts ] 
Split directory name & filename from a string 
Author Message

Joined: 29 Jan 2009 12:46
Posts: 1
Post Split directory name & filename from a string
Wondering if there is any way to split directory name & filename from a string that contains a complete path of file including filename.


29 Jan 2009 12:56
Profile
Expert

Joined: 18 Feb 2006 19:54
Posts: 235
Post 
santoshsri,

Use a FOR loop like this:
Code:
@echo off
Set filename=C:\Documents and Settings\All Users\Desktop\Dostips.cmd
For %%A in ("%filename%") do (
    Set Folder=%%~dpA
    Set Name=%%~nxA
)
echo.Folder is: %Folder%
echo.Name is: %Name%

Output will be:
Quote:
Folder is: C:\Documents and Settings\All Users\Desktop\
Name is: Dostips.cmd

See also the help for the FOR command:
http://www.dostips.com/DosCommandIndex.htm#FOR
DosItHelp? :wink:


03 Feb 2009 22:08
Profile
Expert

Joined: 04 Feb 2009 10:03
Posts: 80
Post 
Along those same lines... what is the easiest way to deal with looping through a directory set that includes spaces (ie "Documents and Settings")?

I have a situation where I need to find specific files (wildcard search) on a drive to do a secure delete on these specific files. I know I can do this with a "Dir /s/b", but if the files are included with a directory that has spaces, there are identification situations that are hosed due to the spaces.

Currently, I am looking at:

Code:
   
   Set LookUpSet=Dir /b/s File1* File2*
   For /F %%F In ('%LookUpSet%') DO (

       Echo;
       Echo Path:   %%~fF
       Echo File:   %%~nxF
       Echo;

       Pause

   )


When I get to a File1* entry that happens to be in "Documents and Settings" the reply is:

Code:
Path:   C:\Documents
File:   Documents


Thanks for your help!

-Rick


04 Feb 2009 10:37
Profile
Expert

Joined: 18 Feb 2006 19:54
Posts: 235
Post 
RElliott63,
FOR /F uses spaces as default delimiter. Empty the delimiter set using FOR /F "delim=", i.e.:
Code:
Set LookUpSet=Dir /b/s File1* File2*
For /F "delim=" %%F In ('%LookUpSet%') DO (
       Echo;
       Echo Path:   %%~fF
       Echo File:   %%~nxF
       Echo;
       Pause
)

Let us know if it works for you.


04 Feb 2009 22:24
Profile
Expert

Joined: 04 Feb 2009 10:03
Posts: 80
Post 
Thanks for the advice. After playing around with it yesterday afternoon, I ended up with the following:

Code:
 
 Set LookUpSet=Dir /b/s File1* File2 File3*
   
 For /F "Delims=" %%F In ('%LookUpSet%') DO (

     Set "f=%%~nxF"
     Set "p=%%~pF"

     CD /D "!p!"
       
     Call :LogMsg "Flagged for Removal"
     Call :RemoveFile
     Call :LogMsg "Removed"

 )


Code:
 
:: File Removal
 :RemoveFile

 Echo --Changing Directory to:  !p!
 CD /D !p!
 
    Echo --Removing File:  !f!

    Call :LogMsg "Removing !f! from Folder..."
    SDelete -p 5 !f!

 Goto:EOF


Thanks for the help!

-R


05 Feb 2009 09:11
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.