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?
views:
776answers:
2
+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
2008-11-11 21:29:10
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
2008-11-11 21:40:12
This is great, is there something similar in php?
2008-11-11 21:40:49
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
2008-11-11 21:49:02
+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
2008-11-11 21:43:32
+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
2008-11-11 22:10:05