tags:

views:

27

answers:

2

I found this question, which is very similar to what I want but I just couldn't get it to work.

I want to run process.exe on each subdirectory's XML directory of C:\ToProcess. If I did it by hand, the first 6 of the 50 or so commands would look like this:

process.exe -switch1 -switch2 -i "C:\ToProcess\abx\XML" -o "C:\Processed\abx\XML"
process.exe -switch1 -switch2 -i "C:\ToProcess\dez\XML" -o "C:\Processed\dez\XML"
process.exe -switch1 -switch2 -i "C:\ToProcess\ghm\XML" -o "C:\Processed\ghm\XML"
process.exe -switch1 -switch2 -i "C:\ToProcess\jkq\XML" -o "C:\Processed\jkq\XML"
process.exe -switch1 -switch2 -i "C:\ToProcess\mn0\XML" -o "C:\Processed\mn0\XML"
process.exe -switch1 -switch2 -i "C:\ToProcess\pq2\XML" -o "C:\Processed\pq2\XML"

But before running those commands, I would have to do this, because the target folders do not yet exist:

md "C:\Processed\abx"
md "C:\Processed\dez"
md "C:\Processed\ghm"
md "C:\Processed\jkq"
md "C:\Processed\mn0"
md "C:\Processed\pq2"
md "C:\Processed\abx\XML"
md "C:\Processed\dez\XML"
md "C:\Processed\ghm\XML"
md "C:\Processed\jkq\XML"
md "C:\Processed\mn0\XML"
md "C:\Processed\pq2\XML"

So, is there a way to do all this in just a couple commands?

+1  A: 

Does it need to be a single line? If you can do this in a powershell script, then you could do something like the following:

foreach ($path in (get-childitem -recurse | foreach-object -process { if ($_.FullName.EndsWith("\xml")) {$_.FullName} }))
{
    $newpath = $path.Substring(0,$path.LastIndexOf("\xml"));
    $newpath = $newpath.Substring($newPath.LastIndexOf("\"));
    $newpath = "c:\processed" + $newpath + "\xml";
    [IO.Directory]::CreateDirectory($newpath);
}

And of course you'll need to then execute your process.exe in that foreach loop.

nithins
No - sorry. Having it be a single line is a silly requirement. I removed that. A script would be fine. I will try this.
Chris
+2  A: 

This should work. It will create each of the destination directories if they don't exist and then run process.exe on each.

Get-ChildItem C:\ToProcess\*\XML | ForEach-Object { 
    $newPath = $_.FullName.Replace("ToProcess","Processed"); 
    New-Item $newPath -ItemType Directory  -ErrorAction SilentlyContinue;   
    .\process.exe -switch1 -switch2 -i $_.FullName -o $newPath;
}

Update: Added .\ before process following comment

Matthew Manela
process.exe needed to be .\process.exe and it worked!
Chris