Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3194
  • Last Modified:

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

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
zeetec
Asked:
zeetec
  • 3
  • 2
1 Solution
 
Steve KnightIT ConsultancyCommented:
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
 
zeetecAuthor Commented:
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
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zeetecAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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

Independent Software Vendors: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now