Search all the items listed in a file against all versions of sub folders till found

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Search all the items listed in a file against all versions of sub folders till found

#1 Post by goodywp » 11 Sep 2017 08:56

I had this requirements for a while. And I also tried some solution with 2 or 3 steps. I am still wondering if any good solution can be reached.

Requirement:
input: a list of files (which can be search from mainly 3 sub folders).
input
500007011000.S3S
500008011000.S3S
500022010300.S3S
500009011000.S3S
500012011000.S3S
500014011000.S3S


One thing is that these 3 sub folders are version based, meaning not only 1 version, can have many versions when it goes...

C:/test/T501-01122-0100
C:/test/T501-01122-0101

C:/test/T501-01133-0100
C:/test/T501-01133-0101

C:/test/T501-02233-0100
C:/test/T501-02233-0101


The search always try to search the latest version of these 3 sub folders, like this case search version 0101 first. then the previous version, 0100 for the files which has not been found in the latest version

output: if one or two not found give a message, otherwise it will out put the files with their path found, looks like this:
output
C:/test/T501-01122-0100/500007011000.S3S
C:/test/T501-01122-0100/500008011000.S3S
C:/test/T501-01122-0101/500009011000.S3S
C:/test/T501-01133-0101/500010011000.S3S
C:/test/T501-02233-0100/500011011000.S3S
C:/test/T501-02233-0101/500012011000.S3S

Any suggestion is also welcomed in regards the structure of sub folders, favor to the search...
like always 3 sub folders for the search then search the missing one in previous version sub folder...

Thanks

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Search all the items listed in a file against all versions of sub folders till found

#2 Post by pieh-ejdsch » 11 Sep 2017 12:46

Hello,

File Versioning File versioning is one way to accomplish this.
Instead of looking for a backup folder you are looking for the three subfolders.
The files may not be saved to another folder, but only one variable with the current version must be set to read this variable at the end.
one file after another.
You can also find the latest version quite independently of a version name with Robocopy.
Simply omit the timestamp in the name.

You can also run each file recursively in all folders and then save them with Robocopy in a seperate folder (the key is: 0 byte files) and at the end of this folder list.

Phil

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Search all the items listed in a file against all versions of sub folders till found

#3 Post by goodywp » 13 Sep 2017 07:38

Thanks Phil, I did not get the time to try yet but thanks for your suggestion!!

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Search all the items listed in a file against all versions of sub folders till found

#4 Post by pieh-ejdsch » 18 Sep 2017 17:28

Hello,
I shook something from my sleeve
Although it is not made with an input list, this can be safely implemented.
It is not controlled whether filenames with uneven lengths exist.
This version control is only made via the filename.
The name is browsed from the back to remove the different components.
The directory is scanned after the first and last version, and then everything is listed in between.
If something is not found in the chronological order, this is done with the error output.

Code: Select all

@echo off
setlocal
set prompt=$g$s
call :setAllMacros
pushD C:\Users\piehejdsch\Desktop\CMDtestX\test_012

for /f "delims=" %%i in ('dir /s /b *.test') do set "Name_%%~ni=%%~fi"
set /a fullList=stringMax=0
set "stringMin="
for /f "tokens=1,2* delims=[]=" %%i in ('set Name_^|sort^|find /n /v "" ') do (
  set /a "fullList =%%i"
  set "compare_%%i=%%j"
  set "NAME=%%j"
  setlocal enabledelayedexpansion
  %strLen(var):var=NAME%
  for /f "tokens=1,2" %%L in ("!len! !stringMax!") do (
    endlocal
    if NOT defined stringMin set /a "stringMin =%%L"
    if %%M lss %%L set /a "stringMax =%%L"
  )
)
set "checkBegin="
setlocal enabledelayedexpansion
for /L %%B in (1 1 %stringMax%) do (
  for /L %%A in (1 1 %fullList%) do (
    if not defined compareX_%%B set "compareX_%%B=!compare_%%A:~-%%B,1!"
    (call;)
    if !compareX_%%B! neq !compare_%%A:~-%%B^,1! (call)
    if !compareOffset_%%B! == 1 (call)
    if errorlevel 1 (
      if NOT defined compareBegin set "compareBegin=%%B"
      if NOT defined compareOffset_%%B set "compareEND=%%B"
      set "compareOffset_%%B=1"
      if NOT defined compareOffsetBegin_%%B (
        set "compareOffsetBegin_%%B=!compareX_%%B!"
        set "checkBegin=!compareX_%%B!!checkBegin!"
      )
      set "checkEND="
      for /L %%C in (!compareBegin! 1 %%B) do (
        set "compareOffsetEND_%%C=!compare_%%A:~-%%C,1!"
        set "checkEND=!compare_%%A:~-%%C,1!!checkEND!"
      )
    )
  )
)

set /a "compareBegin=-compareBegin+1"
if %compareBegin% == 0 set "compareBegin=,"
if %compareEND% == 0 (set "compareEND=") else set "compareEND=,-%compareEND%"
set "fail="
for /L %%B in (1%checkBegin% 1 1%checkEND%) do (
  set "compareNR=%%B"
  set "compareNR=!compareNR:~1!"
  for %%A in ("!NAME:~%compareEND%!!compareNR!!NAME:~%compareBegin%!") do (
    if defined %%~A (
      echo !%%~A!
    ) else (
      >&2 echo --- Dateiversion %%A nicht gefunden!
      set /a fail+=1
    )
  )
)
if defined fail (
  echo %fail% Fehler im Backup
  (call)
)
cmd /k

exit /b

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:setAllMacros
:: define LF as a Line Feed (newline) character
set ^"LF=^

^" Above empty line is required - do not remove

:: define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:strLen.var
@for %%T in ("%temp%\%~n0.tmp.cmd") do @(
 @ >%%T (
  echo(@set strLen(var^)=(%%\n%%
  echo( set "str=A!var!"%%\n%%
  echo( set "len=0"%%\n%%
  for /l %%i in (12 -1 0) do @(
   echo( set /a "len|=1<<%%i"%%\n%%
   echo( for %%%%# in (!len!^) do if .!str:~%%%%#^^^^^^,1!==. set /a "len&=~1<<%%i"%%\n%%
  )
  echo(^)
 )
 call %%T
 del %%T
)
set "LF="
set "\n="

Probably are still a few variable too much or useless in the code - I have still let the try out.

Phil

Post Reply