Solved

DiskPart run from batch script won't pipe to file in Windows 7 x64

Posted on 2013-01-28
6
2,942 Views
Last Modified: 2013-01-28
Can someone please tell me why the following code only works when run from the command prompt in Windows 7 but not when run from a batch file. When run from a batch file output.txt is created but is empty.

DiskPart.exe /s listvolumes.txt > output.txt

listvolumes.txt contains just list volume
0
Comment
Question by:zeetec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38826667
Where is the directory it is run from - is your script file in that dir, or is it getting c:\windows\system32 say as path (especially if run from a network share) and not finding it or being able to write to it.

Try:

@echo off
( echo current dir is %cd%
 DiskPart.exe /s c:\specific\dir\listvolumes.txt
) > c:\specific\dir\output.txt

Also you will normally need to "run as administrator" for this to work afaik.

Steve
0
 

Author Comment

by:zeetec
ID: 38826726
I am running the batch file from C:\DiskPart by selecting "run as administrator" and even if I use the following in the script

DiskPart.exe /s C:\DiskPart\listvolumes.txt > C:\DiskPart\output.txt

and run the batch file from the same location, the result is the same.

I tried your code but it just outputs the current directory to the output.txt file and nothing else.
0
 
LVL 96

Accepted Solution

by:
Lee W, MVP earned 500 total points
ID: 38826731
Some commands seem to redirect to standard error - try including standard error to the same location.  (Standard Error is 2>, standard output is 1 and assumed if 1 is not specified)

Explicitly, try this:
DiskPart.exe /s listvolumes.txt >output.txt 2>&1
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zeetec
ID: 38826770
Thank you very much. That was exactly my problem. I was pulling my hair out trying to figure this out.

I think I need to some homework on the difference between running commands from the command prompt versus a batch file as I still don't understand why it worked without the explicit instruction to pipe the standard error when run from the command line.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38826825
Very odd.  Redirection works find for me to the file with our without stderr redirection.

You could check if you wish:


DiskPart.exe /s listvolumes.txt >output.txt 2>errors.txt

and see where it ends up.

The only thing that led to 0 byte files was not running the command prompt as admin, if a UAC prompt came up, even if Yes answered then no output file.

Showing in same command prompt and running .cmd file worked the same.

Glad you got it working, just curious whether it is actually sending to stderr for you somehow?!

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38826835
Oh and I notice looking back you only need one command.  In which just keep it all in the batch without the external script file:

echo LIST VOLUME | diskpart.exe > output.txt

Steve
0

Featured Post

Scamming the Scammers!

Have you ever heard of Scam Baiting?
It's a highly entertaining sport that you can participate in.
Introduction to beating scammers at their own game and how you can help
Share your thoughts, ideas and experiences on the topic.
Links to top Anti-Scam resources provided.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question