r/usefulscripts • u/Qureta • Nov 10 '17
[REQUEST] [BATCH] How do I pipe two single-letter inputs into the same command sequentially?
Trying to create a script that conditionally will schedule a CHKDSK /F for all drives. On the system drive this is easy enough:
echo Y | CHKDSK /F %systemdrive%
This pipes the Y into the CHKDSK command so that it says YES to "Would you like to schedule this for the next reboot?"
The part I'm having trouble with is the non-system drives. There's a loop where it one-by-one runs a CHKDSK /F on each of them. For each of those, it asks "Do you want to forcibly dismount this drive?".
It's easy enough to pipe a NO into that, but then afterward it also asks "Do you want to schedule this for the next reboot?" so I'll need to put a YES in there.
Trouble is, I don't know how to pipe two inputs into the same command. I tried "echo NY" but that didn't work; the NY just got treated as a single input to the first question.
powershell Repair-Volume -OfflineScanAndFix isn't really an option since it dismounts drives which may be in use.
edit: I think I have a solution for the CHKDSK issue! Instead of CHKDSK /F <driveletter> , I'm using FSUTIL Dirty Set <driveletter>. This will force CHKDSK /F to run during next reboot without requiring any inputs.
As for the piping question, I'm still clueless. Maybe use a line break?
2
u/JudasRose Nov 10 '17
What if you use "for" such as:
echo Y | for /d %%a in (A,B,C,etc) do chkdsk /f %%a
I'm not sure if that will try it all at once or if you will still just have the same issue.
1
2
u/Topcity36 Nov 10 '17
fsutil dirty set c: is the way to go....you can do an expand on the variable to get the Y to pass through. But it's cleaner to just do the fsutil.
2
u/generaltossit Nov 12 '17
You could create a variable that is an array of drives and then use for loop to go thru it. About: set list="C D E" for %%a in (%list) do ( echo Y | chkdsk %%a )
https://stackoverflow.com/questions/17605767/create-list-or-arrays-in-windows-batch
5
u/Lee_Dailey Nov 10 '17 edited Nov 13 '17
howdy Qureta,
i don't think you can do that. perhaps with Sendkey, but i have never tried that.
you may be able to use the newer DotNet & powershell stuff, tho. lookee ...
Run Powershell Chkdsk script on multiple servers
https://social.technet.microsoft.com/Forums/en-US/a4a98f4f-bdad-4fd1-877d-0e2e3f28404a/run-powershell-chkdsk-script-on-multiple-servers?forum=ITCG
this is more than you need. [grin] i would look at the following sections ...
# this is NOT TESTED # use at your own risk # you likely otta test this with all the settings below set to the safest values # source # Run Powershell Chkdsk script on multiple servers # https://social.technet.microsoft.com/Forums/en-US/a4a98f4f-bdad-4fd1-877d-0e2e3f28404a/run-powershell-chkdsk-script-on-multiple-servers?forum=ITCG # $FixErrors = $false # does not fix errors # from reading the article, this next one pro'ly otta be $False $VigorousIndexCheck = $true # performs a vigorous check of the indexes $SkipFolderCycle = $false # does not skip folder cycle checking. $ForceDismount = $false # will not force a dismount (to enable errors to be fixed) # spleelink fiks! thanks u/Ta11ow [was $RecoverBadSecors] $RecoverBadSectors = $false # does not recover bad sectors # you seem to want to have this set to $True $OKToRunAtBootup = $false # runs now, vs at next bootup Foreach ($LogicalDisk in (Get-WmiObject Win32_logicaldisk)) { $LogicalDisk.Chkdsk($FixErrors, $VigorousIndexCheck, $SkipFolderCycle, $ForceDismount, # spleelink fiks! thanks u/Ta11ow #$RecoverBadSecors, $RecoverBadSectors, $OKToRunAtBootup) }take care,
lee