I have a problem with the elaboration of a script with two conditions. I would like to check the extension and a part of the file name. I need to perform actions on the day's files with the .doc extension
I have a problem with this script. Here is what I have done but it only works partially:
@echo off
chcp 1252
set Pathname="D:\testDir"
set year=%date:~-4%
set month=%date:~-7,2%
set day=%date:~-10,2%
set logfile=%Pathname%\logs\log.txt
cd %Pathname%
d:
for /R %%i in (*) do (
if "%%~xi"==".doc" (
echo "%%i"|findstr /i /L "%year%_%month%_%day%">nul
if %ERRORLEVEL%==0 (
echo "file : %%i worked and does an output at %time%" >> %Pathname%\logs\log.txt
)
)
)
I don't understand why the output of %ERRORLEVEL% is always equal to 0.
Here is an extract of the tree structure :
D:\testDir\directory1\test_2021_11_07.doc
D:\testDir\directory1\test_2021_11_07.log
D:\testDir\directory1\test_2021_11_08.doc
D:\testDir\directory1\test_2021_11_08.log
D:\testDir\directory1\test_2021_11_09.doc
D:\testDir\directory1\test_2021_11_09.log
D:\testDir\directory1\test_2021_11_10.doc
D:\testDir\directory1\test_2021_11_10.log
D:\testDir\directory2\test_2021_11_07.doc
D:\testDir\directory2\test_2021_11_07.log
D:\testDir\directory2\test_2021_11_08.doc
D:\testDir\directory2\test_2021_11_08.log
D:\testDir\directory2\test_2021_11_09.doc
D:\testDir\directory2\test_2021_11_09.log
D:\testDir\directory2\test_2021_11_10.doc
D:\testDir\directory2\test_2021_11_10.log
D:\testDir\directory3\test_2021_11_07.doc
D:\testDir\directory2\test_2021_11_07.log
D:\testDir\directory3\test_2021_11_08.doc
D:\testDir\directory2\test_2021_11_08.log
D:\testDir\directory3\test_2021_11_09.doc
D:\testDir\directory2\test_2021_11_09.log
D:\testDir\directory2\test_2021_11_10.doc
D:\testDir\directory2\test_2021_11_10.log
D:\testDir\logs\log.txt
If you have another solution, I'm interested! Thanks for your help and advices.
%errorlevel% is always 0 because %variables% are not updated inside (
)
blocks. You need delayed expansion:
@echo off
setlocal enableextensions enabledelayedexpansion
if 1==1 (
set foo=bar
echo !foo!
)
but there are better ways. You could use the older if not errorlevel 1
syntax inside the code block but in this specific case you can get the loop to filter for you:
for /R D:\testDir %%i in (*_%year%_%month%_%day%.doc) do (
echo %%i
)