0

I wrote a batch script which I want it to look into folders and find textfiles which match a certain pattern. When there is a match with the pattern that i specified, i want to create a folder and move the matched files into the created folder.

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

set source=Folder_Name.txt


For /f "tokens=2-4 delims=/ " %%a in ('date /t') DO (set year=%%c)
For /f "tokens=2-4 delims=/ " %%a in ('date /t') DO (set /a month=%%b-1)

if !month! == 0 (set /a year=year-1)
if !month! == 0 (set month=12)


set exception=!year!!month!

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Check Every Folder in the source for pattern matching.
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

FOR /f "tokens=* delims=" %%H IN (!source!) DO ( 

set location=%%H\Logs

 For /R "!location!" %%f in ("*!exception!*.txt") Do (echo %%f>> output.txt)


)

Folder_Name.txt

Folder_1

Folder_2

Folder_3

The script will go into the sub directories of each folder, eg.(C:\Users\halo\Desktop\Scripts\Move\folder_1\logs) and find files which match the date pattern of the previous month,yyyymm eg.(201511).

So I hope to find files such as (cloud_20151101.txt or cloud_20151102.txt) and move them to a folder.

However, my script doesnt work when i read in the folder name. When i change the !location! in the recursive for loop to C:\Users\halo\Desktop\Scripts\Move\folder_1\logs\, it works.I have not implemented moving the files as i want to resolve this issue first. Thanks for help.

user1823986
  • 87
  • 3
  • 11
  • 1
    Your script doesn't set the working directory - so the behaviour depends on where it is launched from – foxidrive Dec 03 '15 at 12:15
  • Hi, i placed my script in the working directory so in my script, i didn't set any explicit path. Just the sub directory path which i want the script to search. – user1823986 Dec 04 '15 at 03:06

2 Answers2

0

First of all, check how you are getting the tokens for the dates. Since you are delimiting it at the "/" character, you will have 3 tokens from the date /t command.

So for getting your date pattern, try doing like this:

For /f "tokens=3 delims=/ " %%a in ('date /t') DO (set year=%%a)

For /f "tokens=2 delims=/ " %%a in ('date /t') DO (set /a month=%%a-1)

Now for the checks in the directory read from "Folder_Name.txt", you could do:

FOR /f "tokens=* delims=" %%H IN (!source!) DO ( 
    set location=%%H\Logs
    dir /b !location! | findstr /r /c:"!exception!">>output.txt
)

Then you just need to implement the logic to move the files around. At the dir line, you could output to something like:

dir /b !location! | findstr /r /c:"!exception!">>output_%%~nH.txt

So you will have one output file for each directory you read. The format of the output file will be: output_NAME-OF-THE-DIRECTORY-SEARCHED.txt

Hope it helps.

Daniel Luz
  • 134
  • 1
  • 6
  • Hi, Thanks for the explanation. I tried your method. It works if i don't change my tokens for the year and month to 3 & 2. It should probably be the date time of my system settings.Thanks! – user1823986 Dec 04 '15 at 03:21
0

Format of string output by date /t depends on Windows region and language settings.

Instead of date /t the value of environment variable DATE is used in code below which on access has always the current date. The format of the date string depends also on Windows region and language settings. For example for German countries the format is dd.mm.yyyy while in other countries the format is dd/mm/yyyy or mm/dd/yyyy. Run in a command prompt window echo %DATE% to see local date format.

Note: Format of date \t (often with weekday) is quite often different to format of %DATE% (usually without weekday).

My answer on Find out if file is older than 4 hours in batch file contains more information about date and time on Windows and also demonstrates how to use wmic to get date string in a region and language independent format with the disadvantage of batch script becomes slower.

The code below works for date formats dd.mm.yyyy, dd/mm/yyyy and d/m/yyyy (no leading 0 for day or month smaller than 10).

Other Windows command processing specifics must be taken also into account like a number with 2 or 3 digits with a leading 0 being interpreted as octal number on calculations.

Here is a commented code which should work for your task.

@echo off
setlocal EnableExtensions
set "source=Folder_Name.txt"

rem Set directory of batch file as current directory.

cd /D "%~dp0"

rem Get month and year from environment variable DATE which has for
rem countries with German as native language the format dd.mm.yyyy.
rem The code below works also for date formats dd/mm/yyyy and d/m/yyyy.

for /F "tokens=2,3 delims=./" %%A in ("%DATE%") do (
    set "month=%%A"
    set "year=%%B"
)

rem Remove leading zero from month as otherwise the value
rem would be interpreted as octal number resulting in an error
rem for 08 (August) and 09 (September) on next calculation.

if "%month:~0,1%" == "0" set "month=%month:~1%"

rem Subtract 1 from month.

set /A month-=1

rem Decrease year by 1 and set month to December if month is now 0.
rem Otherwise check if month is smaller than 10 and
rem insert a leading 0 if this condition is true.

if %month% == 0 (
    set /A year-=1
    set "month=12"
) else if %month% LSS 10 set "month=0%month%"

set "exception=%year%%month%"

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Check every folder in the source for pattern matching.
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

rem Command DIR with parameter /B (bare format - only file name) returns
rem only file names (parameter /A-D) matching the search pattern ordered
rem by name (parameter /ON) found in specified directory or any subdirectory
rem (parameter /S) with full path because of parameter /S and always without
rem quotes even if file name with path contains 1 or more spaces.

if exist output.txt del output.txt
for /F "usebackq delims=" %%H in ("%source%") do (
    for /F "delims=" %%F in ('dir /A-D /B /ON /S "*%exception%*.txt" 2^>nul') do echo %%F>>output.txt
)

rem Restore previous environment table, restore previous
rem states for command extensions and delayed expansion
rem and restore also previous current directory.

endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks for the help. I verified that the date time that i pulled was correct and exception was the right one. Was unable to resolve the issue with why i had to set the full path of the folder when my script is in the working directory. Thanks. – user1823986 Dec 04 '15 at 03:46