Two For Loops Using %1 - Delayed Expansion? - batch-file

My working batch file scans a long list of remote servers, copies anything there to a local server, checks the log file for a keyword, and if the keyword is found sends an email. I noticed it is always sending emails, even with a blank log file.
I discovered both FOR loops are using the %1 variable for their output - as seen in ECHO %1 and each line of the called :servermove. For lack of a better explanation it is not resetting %1 to null between loops.
I reviewed almost a dozen SO posts and am somewhat confident using SETLOCAL ENABLEDELAYEDEXPANSION would resolve this. That is where my understanding ends and I am unsuccessful thus far.
Here is the relevant code:
SET DATE=%date:~4,2%-%date:~7,2%-%date:~10,4%
SET HH=%time:~0,2%
SET MN=%time:~3,2%
SET TSTAMP=Time Run is %HH%%MN%
SET DATETIME=%DATE% at %HH%%MN%
SET LOGFILE="\\nt980a3\CreditFileImagesTransmission\LogFiles\%DATETIME%-File Move Log.txt"
SET MailDst=
SET MailSrc=
SET MailSrcName=Center to LDSD File Mover
SET OKMailSub=A Branch Has Sent You Some Files
ECHO %DATETIME% > %LOGFILE%
ECHO. >> %LOGFILE%
FOR /F "tokens=1" %%A IN (%~dp0SourceServers.txt) DO CALL :ServerMove %%A
:cleanuplogs
PUSHD "\\nt980a3\CreditFileImagesTransmission\LogFiles" &&(
FORFILES /S /M *.txt /D -45 /C "CMD /C DEL /Q #path"
) & POPD
:mailtest
FOR /F "tokens=*" %%A IN (%LOGFILE%) DO CALL :searchlog "%%A"
:searchlog
ECHO %1 | find "\\nt">NUL
IF NOT ERRORLEVEL 1 GOTO successmail
GOTO exit
:successmail
IF EXIST %temp%\to.txt DEL %temp%\to.txt
FOR %%a IN (%MailDst%) DO ECHO %%a>>%temp%\to.txt
"%~dp0sendmail.exe" /TO=%temp%\to.txt /FROM=%MailSrcName% ^<%MailSrc%^> /REF=%OKMailSub% /MESSAGE=%LOGFILE% /HOST=
:exit
EXIT
:ServerMove
DIR /S /B \\%1\CreditFileImagesTransmission\*.* >> %LOGFILE%
XCOPY /E /C /I /Y "\\%1\CreditFileImagesTransmission\*.*" "\\nt980a3\CreditFileImagesTransmission\%DATE%\%HH%%MN%\"
FOR /D %%P IN ("\\%1\CreditFileImagesTransmission\*.*") DO RMDIR "%%P" /Q /S
DEL /Q /S "\\%1\CreditFileImagesTransmission\*.*"
I tried changing :mailtest to use %%B in both instances but that also fails. Placing SETLOCAL ENABLEDELAYEDEXPANSION and its counterpart ENDLOCAL before one or the other loop and changing the %%A to !A! does not work either.
Would someone kindly point out the error in my ways and offer suggestions or resources that will help me resolve this?

%1 is the first parameter provided to the procedure - either from the command-line (in the main procedure) or the parameter following the procedure name in call :procedurename parameter1.
In your case, %1 to :servermove is an entry from SourceServers.txt and %1 to :searchlog is each line from %LOGFILE%.
Since you've censored your batch, what you've posted makes little sense. For instance, the :searchlogs routine will take the first line from %LOGFILE% and go to successmail or cleanlogs depending on whether that first line contains the target string \\nt. What it does from there, we can't tell.
We're faced with an XY problem - trying to fix a solution, not a problem.
First problem: Don't use date as a user-variable. It's a "magic variable" which contains the date but it's overridden by a specific set statement.
Having run :servermove for each entry in SourceServers.txt, you are
- accumulating a directory list from \CreditFileImagesTransmission\*.* on that server.
- copying those files to server nt980a3 with a date/timestamp but not including the source-servername so any duplicate name anywhere will overwrite an earlier version. I suggest you include %1 into your destination-name.
- deleting subdirectories
- deleting files.
I'd suggest you simply remove the directory \\%1\CreditFileImagesTransmission\, then re-create it.
I'd also suggest that you add an extra line
goto :eof
after the del /q /s... line. This will cause execution to be transferred to the end-of-file (the colon in :eof is required) and may seem superfluous, but it ensures that the routine has a defined endpoint - if you add a further routine, there is no way the :servermove routine will continue into your new code.
After each server has been processed, you proceed to the :cleanuplogs routine, which I presume deletes logs older than 45 days.
Your next statement is a real problem. What it will do is grab the very first line of the logfile (which contains "%DATE% at %HH%%MN%" with the date resolved as you've set at the start and it then processes this line in :searchlog; there is no \\nt in this line, so errorlevel is set to 1, and the batch proceeds to :EXIT (not a good label in my view, since it's a keyword); executes an exitand should terminate the batch.
This appears not to be what it is actually doing, and I'm at a loss to explain why.
I'd suggest changing
:mailtest
FOR /F "tokens=*" %%A IN (%LOGFILE%) DO CALL :searchlog "%%A"
:searchlog
ECHO %1 | find "\\nt">NUL
IF NOT ERRORLEVEL 1 GOTO successmail
GOTO exit
to
:mailtest
find "\\nt" %LOGFILE%>NUL
IF NOT ERRORLEVEL 1 GOTO successmail
:failmail
echo "\\nt" was found in the log
pause
GOTO exit
but I can't test that...

:mailtest
FOR /F "tokens=*" %%A IN (%LOGFILE%) DO CALL :searchlog "%%A"
You are missing a GOTO :EOF or similar goto here because it will drop through to the routine below once the above is finished.
:searchlog
ECHO %1 | find "\\nt">NUL
IF NOT ERRORLEVEL 1 GOTO successmail
GOTO exit

I feel you can not carry the %1 of first for loop to others. Try to transfer that to another variable like below.
:ServerMove
set servername=%1
DIR /S /B \\%servername%\CreditFileImagesTransmission\*.* >> %LOGFILE%
XCOPY /E /C /I /Y "\\%servername%\CreditFileImagesTransmission\*.*" "\\nt980a3\CreditFileImagesTransmission\%DATE%\%HH%%MN%\"
FOR /D %%P IN ("\\%servername%\CreditFileImagesTransmission\*.*") DO RMDIR "%%P" /Q /S
DEL /Q /S "\\%servername%\CreditFileImagesTransmission\*.*"
Cheers, G

Related

Command works on command line, but operates unexpectedly in batch file

I have this simple batch code to check the Date Modified on a sub-folder (specifically the Recycle Bin.)
This search works flawlessly when manually input in Command Prompt, but not batch.
And using the same exact code to check other folders it works just fine. Help?
Code:
if exist C:\$Recycle.Bin (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do set {file}=%%a
for %%a in ("%{file}%") do echo Recycle Bin: %%~ta
popd
)
The reason this is not working in batch is for one annoying feature of IF statements with the SET command. As stated by This Post - "cmd expands variables when commands are parsed, not when they are run. It so happens that an if or for statement with a block ( ... ) (or actually any block) counds as a single command in that case. So when you set variables inside a block and try using them in the same block there are no variables anymore – they were replaced by the values the variables had before the block even executed." - Joey
To fix this you can simply not put your code block inside the IF statement but rather use an ELSE and have it goto an :EOF
Option 1: - Avoid IF Statement W/H Code Block
#ECHO OFF
Rem | Check If Directory Exists & pushd It.
if exist "C:\$Recycle.Bin" (pushd "C:\$Recycle.Bin") ELSE (goto :EOF)
Rem | Grab data on folders
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{File}=%%a")
Rem | Display data on folders
for %%a in ("%{file}%") do (echo Recycle Bin: %%~ta)
Rem | Un-pushd
popd
pause
goto :EOF
If you do however wish to use a block inside the IF statment you will need to use setlocal enabledelayedexpansion at the top of your script. Furthermore, to echo or read brackets you will have to use !{File}! over %{File}%.
Option 2: - Properly expand IF Statement W/H Code Block
#ECHO OFF
#setlocal enabledelayedexpansion
if exist "C:\$Recycle.Bin" (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{File}=%%a")
for %%a in ("!{file}!") do (
Set "data=%%~ta"
echo Recycle Bin: !data!
)
popd
) Else (Goto :EOF)
pause
goto :EOF

How to know end status of execution of command in batch programming

I am trying to cache clear in Mozilla browser by following command
#echo off
set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles
del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"
for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do del /q /s /f %%x\*sqlite
It is working fine but how can i make sure that execution of the command is completed i.e. cache in mozilla browser is cleared.
Ok, so as Christian.K stated, you can use %ERRORLEVEL% to determine the success or failure of each command executed. Lasse V. Karlsen stated, you can fail on this %ERRORLEVEL%'s value. Example using your code below:
#echo off
set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles
del /q /s /f "%DataDir%"
if %ERRORLEVEL% NEQ 0 goto :failure
rd /s /q "%DataDir%"
if %ERRORLEVEL% NEQ 0 goto :failure
setlocal enabledelayedexpansion
for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do (
del /q /s /f %%x\*sqlite
if !ERRORLEVEL! NEQ 0 goto :failure
)
endlocal
goto :EOF
:failure
echo do something here
exit
goto :EOF
Use NEQ 0 for testing %ERRORLEVEL%. I have seen this value be something other than 1 when a command fails.
setlocal enabledelayedexpansion handles the %ERRORLEVEL% variable changing after each execution of the for loop. Without this, it would just evaluate to whatever was contained in the %ERRORLEVEL% variable when the for loop was entered.
goto :EOF at the end of your main script so your labels are not executed after the script completes. Also add this at the end of your label to prevent the same (the exit I have in mine would prevent that as well, but you may not want to exit the script on failure).
To handle spaces etc in usernames you can use this as your last command:
for /d %%x in ("C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*") do del /q /s /f "%%x\*.sqlite"
This also assumes that the files you are deleting have a .sqlite extension.
The files will probably be locked if they are in use, if the browser hasn't been closed.

Batch file and DEL errorlevel 0 issue

The batch has to remove files and directories from specific locations and output success or stdout/stderr messages to a new .txt file. I have created the most of the script and it performs exactly as it should, except when the deletion is successful it moves forward to the next line rather than echo a 'successful' message on the log.
echo Basic Deletion Batch Script > results.txt
#echo off
call :filelog >> results.txt 2>&1
notepad results.txt
exit /b
:filelog
call :delete new.txt
call :delete newer.txt
call :delete newest.txt
call :remove c:\NoSuchDirectory
GOTO :EOF
:delete
echo deleting %1
del /f /q c:\Users\newuser\Desktop\%1
if errorlevel 0 echo succesful
GOTO :EOF
:remove
echo deleting directory %1
rmdir /q /s %1
GOTO :EOF
For some reason I can't find the syntax for if del succeeds echo 'successful'. In the above example if I remove the line
if errorlevel 0 echo successful
Everything works fine, but no success message. With this line left in it echoes success for every line.
del and ErrorLevel?
The del command does not set the ErrorLevel as long as the given arguments are valid, it even resets the ErrorLevel to 0 in such cases (at least for Windows 7).
del modifies the ErrorLevel only in case an invalid switch is provided (del /X sets ErrorLevel to 1), no arguments are specified at all (del sets ErrorLevel to 1 too), or an incorrect file path is given (del : sets ErrorLevel to 123), at least for Windows 7.
Possible Work-Around
A possible work-around is to capture the STDERR output of del, because in case of deletion errors, the related messages (Could Not Find [...], Access is denied., The process cannot access the file because it is being used by another process.) are written there. Such might look like:
for /F "tokens=*" %%# in ('del /F /Q "\path\to\the\file_s.txt" 2^>^&1 1^> nul') do (2> nul set =)
To use the code in command prompt directly rather than in a batch file, write %# instead of %%#.
If you do not want to delete read-only files, remove /F from the del command line;
if you do want prompts (in case wildcards ? and/or * are present in the file path), remove /Q.
Explanation of Code
This executes the command line del /F /Q "\path\to\the\file_s.txt". By the part 2>&1 1> nul, the command output at STDOUT will be dismissed, and its STDERR output will be redirected so that for /F receives it.
If the deletion was successful, del does not generate a STDERR output, hence the for /F loop does not iterate, because there is nothing to parse. Notice that ErrorLevel will not be reset in that case, its value remains unchanged.
If for /F recieves any STDERR output from the del command line, the command in the loop body is executed, which is set =; this is an invalid syntax, therefore set sets the ErrorLevel to 1. The 2> nul portion avoids the message The syntax of the command is incorrect. to be displayed.
To set the ErrorLevel explicitly you could also use cmd /C exit /B 1. Perhaps this line is more legible. For sure it is more flexible because you can state any (signed 32-bit) number, including 0 to clear it (omitting the number clears it as well). It might be a bit worse in terms of performance though.
Application Example
The following batch file demonstrates how the above described work-around could be applied:
:DELETE
echo Deleting "%~1"...
rem this line resets ErrorLevel initially:
cmd /C exit /B
rem this line constitutes the work-around:
for /F "tokens=*" %%# in ('del /F /Q "C:\Users\newuser\Desktop\%~1" 2^>^&1 1^> nul') do (2> nul set =)
rem this is the corrected ErrorLevel query:
if not ErrorLevel 1 echo Deleted "%~1" succesfully.
goto :EOF
Presetting ErrorLevel
Besides the above mentioned command cmd /C exit /B, you can also use > nul ver to reset the ErrorLevel. This can be combined with the for /F loop work-around like this:
> nul ver & for /F "tokens=*" %%# in ('del /F /Q "\path\to\the\file_s.txt" 2^>^&1 1^> nul') do (2> nul set =)
Alternative Method Without for /F
Instead of using for /F to capture the STDERR output of del, the find command could also be used like find /V "", which returns an ErrorLevel of 1 if an empty string comes in and 0 otherwise:
del "\path\to\the\file_s.ext" 2>&1 1> nul | find /V "" 1> nul 2>&1
However, this would return an ErrorLevel of 1 in case the deletion has been successful and 0 if not. To reverse that behaviour, an if/else clause could be appended like this:
del "\path\to\the\file_s.ext" 2>&1 1> nul | find /V "" 1> nul 2>&1 & if ErrorLevel 1 (1> nul ver) else (2> nul set =)
Different Approach: Checking File for Existence After del
A completely different approach is to check the file for existence after having tried to delete it (thanks to user Sasha for the hint!), like this, for example:
del /F /Q "\path\to\the\file_s.txt" 1> nul 2>&1
if exist "\path\to\the\file_s.txt" (2> nul set =) else (1> nul ver)
When using this syntax, instead of this
if errorlevel 0 echo successful
you can use this - because errorlevel 0 is always true.
if not errorlevel 1 echo successful
Just use rm from UnxUtils (or gow or cygwin). It sets the errorlevel correctly in case of a nonexistent file, or any errors deleting the file.
This was added as an edit by the original asker, I have converted it to a community wiki answer because it should be an answer, not an edit.
I found out how to do it... one way anyway.
echo Startup > results.txt
#echo off
call :filelog >> results.txt 2>&1
notepad results.txt
exit /b
:filelog
call :delete new.txt
call :delete newer.txt
call :delete newest.txt
call :remove c:\NoSuchDirectory
GOTO :EOF
:delete
echo deleting %1
dir c:\users\newuser\Desktop\%1 >NUL 2>&1
SET existed=%ERRORLEVEL%
del /f /q c:\Users\newuser\Desktop\%1
dir c:\users\newuser\Desktop\%1 2>NUL >NUL
if %existed% == 0 (if %ERRORLEVEL% == 1 echo "successful" )
GOTO :EOF
:remove
echo deleting directory %1
rmdir /q /s %1
GOTO :EOF
IF ERRORLEVEL 0 [cmd] will execute every time because IF ERRORLEVEL # checks to see if the value of ERRORLEVEL is greater than or equal to #. Therefore, every error code will cause execution of [cmd].
A great reference for this is: http://www.robvanderwoude.com/errorlevel.php
>IF /?
Performs conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT Specifies that Windows should carry out
the command only if the condition is false.
ERRORLEVEL number Specifies a true condition if the last program run
returned an exit code equal to or greater than the number
specified.
I would recommend modifying your code to something like the following:
:delete
echo deleting %1
del /f /q c:\Users\newuser\Desktop\%1
if errorlevel 1 (
rem This block executes if ERRORLEVEL is a non-zero
echo failed
) else (
echo succesful
)
GOTO :EOF
If you need something that processes more than one ERRORLEVEL, you could do something like this:
:delete
echo deleting %1
del /f /q c:\Users\newuser\Desktop\%1
if errorlevel 3 echo Cannot find path& GOTO :delete_errorcheck_done
if errorlevel 2 echo Cannot find file& GOTO :delete_errorcheck_done
if errorlevel 1 echo Unknown error& GOTO :delete_errorcheck_done
echo succesful
:delete_errorcheck_done
GOTO :EOF
OR
:delete
echo deleting %1
del /f /q c:\Users\newuser\Desktop\%1
goto :delete_error%ERRORLEVEL% || goto :delete_errorOTHER
:delete_errorOTHER
echo Unknown error: %ERRORLEVEL%
GOTO :delete_errorcheck_done
:delete_error3
echo Cannot find path
GOTO :delete_errorcheck_done
:delete_error2
echo Cannot find file
GOTO :delete_errorcheck_done
:delete_error0
echo succesful
:delete_errorcheck_done
GOTO :EOF
The answer of aschipfl is great (thanks, helped me a lot!) using the code under Presetting ErrorLevel you get a nice standard function:
Take care to use %~1 instead of %1 in the del statement, or you will get errors if you use a quoted filename.
::######################################################################
::call :DELETE "file.txt"
::call :DELETE "file.txt" "error message"
:DELETE
>nul ver && for /F "tokens=*" %%# in ('del /F /Q "%~1" 2^>^&1 1^> nul') do (2>nul set =) || (
if NOT .%2==. echo %~2
)
goto :EOF
BTW 1: You can give a nifty error message as a second parameter
BTW 2: Using :: instead of REM for comments makes the code even more readable.
Code:
Error Code: (What you did)
if errorlevel 0 echo succesful
The problem here is that you aren't calling errorlevel as a variable and plus you didn't add in the operator to the statement as well.
Correct Code: (Here is what it should actually be.)
if %ERRORLEVEL% EQU 0 echo succesful
Definitions:
EQU: The EQU stands for Equal. This kind of operator is also called a relational operator. Here is the documentation link to operators if you wanna know more, there are other ones but this helped me.
ERRORLEVEL: is declared as a variable and usually get the error level of the last command run usually. Variables are usually called when they are between percent signs like this
%foo%
For some more help on variables, go to cmd (Which you can go to by searching it on windows 10) and type in "set /?", without the quotes. the set command is the command you use to set variables

Search & delete temp files/folder (script's got a failure)

I use this script to clean history, cookies and cache (Temporary Internet Files) for all users AND it should also clean the temp dir BUT there seems to be something wrong.
Two things get mixed up I think, the %temp% variable (= D:\TEMP in my environment)
AND the users temp dir in the %userprofile%.
:: Works on Win XP -and- on Win 7
#echo off
Set "RegKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
set "regkey2=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\shell folders"
call:getspecialfolders "Cache, History, Cookies"
For /f "tokens=*" %%? in (
'Reg.exe QUERY "%RegKey%" ^|findstr /ric:"\S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$"'
) do (
For /f "tokens=2,*" %%A in (
'Reg.exe QUERY "%%?" /v ProfileImagePath ^|find /i "ProfileImagePath"'
) do call:Go %%B
)
start ""/w "%windir%\system32\RunDll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255
:end ***
goto:EOF
:Go
call Set "Target=%*"
If EXIST "%Target%" call:Clear "%Target%"
exit /b 0
:Clear
REM echo.&echo.%~1\%$$Cache%
pushD "%~1\%$$Cache%" &&(
rmdir /S /Q .
popD)2>C:\test1_TEMP_IE.txt
REM echo.&echo.%~1\%$$History%\History.IE5
REM pushD "%~1\%$$History%\History.IE5" &&(
REM rmdir /S /Q .
REM popD)2>C:\test1_History_IE.txt
REM echo.&echo.%~1\%$$History%
pushD "%~1\%$$History%" &&(
rmdir /S /Q .
popD)2>C:\test1_History.txt
REM echo.&echo.%~1\%$$Cookies%
pushD "%~1\%$$Cookies%" &&(
rmdir /S /Q .
popD)2>C:\test1_Cookies.txt
ECHO.&echo.%~1\%$$temp%
pushD "%~1\%$$temp%" &&(
rmdir /S /Q .
popD)2>C:\test1_Temp.txt
exit /b 0
:getspecialfolders
Set "FoldersToClear=%~1"
For %%* in (%FoldersToClear%) Do (
For /f "tokens=2,*" %%A in (
'reg.exe query "%regkey2%" /v %%* ^|find /i "%%~*"'
) do Call:sf1 "%%~B" "%%~*"
)
Call:sf2 "%temp%" "temp" "%userprofile%"
exit /b 0
:sf1
Call set "sf=%~1"
Call set "$$%~2=%%sf:%userprofile%\=%%"
exit /b 0
:sf2
Call set "sf=%~1"
call Set "usr=%~dpns3"
Call set "$$%~2=%%sf:%usr%\=%%"
exit /b 0
BUT somehow I can't get the last "temp part" to function so it cleans the %temp% (D:\Temp in my environment) and to also find al "temp dir's" in the %userprofile%.
ie. this for instance does work for %temp%:
PushD "%Temp%" && (
ATTRIB -S -H -R -A /D /S & (
For /f "Tokens=*" %%* in ('dir "%Temp%" /B') Do (
RD "%Temp%\%%*" /S /Q || Del /F /S /Q "%Temp%\%%*"))&PopD)2>c:\test0b_TEMP.txt
and this ie. work for the "user(s) temp":
::Set Search directory to "Documents and Settings" folder
(Set Target=%AllUsersProfile:~0,-10%)
title,Finding the Temp subfolders in %Target%&COLOR 9E
If EXIST "%Target%",(
For /f "Tokens=*" %%* in ('dir "%Target%" /B') Do (
cd/D "%target%\%%*\Local Settings\Temp" && (
ATTRIB -S -H -R -A /D /S >nul & (
For /f "Tokens=*" %%* in ('dir /B') Do (
RD "%%*" /S /Q ||Del /F "%%*" )))>nul)
)
I hope some one can help me out fixing the script, I think it's in the :sf2 and/or in combination with the %temp% part, somehow 2 things get mixed-up now ("users temp" en "environment temp").
All right, this time I think there is a way to fix this, namely, by adding a check whether the variable contains a relative or an absolute path, directly before proceeding with the cleanup. The idea is to test for the presence of the colon (:) in the string. If the colon is present then the path could not be converted to a relative path previously and so should be used as is, without pre-pending the profile path, otherwise the profile path should be attached before going on.
Here's a basic example that you can test and play with, if you like:
#ECHO OFF
SET somepath=D:\TEMP
CALL :checkpath
SET "somepath=Local Settings\Temp"
CALL :checkpath
PAUSE
GOTO :EOF
:checkpath
IF "%somepath%"=="%somepath:*:=%" (ECHO Relative path) ELSE ECHO (Absolute path)
In your particular situation I would probably apply the method like this:
instead of
…
ECHO.&echo.%~1\%$$temp%
pushD "%~1\%$$temp%" &&(
rmdir /S /Q .
popD)2>C:\test1_Temp.txt
…
I would try
…
IF "%$$temp%"=="%$$temp:*:=%" (SET "tmppath=%~1\%$$temp%") ELSE SET "tmppath=%$$temp%"
ECHO.&echo.%tmppath%
pushD "%tmppath%" &&(
rmdir /S /Q .
popD)2>C:\test1_Temp.txt
…
As you can see, a temporary variable is used to store the actual path to be processed. I understand it is enough to replace only the part where the temporary folder is being cleared, but you can see that the method can be easily applied to other folders as well, if needed.
It seems like your sf2 subroutine is trying to get a relative path of the Temp folder by cutting off the beginning of the path, which is essentially the user profile path:
Call set "$$%~2=%%sf:%usr%\=%%"
where sf contains the Temp folder and usr the user profile. So instead of, for instance, C:\Documents and Settings\APOC\Local Settings\Temp you would get simply Local Settings\Temp.
But there's a problem. Though the sf variable is assigned with the proper path, the usr variable, on the other hand, for some reason is assigned with the short named variant of the user profile path. That is, instead of something like C:\Documents and Settings\APOC it receives something like C:\DOCUME~1\APOC. Here's the offending line:
call Set "usr=%~dpns3"
So, when the formerly quoted line is executed, the expected substitution never happens, because C:\Documents and Settings\APOC doesn't match C:\DOCUME~1\APOC, naturally. As a result, the $$temp variable ends up with the complete path instead of the relative path, and because of that, the relevant parts of your code that reference $$temp don't do their job as expected.
In short, I think you don't need that sf2 routine at all. The sf1 routine seems to do just the same as sf2, only with a different set of parameters to pass. So, instead of
Call:sf2 "%temp%" "temp" "%userprofile%"
I'd suggest you to try this:
Call:sf1 "%temp%" "temp"
I changed the line for temp a little, is this a correct way? (it does work but is it properly formated?)
IF "%$$temp%"=="%$$temp:*:=%" (SET "tmppath=%~1\%$$temp%") ELSE SET "tmppath=%$$temp%"
ECHO.&echo.%tmppath%
pushD "%tmppath%" &&(
ATTRIB -S -H -R -A /D /S & (
rmdir /S /Q . || Del /F /S /Q .
)&popD)2>C:\test1_Temp.txt
+Extra "Del /f /s /q" for possible hard/read-only files (and additional attrib)
By the way
There still seems something not going 100% in the script with the "temp"-part, I changed my default temp settings in windows to the default paths voor testing purposes and I get these 3 error's:
screenshot of the error | url: http://img199.imageshack.us/img199/9127/errorif.jpg
script run with the windows default temp-path's:
[user] %UserProfile%\Local Settings\Temp and [system] %SystemRoot%\Temp
I'm not sure why I get these error's, when I set both temp parameters to D:\TEMP, I get 3 times the same sort of error but than for D:\TEMP instead of available/active users \local settings\Temp. Perhaps the problem is in locals~1???
(The temp-directories do get cleaned though, but why does it say it can't find the path?)

How to stop batch script on del failure

We have a batch script that removes files (del) and directories (rd). Does anyone know how to halt (fail) execution of the script if any of these delete statements fail? They could fail if a file/directory is locked by Windows. Thanks.
Update
Statements I'm using:
Del: del *.* /S /Q
RD: FOR /D %%G in (*) DO RD /s /q %%G
For deleting the files, you can first try to ren (rename) the file.
ren will set ERRORLEVEL to 1 if the file is locked. Add quotes around filename.
#echo OFF
:: Delete all files, but exit if a file is locked.
for %%F in (*.*) do (
#echo Deleting %%F
ren "%%F" tmp 2> nul
if ERRORLEVEL 1 (
#echo Cannot delete %%F, it is locked.
exit /b 1
)
del tmp
)
I suspect you may be able to do the same thing for directories, but I can't seem to figure out how to get a directory locked so I can test. The following may work:
:: Remove all directories, but exit if one is locked.
FOR /D %%G in (*) DO (
#echo Removing %%G
ren "%%G" tmpdir 2> nul
if ERRORLEVEL 1 (
#echo Cannot remove %%G, it is locked
exit /b 1
)
RD /s /q tmpdir
)
DEL doesn't return an errorlevel if the file is locked. I just did a test with excel file and I saw a zero (on Windows XP).
It might be better to use IF EXIST for the file to be deleted after you do the delete.
del file.txt
if exist file.txt ECHO "FAIL"
AFTER EDIT
Disclaimer: I have no idea how this performs...
You could do this for the files
DIR /B /S /A-d > c:\filestodelete.txt
del *.* /S /Q
FOR /F %%i in (c:\filestodelete.txt) DO (
IF EXIST %%i ECHO %%i STILL EXISTS
)
then for the directories
DIR /B /S /Ad > c:\directoriestodelete.txt
FOR /D %%G in (*) DO RD /s /q %%G
FOR /F %%i in (c:\directoriestodelete.txt) DO (
IF EXIST %%i ECHO %%i STILL EXISTS
)
EDIT: Right, so del does not set the ERRORLEVEL correctly. See ERRORLEVEL on DEL and Windows delete command can fail silently
PREVIOUS (incorrect) SOLUTION
You should check the errorlevel.
For example:
del file.txt
if errorlevel 1 goto FAIL
:PASS
echo Worked!
goto :END
:FAIL
echo Failed!
exit /B 1
:END
Here's my preferred way to check for errors while deleting files. We redirect the "error output" (standard file "2") to a file (e.g. delCmd.err). Then we use the FOR command as a way to get access to the ~z "file size" operater. If the size of the output file is not 0, then we know that "del" got an error... we display the error with the "type" command and exit the batch file with a non-zero error code:
del unwanted.txt 2> delCmd.err
FOR /F "usebackq" %%A IN ('delCmd.err') DO set size=%%~zA
if not "%size%"=="0" (
echo Error deleting unwanted.txt
type delCmd.err
exit /B 1
)
Here is a shorter version of the result monitoring variant. This is using 2>&1 trick to redirect stderr to stdout and for /f to check for any output.
#echo off
setlocal
set error=0
for /f %%i in ('del notepad2.exe 2^>^&1') do set error=1
echo %error%
Just for your information. The DEL command does return an error code if a serious error occurs, however it's behavior is way beyond our intuition that people would simply believe that the error code doesn't work at all.
This is what I've tested in DEL command in Windows 7:
Successful deletion of all files: 0 (of course)
Some files deleted, some files missing: 0 (intuition expects 1)
Deletion failure due to no permission or a read-only medium: 0 (intuition expects ≥ 1)
Non-existent drive or drive not ready (such as no CD on a CD-ROM drive): 1 (yes, you get it, but I will expect a higher error code)
Invalid path: 1 (I will expect a higher error code, too)
And, if you specify a list of files to DEL command, where at least one of the files fit the last two kinds of error mentioned above, then none of the files in the list will be deleted at all.
There is a script that will work like a charm.
It tests deletion failure by testing the existence of the supposedly deleted file/directory.
The output will be an existing c:\mydirectory with completely empty content.
Any error breaks the process and is reported.
The only downside is that directories are removed recursively, because there is no simple way to start deleting directories starting with the most "sub" directories.
#echo OFF
set path_to_clean=c:\mydirectory
#echo -Deleting Files
dir /A-D /S /B %path_to_clean%\* > fileslist.txt
for /F %%F in (fileslist.txt) do (
#echo Deleting %%F
del %%F 2> nul
if exist %%F (
#echo Cannot delete %%F, it is locked.
goto errorhandling
)
)
del fileslist.txt
#echo -Deleting Files done
#echo -Deleting Directories
dir /AD /B %path_to_clean%\* > directorieslist.txt
for /F %%D in (directorieslist.txt) do (
#echo Deleting %path_to_clean%\%%D
rmdir /S /Q %path_to_clean%\%%D 2> nul
if exist %path_to_clean%\%%D (
#echo Cannot delete %path_to_clean%\%%D. This folder or one of its sub-directories is locked.
goto errorhandling
)
)
del directorieslist.txt
#echo -Deleting Directories done
:errorhandling
rem some code here
These seem to work for me:
RD:
FOR /D %d IN (*) DO ( RD /S /Q "%d" & IF EXIST "%d" EXIT /B 1 )
DEL:
FOR %f IN (*.*) DO ( DEL /Q "%f" & IF EXIST "%f" EXIT /B 1 )
No idea about the performance though.
You can add flag /F to DEL for a "force delete" mode.
For rd and rmdir you can do
rd "some directory" || rem
this will set the errorlevel correctly - see
Hi This should work as well
#echo off
for /f %%i in ('del notepad2.exe 2^>^&1') do set ERRORLEVEL=1
IF %ERRORLEVEL%=1 then exit

Resources