1

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?

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.