DosTips.com

A Forum all about DOS Batch
It is currently 23 Jul 2016 15:13

All times are UTC-06:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
PostPosted: 29 Jan 2009 12:56 
Offline

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


Top
   
 Post subject:
PostPosted: 03 Feb 2009 22:08 
Offline
Expert

Joined: 18 Feb 2006 19:54
Posts: 235
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:


Top
   
 Post subject:
PostPosted: 04 Feb 2009 10:37 
Offline
Expert

Joined: 04 Feb 2009 10:03
Posts: 80
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


Top
   
 Post subject:
PostPosted: 04 Feb 2009 22:24 
Offline
Expert

Joined: 18 Feb 2006 19:54
Posts: 235
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.


Top
   
 Post subject:
PostPosted: 05 Feb 2009 09:11 
Offline
Expert

Joined: 04 Feb 2009 10:03
Posts: 80
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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Baidu [Spider] and 6 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:  
cron
Powered by phpBB® Forum Software © phpBB Limited