Batch: Checking if a file is locked does not work - batch-file

I wrote the following batch to do the following steps:
Check if a file on a server is opened by another user
make a backup of the file
open the file
2>nul ( >>test.xlsx (call )) if %errorlevel% == 1 goto end
#echo off
rem get date, make if file name friendly
FOR /F "tokens=1-4 delims=/ " %%i in ('date/t') do set d=%%j-%%k-%%l#%%i#
rem get time, make if file name friendly
FOR /F "tokens=1-9 delims=:. " %%i in ('time/t') do set t=%%i_%%j_%%k%%l
set XLSX=%d%%t%.xlsx
ren Test.xlsx %xlsx%
xcopy *.xlsx J:\Test\Backup
ren %xlsx% Test.xlsx
call Test.xlsx
:end
The problem is, that the line wich tries to check if the file is locked does not work on the server.
Can anybody help me to find the mistake in my batch?

If you write
2>nul ( >>test.xlsx (call )) if %errorlevel% == 1 goto end
you get an error. if is not expected. Two instructions in the same line without separation.
If it is converted into
2>nul ( >>test.xlsx (call )) & if %errorlevel% == 1 goto end
Then the problem is delayed expansion. The %errorlevel% variable is replaced with its value when the line is parsed and at this time, the first part of has not been executed, so no errorlevel is set.
If you change it to
2>nul ( >>test.xlsx (call ))
if %errorlevel% == 1 goto end
it will work
For a more concise construct, you can try this
(>>test.xlsx call;) 2>nul || goto end
Same function, less code.

Related

IF EXIST - Batch script to match case

My requirement is to check if the file exists in a folder only when case matches the search.
I have searched the internet, also the basic command prompt help guide. I have no accurate answer. However, in the StackOverflow post: REF-Post the solution is provided by sending FileName as a variable to batch file. However, I would like to search the file by not using any variable sent to the Batch file.
#echo off
dir /b /a-d "%~1"|find "%~1" >nul
if %errorlevel% == 0 (echo found) else (echo fail)
This code takes %~1 value and I want to substitute %~1 with a path directly! Please help with the solution
For example:
a file named testfile.txt exists in folder C:\Files\
Logic should be something like below:
IF EXIST C:\Files\TESTFILE.txt (
echo file of case exists
) else (
echo file of this case does not exist
)
Here is a corrected form of Gerhard's original answer using FINDSTR:
dir "C:\files" /b /a-d | findstr /x /c:"TESTFILE.txt" && echo Found || echo Not found
The /c:"string" option is needed to force a literal interpretation (not regex), and to allow spaces in the name. The /X option is needed to force an exact match. You don't want to mistakenly match "the_TESTFILE.txt".
The above is probably the most straight-forward technique.
This can also be solved with a FOR statement. If %%F contains the name of an existing file (case insensitive), then %%~nxF will expand to the actual case sensitive name on disk.
But if %%F does not exists, then %%~nxF will never change the case. So IF EXIST must also be used.
The command must be carefully constructed to give the correct result. My goal is to have a self contained construct that allows use of && and || at the end to conditionally take action depending on whether the file exists or not.
pushd C:\files
(
for %%F in ("TESTFILE.txt") do (
if exist %%F if %%~F==%%~nxF popd & (call)
) && popd
) && echo NOT FOUND || echo FOUND
or on a single line
pushd C:\files&(for %%F in ("TESTFILE.txt") do (if exist %%F if %%~F==%%~nxF popd&(call))&&popd) && echo NOT FOUND || echo FOUND
Note the inverse logic. If the file exists with matching case, then (call) is the last executed command within the parentheses, resulting in a non-zero return code. If the file does not exists or does not match case, then popd is the last executed command within parentheses, resulting in a zero return code.
To search for the file with the prefered case:
#echo off
set "res=" && for /f "delims=" %%i in ('dir "C:\Files" /b /a-d') do if "%%i"=="TESTFILE.txt" set "res=%%i"
if defined res (echo %res% Found) else (echo File not found)
still using for /f but without setting a variable:
#echo off
for /f "delims=" %%i in ('dir "C:\files" /b /a-d') do if "%%i"=="TESTFILE.txt" echo File found & goto :eof
echo File not found
or simply without /f but you would need to
#echo off
for %%i in (C:\Files\*) do if "%%i"=="TESTFILE.txt" echo File found & goto :eof
echo File not found

check for spaces in file name inside of for loop in batch file

I have a batch script that is calling a VBscript file. It reiterates through all files in a watched folder.
It needs to verify if the file name has spaces in it and if so reject the file and not process it with the VBS.
I must have an error on the code as I get the error message:
ELSE was not expected at this time.
I have looked at the documentation and searched for the answer for quite some time, including this question: check "IF" condition inside FOR loop (batch/cmd)
But still, I can't see what is wrong in my syntax:
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
call :ReadIni Infolder inFolder
call :ReadIni Outfolder outFolder
echo %inFolder%
echo %outFolder%
pause
:StartLoop
FOR %%I in (%inFolder%\*.srt) DO (
ECHO %%I
ECHO %%~nxI
SET TESTNAME=%%~nxI
ECHO !TESTNAME!
ECHO !TESTNAME: =_!
PAUSE
IF NOT !TESTNAME!==!TESTNAME: =_! (
move "%~sdp0%%~nxI" "%outFolder%\ERROR_IN_FILE_NAME_%%~nxI"
) ELSE (
copy /y "%%I" "%~sdp0%%~nxI"
%~sdp0SRToffset.vbs "%~sdp0%%~nxI" "%~sdp0%%~nxI"
IF %ERRORLEVEL%==1 (
Goto StartLoop
) else (
move "%~sdp0%%~nxI" "%outFolder%\"
move "%~sdp0QC_%%~nxI" "%outFolder%\"
del "%%I"
)
)
)
timeout /t 1
goto StartLoop
:ReadIni
FOR /F "tokens=2 delims==" %%a in ('find "%~1=" config.ini') do set %~2=%%a
exit /b
Any help would be appreciated.
IF NOT "!TESTNAME!"=="!TESTNAME: =_!" (
...
IF %ERRORLEVEL%==1 (
Quoting the strings causes cmd to regard the string as a single entity.
Note that the following if %errorlevel% will be executed using the value of errorlevel at :startloop. (See delayed expansion for reasoning.)
Cure by using if !errorlevel!==1 (. (Using the runtime value of errorlevel as established by the vbs routine.)

Delete duplicate files using Windows command line

Given two directories c:\foo and c:\bar I want to delete the files in c:\bar that are identical to files present in c:\foo. I can use the fc command to compare each file in c:\bar with a file of the same name in c:\foo and delete duplicates manually. Is there a simple way to automate this using CMD?
If identical means similar or alike in every way: in every way, not only in date and size, therefore forced binary comparison:
#ECHO OFF >NUL
SETLOCAL enableextensions
pushd "D:\bat\FooBar"
for /F "delims=" %%G in ('dir /B /A:-D *.*') do (
call :proFC "%%~fG" "D:\bat\FooFoo\%%~nxG"
)
popd
ENDLOCAL
goto :eof
:raiseerror
exit /B %1
:proFC
call :raiseerror 321
fc /B "%~1" "%~2" >NUL 2>&1
if %errorlevel% EQU 0 (
echo del "%~1"
) else (
echo %errorlevel% "%~2"
)
goto :eof
Commented crucial points in the above script:
pushd ... switches current working directory
for /F ... loop treats static file list of the Bar folder
call :proFC ... with properly quoted line arguments parameters
popd switches current working directory back
goto :eof ends the script
:raiseerror subroutine returns exit code via exit /B %1
:proFC productive subroutine
call :raiseerror 321 important as For an invalid switch (with two passed files) an error message is printed but the errorlevel is not changed
fc /B "%~1" "%~2" >NUL 2>&1 output and error messages redirected to NUL as the errorlevel is important;
if %errorlevel% EQU 0 (
echo del "%~1" a file delete merely _echo_ed for debugging purposes
) else (
echo %errorlevel% "%~2" for debugging purposes (see below).
goto :eof returns from the subroutine
FC will set an ErrorLevel as follows (but see a note at the call :raiseerror 321 point):
-1 Invalid syntax (e.g. only one file passed)
0 The files are identical.
1 The files are different.
2 Cannot find at least one of the files.
#echo off
cd c:\bar
for %%a in (*.*) do for %%b in ("c:\foo\%%a") do (
if exist "%%b" (
if "%%~Ta %%~Za" equ "%%~Tb %%~Zb" (
del "%%a"
) else (
fc "%%a" "%%b" > NUL
if not errorlevel 1 del "%%a"
)
)
)
If two files may have different modification dates but be equal, remove the %%~T.. parts in the comparison.

How to change variable value within BATCH file by the file itself?

I was looking for a long time now for an answer to this, learned nice tricks from http://www.dostips.com/DtTutoPersistency.php and http://ss64.com/nt/for_cmd.html sites, but still - don't have a solution to the problem I've encountered in:
I have a BATCH file where I test the existence of specific folder (SendTo folder). In case I couldn't find it by the script - I want the user to enter the path to that folder - and keep the result in the BATCH file.
My narrowed BATCH file ("Some file.bat") looks something like:
#echo off
REM SomeNonsense
:: Win7/Vista
IF EXIST %APPDATA%\Microsoft\Windows\SendTo\NUL (
REM Do something
GOTO :EOF
)
:: WinXP
IF EXIST %USERPROFILE%\SendTo\NUL (
REM Do something
GOTO :EOF
)
:: Else
SET SendPath=
SET /P SendP="Please enter the path to the SendTo Folder:> "
IF EXIST %TMP%\SendPath.txt DEL %TMP%\SendPath.txt
FOR /F "usebackq TOKENS=* DELIMS=" %%A in ("%~0") DO (
ECHO %%A>>%TMP%\SendPath.txt
REM Later I want to change the value of SendPath with SendP,
REM And swap the file back to the original name
)
My problem right now is that the lines of the file actually being interpreted, when I want only to copy the text itself to a temp file (without using COPY, because I want to copy line by line in order to change SendPath value).
Another thing is that empty lines aren't copied.
Any solution?
This do what you want:
#echo off
rem Your previous Win7/Vista, WinXP testings here...
:: Else
call :defineSendPath
if defined SendPath goto continue
SET /P "SendPath=Please enter the path to the SendTo Folder:> "
rem Store the SendPath given into this Batch file:
echo set "SendPath=%SendPath%" >> "%~F0"
:continue
rem Place the rest of the Batch file here...
goto :EOF
rem Be sure that the following line is the last one in this file
:defineSendPath
As a proof of concept
#echo off
setlocal enableextensions disabledelayedexpansion
call :persist.read
if not defined savedValue (
set /p "savedValue=Value to save:" && ( call :persist.write savedValue ) || (
echo Value not set, process will end
exit /b 1
)
)
echo Saved value = [%savedValue%]
goto :eof
:persist.read
for /f "tokens=1,* delims=:" %%a in ('
findstr /l /b /c:":::persist:::" "%~f0"
') do set "%%~b"
goto :eof
:persist.write varName
if "%~1"=="" goto :eof
for %%a in ("%temp%\%~nx0.%random%%random%%random%.tmp") do (
findstr /l /v /b /c:":::persist::: %~1=" "%~f0" > "%%~fa"
>"%~f0" (
type "%%~fa"
echo(
setlocal enabledelayedexpansion
echo(:::persist::: %~1=!%~1!
endlocal
)
del /q "%%~fa"
)
goto :eof
The problem with a batch file that edits itself while running is that it keeps pointers to the character position in the file where the commands are being executed. You can only make changes in lines after the current executing one and this can also generate other problems. So, the safest (not the more elegant nor the fastest) generic approach could be to write the data as comments at the end of the file.

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

Resources