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
Solved

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

Posted on 2013-01-28
6
2,864 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

808 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