0

I recently asked you guys some help for a double conditions in batch file. You guys helped me very well but I'm now struggling with a new trouble... I'm trying to optimize my script.

Previous question (How to double conditions in batch file?)

I would like to list all files with the .doc extension that are in the subfolders named on the current date only. I think I'm making a mistake on the use of this double loop.

@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 /D /R %%i in (*%year%_%month%_%day%*) do (
    for /R %%i %%s in (*.doc) do (
        echo "file : %%s worked and does an output at %time%" >> %logfile%
    )
)


Can you give me a little help? Thanks for your help and advices.

EDIT: I need to perform actions on each .doc file in folders containing the string YEAR_MONTH_DAY. But the .doc files can also be located in other subdirectories.

It could be something like that :

D:\testDir\directory1_2021_11_16\test.doc
D:\testDir\directory1_2021_11_16\test.log
D:\testDir\directory1_2021_11_16\subDirectory1\test.doc
D:\testDir\directory1_2021_11_16\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\test.doc
D:\testDir\directory1_2021_11_17\test.log
D:\testDir\directory1_2021_11_17\subDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\subSubDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\ubSubDirectory1\test.log
D:\testDir\directoryThatIDontCare\test.doc
D:\testDir\directoryThatIDontCare\test.log
D:\testDir\directoryThatIDontCare\subDirectory1\test.doc
D:\testDir\directoryThatIDontCare\subDirectory1\test.log

Any solutions ? Thanks for your time guys !

mrRobot
  • 3
  • 2
  • 1
    Consider using a `for /f` loop on the output of `dir /b /s *%year%_%month%_%day%*.doc` – T3RR0R Nov 17 '21 at 18:39
  • thanks for that answer but this does not work because it also lists files @T3RR0R – mrRobot Nov 17 '21 at 19:30
  • 1
    Then add the `/A:D` option to @T3RR0R's suggested `dir` command line, or use just one loop like `for /D /R %%i in (*%year%_%month%_%day%*.doc) do (…)`. And merge `cd %Pathname%` plus `d:` to `cd /D "%Pathname%"`… – aschipfl Nov 17 '21 at 20:22
  • @mrRobot Consider clarifying your question. You state: "I would like to list all files with the .doc extension" . with the search pattern exampled, only .doc ( or docx ) files matching Year_Month_Date should be output, which is what you have stated is your goal. Other doc extensions can be eliminated using: `For /f "Delims=" %%G in ('dir /b /s /A:-D *%YEAR%_%MONTH%_%DAY%*.doc')Do If "%%~xG"==".doc" Echo(%%~fG` – T3RR0R Nov 17 '21 at 20:34
  • 1
    Note also that when changing drives via the `CD` command, the `/D` Switch is required. – T3RR0R Nov 17 '21 at 20:35
  • I would like to check the directories with today's date first and then take action on the files inside. Only, there may be several sub-folders as well. I'm trying to find the best solution in terms of processor load. Thanks guys for your answers, I learned some. I have also modified my application to make it clearer @T3RR0R – mrRobot Nov 17 '21 at 22:35

1 Answers1

0

Here's an example script you can learn from, and run, to achieve what your submitted code was intending to do.

@Echo Off
SetLocal EnableExtensions
Set "CurDate="
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
For /F "Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
 Win32_LocalTime Get Day^, Month^, Year /Format:List
 2^>NUL') Do For /F "Tokens=*" %%H In ("%%G") Do Set /A "_%%G + 10000"
For /F "Tokens=1,* Delims==" %%G In ('"(Set _) 2>NUL"') Do (
    SetLocal EnableDelayedExpansion & If %%H Gtr 10059 (For %%I In (!%%G:~-4!
        ) Do EndLocal & Set "%%G=%%I") Else For %%I In (!%%G:~-2!
    ) Do EndLocal & Set "%%G=%%I")
Set CurDate=%_Year%_%_Month%_%_Day%
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
If Not Defined CurDate GoTo :EOF
Set "BaseLocation=D:\testDir"
PushD "%BaseLocation%" 2>NUL || GoTo :EOF
Dir "logs" /B /A:D 1>NUL 2>&1 || (MD "logs\%CurDate%" 2>NUL || GoTo :EOF)
Set "LogFile=%BaseLocation%\logs\log.txt"
For /F "EOL=? Delims=" %%G In ('Dir "*_%CurDate%" /B /A:D 2^>NUL'
) Do For /F Delims^= %%H In ('Set "PATHEXT^=" ^& %SystemRoot%\System32\where.exe
 /F /R "%%G" "test.doc" 2^>NUL'
) Do (Echo file : %%~H worked and does an output at %TIME%) 1>>"%LogFile%"

If you want to know how any of it works, please use the built-in help information for each command, use the site search facility and/or your chosen search provider. I am not a private tutor, so will not be performing such a role.

Compo
  • 36,585
  • 5
  • 27
  • 39