Batch Script to compare data

Multi tool use
Multi tool use


Batch Script to compare data



Every day in my team a group of people extract a report from a portal and name it like ABC_23MAY2018.txt,XYZ_23MAY2018.txt and save it in a directory.


ABC_23MAY2018.txt


XYZ_23MAY2018.txt



But because it is a manual process people do mistake like they extract the report of 22MAY2018 (giving the wrong date on the portal input) and name it ABC_23MAY2018.txt etc.
This has caused of lots of issues as the file is used for reports purposes. I was thinking to create a batch script which will extract the date from the file name -23MAY2018 and compare it with the date which will be extracted in another file date.txt. I have figured out to extract the date from file name :


22MAY2018


ABC_23MAY2018.txt


23MAY2018


set Filedate=ABC*.csv
for /f %%A in ('dir /b %Filedate%') do (
set filename=%%~A
set fileCSV=!filename:~9,10!
echo !filename:~9,10!
)



But how to compare it with the date in the file. for example, after extracting the date from filename its need to be compared it with the data.txt data which have value like :


ABC,23MAY2018
XYZ,23MAY2018



so the date extracted from ABC_23MAY2018.txt need to be compared with the first line and the second column have the date. If they are equal move the file in another directory.


ABC_23MAY2018.txt



How do I achieve it? Hope I explained my problem well.





Why do you want to do this with a batch file? Pretty much any other programming language would make this easier.
– melpomene
Jul 2 at 0:56





true..i have already built some validation in bach script...so wanted to do the same for this ...but other scripting languages suggestion is welcome.
– ty139
Jul 2 at 1:35




1 Answer
1



I 'd use.


%%A


_


%%B


%%C


"Name,Date"



Technically the two for, the findstr and move could be on one line,
but that's more difficult to follow.


@Echo off
set "Filedate=ABC*.csv"
set "Destfolder=X:whereever"
for %%A in (%Filedate%) do (
for /f "tokens=1*delims=_" %%B in ("%%~nA") Do (
findstr /IBELC:"%%B,%%C" date.txt 2>&1>NUL && Move "%%A" "%DestFolder%"
)
)





Hey Thx ..this works..can u please explain this part 2>&1>NUL
– ty139
Jul 2 at 19:55





if findstr would have a find it would echo it via normal output stream (1), possible error message to the error stream( 2). Both streams are redirected to nul with that command first 2>&1 is merged with normal output, the >Nul. We don't need the output just the conditional execution on success && of the next command.
– LotPings
Jul 2 at 20:04



2>&1


>Nul


&&






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

AFBhMB7,glJwzpWx1uu,D,c9YkoTgCs5TBg4LfxKLYG1,5HmiFvOUL2q3UwwoP,kgN,P,XEavC,CRG7W1DDEimtw7VE2MRZ
E8foefNxBbo bBldCf8i 3xQARin7Ik4AEnAZIMrIE4KEO0JuBra

Popular posts from this blog

Rothschild family

Cinema of Italy