schedule chkdsk on all drives output results to file - windows

I wish to run a schedule chkdsk on all drives from a bat file via my task scheduler, that will force an automatic reboot where necessary and output the results for each drive to a text file.
I've been following an example listed here http://www.sharkyforums.com/showthread.php?302556-Automating-CHKDSK-F-R-on-all-Drives that looks to do what I need it to but it doesn't seem to be working.
in the DriveLtr.txt file I have my drives listed as
C:
D:
E:
Can anyone suggest a possible fix to get this up and running?
Many thanks

This is a barebones version (slightly modified) of the script in the link you posted.
for /f %%a in (DriveLtr.txt) do (
echo Y| CHKDSK %%a /F /R >>CheckDiskRpt.txt
)
You can add all the extra logging stuff you want to it, but that will loop over all the drives in the text file and run the chkdsk command for each of them, logging stdout to CheckDiskRpt.txt.

#ECHO OFF
REM This is a script which will run a CHKDSK against drives specified between ()
FOR %%X IN (C,D,E,F,G,H,I,J,...,Z) DO (CHKDSK %%X: > "C:\CHKDSK_LOGS\DRIVE_%%X_%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt")

Related

Well-tried and tested Windows batch file stops at for loop since today - what could be the cause? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 18 days ago.
Improve this question
I have written and used a windows batch file that has been working without a problem for quite a while.
This is what it does:
The program loops through the *.pdf files of the current folder, and then performs a few tasks like extracting the text layer of the pdf file, searching for a couple of search terms and in the end it renames the pdf file according to which search term was found.
The batch file worked fine until today.
Now it just stops working at the for loop and I have no idea why.
Stripped down to the essence, the batch looks like this:
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d /o-d *.pdf') do (
echo %%i
echo %%~ti
echo %%~ni
)
The echos would just be examples, but as the batch files never reaches what's after the for loop, I simplified it.
Since today, the program would just stop when it reaches the for loop.
This is what I tried to solve the problem:
examined the syntax of the for loop, found no mistakes
tried simplified variations of the for /f loop including leaving out the delims option, changing it against a usebackq option, changing the name of the loop variable, stripping down the options of the dir command, changing the file filter to *.txt
turned the for loop into a one-liner with just an echo %%i command etc.
looped through the lines of a given text file
changed the location of the batch file to another folder
tried the batch on a Windows 10 and a Windows 7 system
try again after rebooting the system
checked folder access rights, I had all of them, no restrictions
None of these helped, the batch file would always stop and close its window right at the for loop.
I also inserted the dir command before the for loop just to check the syntax - this worked with no problem, so it can't be the syntax of the dir command either.
I remember that I had this problem a few times years ago and it either disappeared by itself or I had to retype the batch word for word in a new text file copying whatever was in the original file as copy and paste wouldn't work.
Has anyone out there observed this strange behaviour and maybe found the cause and at best a solution for it? I'd be so thrilled.
EDIT: When I run the batch in the cmd window, I get a simple SYNTAX ERROR when it reaches the line with the for loop.
Try this at the command prompt:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun
reg query "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun
Both results should be empty, but probably in your case they are set.
The problem of the AutoRun file, it's started when a new cmd instance is started, but also for the FOR/F command.
If this is the problem, you should delete these entries by
reg DELETE "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /f
reg DELETE "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun /f
Or if you really need an AutoRun batch, then it should start with a guard.
#echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
set "_ccl_=!cmdcmdline!"
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** Do your stuff ***
)
endlocal
exit /b

Windows Batch File for loop on user dir

I need to create a batchfile to delete a specific file in the users appdata dir. The batchfile is executed as localsystem.
I did test the following senarios:
Command Prompt as localsystem - this works
Command Prompt as admin - this works
Command Prompt as user - this works for all folders where the user has permissions
As soon as i put the for loop in a batch file, it dosent work anymore.
It also dosent matter in which context (User,admin,localsystem) i start it.
for /D %i in ("C:\Users\*") do del /Q /F "%i\AppData\LocalLow\Sun\Java\Deployment\deployment.properties"
The exit code shows 255, which means to long error.
The error i get is the following:
C:\Windows\Scripts>sync_exeptionsiteslistandconfig_java.bat > output.txt
\Users\*") do del "i\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" /Q /F was unexpected at this time.
Any help why the single line is working and a batchfile with this single line is not working is welcome.
Please also explain why it is not working. (I always run tasks as localsystem)
This may do what you want, but it only processes normal folders, not hidden etc.
Your problem may have been using %i within a batch script.
#echo off
for /D %%i in ("C:\Users\*") do del /Q /F "%%i\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" 2>nul

Batch File Iterating through files on a local network server

I'm trying to open various files stored on a local network server with this piece of batch code:
for /f "tokens=*" %%G in ('dir /b /a:d "\\server\directory\*"') do %%G\setup.xml
but since dir \\server\directory doesn't work so won't that piece of code.
How can I accomplish that?
Thank you in advance
You can map a drive using
net use X: \\server\directory
and then you can change to that directory using
pushd X:
You can then run your batch command on the current directory, and when you are finished and the files are no longer in use, you can delete the drive using
net use X: /delete
You write:
since dir \server\directory doesn't work
At first I was going to say that does work. But what actually works is:
dir \\server\sharename
The server has to share the directory. On the server you can use a command like:
net share public=c:\public
The you should be able to:
dir \\server\sharename

Running Multiple Installations with System-Reboot Inbetween them

Currently I have a set of software-Installations(and their paths) that i have to install on my Windows Machine.
What I do now is -- Hit RUN every time and type in the software installation path into it..
What I want is to design a Batch file which would install all the applications and REBOOT my system after every successful installation and then continue with the NEXT item in the list..
Is it possible using a .bat file ??
This really isn't something batch was designed for, so this will be a bit hacky. It's not elegant by any means but give it a shot, it might work for you.
for /f %%a in (C:\files.txt) do (
start /wait %%a
exit /b
)
for /f "skip=1" %%b in ("C:\files.txt) do (
echo %%b >>C:\newfiles.txt
)
xcopy C:\newfiles.txt C:\files.txt /y
del C:\newfiles.txt /f /q
shutdown /r /t 0 /f
The idea being that you have a text file with the paths of the executables that you want to install. It will go through and execute the first file in the list, wait for it to complete, then re-write the list without the file it just installed.
This is dependend on the setup file having no user interaction and exiting by itself, or maybe it's just to make things easier - in which case just go through each install yourself, and when it finishes the batch file will do the rest.
On the note of rebooting and continuing you will either need to run the batch file again yourself or put it in the registry to start up itself, the latter command being
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchInstaller" /d "C:\MyBatchFile.bat" /f
Hope this helps

Batch or VB Script to copy logs from multi directories to one directory and rename to avoid conflicts

Hope you can help. I have been trying to resolve this for a week but not getting anywhere and can't quite piece together what I need! - My scripting skills are far from great so please forgive my naivety!
Ok, The Problem......
I have an IIS server that has multiple sites that all save their logs in a separate directory, I need to copy the logs from the last 24 hours to a local directory on my machine so I can analyse these in Log Parser Lizard (GUI Version) on a daily basis.
I can map a drive from the remote server to my local machine via a hardware VPN, so this makes things a bit easier. Using the forfiles command I can re-curse the directories to find the logs that are only a day old, and using either copy/xcopy/ or Robocopy I can set a command to copy. My problem is that the IIS logs all have the same name so my copy command just keeps overwriting the previous file, rather than creating a new file. I have tried using the %random% parameter for the file name, but this again creates one random file that is overwritten with the next file, keeping the same name instead of creating lots of randomly named files in one directory.
I know that Log Parser commands include recurse, which I have used successfully, however the format of the log is changed slightly and the GUI Lizard cannot read the data within, so this is not a solution.
My code as it stands at this time is shown below, with IP's changed for obvious reasons. Any help would be greatly appreciated!
#echo off
NET USE Q: /Delete /yes
NET USE Q: \255.255.255.255\D$\Logs
cd C:
RD /S /Q C:\Weblogs\Production
MD C:\Weblogs\Production
forfiles.exe /p Q:\ /s /m *.log /d 0 /c "cmd /c robocopy /S /XC /XN /XO #file C:\Weblogs\Production\%random%.log"
NET USE Q: /delete
exit
%RANDOM% does not work for you in this case because it does not get solved per each iteration but only once at the forfiles invocation.
You'll need either to use in FORFILES some unique identifier, maybe concatenating #RELPATH and #FNAME may work for you in case you have only one level deep recursion.
Or either replace FORFILES with a FOR loop. Inside the loop you may have more freedom to calculate a unique ID, maybe a simple counter might work for you.
Edit: see this simple code sample, to get you started
#ECHO off
SETLOCAL enabledelayedexpansion
SET destdir=C:\LOGS
SET count=%RANDOM%
FOR /R %%A IN (*.log) DO (
SET /A count += 1
#ECHO COPY %%A !destdir!\%%~nA.!count!.log
)

Resources