views:

776

answers:

2

I'm creating a batch file to make multiple directories from a list in a text file however after the directory is listed sometimes a filename is as well. Is there an easy way to have it ignore all data after the last \ on a line?

+4  A: 

I might suggest that DOS batch isn't the right tool for this job, because it doesn't have built-in facilities for string manipulation like this would need.

If you have Perl available, you can do something like this:

#!/usr/bin/perl -w

while (<>) {
    s/\\[^\\]*$//; # this removes a the last backslash and anything after it
    mkdir $_;
}
Greg Hewgill
This post's example is clearly set up for a *nix environment, so it might be worth noting that you can get Perl for Windows and it's very easy to set up. Just go to the perl site.
rmeador
This is great, is there something similar in php?
Yes, you can do similar things in PHP but since I don't really know PHP I wouldn't be able to give a good example.
Greg Hewgill
+4  A: 

You can use something like this:

@echo off
set filename="c:\temp\my files\file.txt"
for /f "tokens=*" %i in ("%filename%") do set filename="%~dpi"
echo %filename%

The result will be "c:\temp\my files\".

Wimmel
+1: Answered the question asked!You can find "Help" for the `for' command in "Start|Help", then search for "command-line reference A-Z", click on 'F' and scroll to 'for'The `connamd-line reference' topic is only available through Search or the Index.
Ken Gentle
or type 'for /?' on the commandline
Wimmel