Solved

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

Posted on 2013-01-28
6
2,913 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

685 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