0

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.

mrRobot
  • 3
  • 2
  • 3
    I'm a little lost, because my quick read of your code suggests that you could just recursively iterate/search for `*_%year%_%month%_%day%.doc` in the first place! – Compo Nov 10 '21 at 18:30

1 Answers1

0

%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
)
Anders
  • 97,548
  • 12
  • 110
  • 164
  • Thanks @Anders for your answer that works very well ! Is it possible to match also if the date is anywhere in the string? like a file with the name : test_2021_11_07_21-10-49.doc ? – mrRobot Nov 10 '21 at 20:16
  • Okay, I got it ! Simply did that: for /R D:\testDir %%i in (*_%year%_%month%_%day%*.doc) do ( echo %%i ) Thanks for your time and answer. – mrRobot Nov 10 '21 at 20:20