Solved

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

Posted on 2013-01-28
6
2,725 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
  • 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 95

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of the features I've come to appreciate about Windows 7 and Windows Server 2008 R2 is the ability to pin applications to the task bar. As useful a feature as I've found this, it does have some quirks.  For example, have you ever tried pinning an…
Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
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). …
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now