# Solved: Batch File Explanation



## computerman29642 (Dec 4, 2007)

I have a batch file that runs the following code:


```
echo y|chkdsk c: /f/r
```
Could someone please explain this code? How does the code know to answer the question with "yes"?


----------



## leroys1000 (Aug 16, 2007)

y=yes.
| pipe to run checkdisk on disk c:
/f=fix
/r=repair


----------



## computerman29642 (Dec 4, 2007)

I understand all of that, but why does the y come before the chkdsk command? I would have thought that the y would come after.


----------



## Squashman (Apr 4, 2003)

Do you see a Y option in the chkdsk help.

```
C:\Users\Squashman>chkdsk /?
Checks a disk and displays a status report.


CHKDSK [volume[[path]filename]]] [/F] [/V] [/R] [/X] [/I] [/C] [/L[:size]] [/B]


  volume          Specifies the drive letter (followed by a colon),
                  mount point, or volume name.
  filename        FAT/FAT32 only: Specifies the files to check for fragmentation
.
  /F              Fixes errors on the disk.
  /V              On FAT/FAT32: Displays the full path and name of every file
                  on the disk.
                  On NTFS: Displays cleanup messages if any.
  /R              Locates bad sectors and recovers readable information
                  (implies /F).
  /L:size         NTFS only:  Changes the log file size to the specified number
                  of kilobytes.  If size is not specified, displays current
                  size.
  /X              Forces the volume to dismount first if necessary.
                  All opened handles to the volume would then be invalid
                  (implies /F).
  /I              NTFS only: Performs a less vigorous check of index entries.
  /C              NTFS only: Skips checking of cycles within the folder
                  structure.
  /B              NTFS only: Re-evaluates bad clusters on the volume
                  (implies /R)

The /I or /C switch reduces the amount of time required to run Chkdsk by
skipping certain checks of the volume.

C:\Users\Squashman>
```
You need to answer the question that comes back from chkdsk so that it can run unatteneded.


----------



## computerman29642 (Dec 4, 2007)

So, the

```
y|
```
 is answering the question produced by the code that follows?

Only options/switches go after the command?


----------



## Squashman (Apr 4, 2003)

run the chkdsk code by itself. What does it do? What does it ask you to do?
_Posted via Mobile Device_


----------



## computerman29642 (Dec 4, 2007)

When I run the chkdsk command it indicates that the active drive cannot be locked, and therefore cannot run. It then ask if the chkdsk command can run the next time the computer is restarted.

A y or n must be entered to answer the question.


I guess my overall goal is to learn why the y| goes before the code and not after. How does the code know the y is meant to answer the question of the chkdsk code if it is done before the questions is ever asked?


----------



## devil_himself (Apr 7, 2007)

Every program has three open "File Handles" by default [stdin -> #0 connected to keyboard, stdout -> #1 | stderr -> #2 connected to terminal | display]

when chkdsk asks a question it does so by printing it on the *stdout* and waiting for an answer on the *stdin*
the pipe ( | ) connects the stdout of one program to the stdin of another ex -
command 1 | command 2 -> this connects the stdout of command 1 to the stdin of command 2

echo y|chkdsk c: /f/r
so the stdout of echo [ echo y ] which is connected to the stdin of chkdsk is used to fulfill the request of yes or no


----------



## computerman29642 (Dec 4, 2007)

Thank you. The explanation helps very much. 

I will read the information provided in the link.


----------



## computerman29642 (Dec 4, 2007)

Thank you Squashman and devil_himself for all the reponses. You both have helped to clear up my confusions. I really appreciate the help from both of you.


----------



## Squashman (Apr 4, 2003)

Most cmd line utilities have the ability to take input from a previous cmd as explained above. So you just use the pipe to pass the output of the previous cmd for the input to the next cmd.

some examples.
find all files that don't have the file extension ZIP, PGP or XLS.

```
Dir /a-d /-c /q /O-S^|Findstr /I /V /E ".zip .pgp .xls
```
This is how I time stamp my log files.

```
Echo exit|cmd /q /k prompt $D $T>>somelogfile.txt
```


----------

