EXTRACT A DATE FILE INSIDE A FOLDER

@echo off

FOR %%A in (FileName1.txt) DO set DATE1=%%~tA
FOR %%B in (FileName2.txt) DO set DATE2=%%~tB

set VARIABLE=%DATE1% %DATE2%

echo %VARIABLE%


THIS not work out . i am gettin filename1.txt date and time, i need inside the filename1.txt i have 2 zip files
PRG-P375ICA2_2018_J1_BTT_010518.zip
PRG-P375ICA2_2018_J1_BTT_011218.zip.
i nned that mmddyy of the zip file(010518).
bhavani vAsked:
Who is Participating?
 
Bill PrewCommented:
Okay, this does what you asked for.

@echo off
setlocal EnableDelayedExpansion

rem Clear variable to store list of file date/times
set DateList=

rem Read each line from list file
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name (date)
    set FileName=%%~nA
    set FileDate=!FileName:~-6!

    rem Add this files date/time to the list variable
    if defined DateList (
        set DateList=!DateList!,!FileDate!
    ) else (
        set DateList=!FileDate!
    )

)

rem Display the list variable
echo %DateList%

Open in new window


»bp
0
 
Bill PrewCommented:
Give this a try, assuming I understood the question correctly.

@echo off
setlocal EnableDelayedExpansion

rem Clear variable to store list of file date/times
set DateList=

rem Read each line from list file
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get access to extended attributes of each file (we want date/time)
    for %%B in ("%%~A") do (

        rem Add this files date/time to the list variable
        if defined DateList (
            set DateList=!DateList!,%%~tB
        ) else (
            set DateList=%%~tB
        )

    )

)

rem Display the list variable
echo %DateList%

Open in new window


»bp
0
 
Bill PrewCommented:
What are you trying to accomplish with this statement, I don't see the variables you reference defined?  There is most likely an easier way to get what you want, but I would need to understand better what you are trying to do.

It seems like you want to find all files that match %programName% in folder %BackupPath%, and create a list of their date/time stamps in a variable?

What are you going to do with that variable later, it could be hard to work with as just a concatenated list of dates?

And is the "=" in the below a typo?

dir /a =%BackupPath%\%programName% /b > filename1.txt

Open in new window


»bp
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Bill PrewCommented:
If just looking for the right 6 characters of the file name then this could do that.

@echo off
setlocal EnableDelayedExpansion

for %%A in ("FileName1.txt") do (
    set FileName=%%~nA
    set FileDate=!FileName:~-6!
)

echo %FileDate%

Open in new window


»bp
0
 
bhavani vAuthor Commented:
echo off
setlocal EnableDelayedExpansion

for %%A in ("FileName1.txt") do (
    set FileName=%%~nA
    set FileDate=!FileName:~-6!
)

echo %FileDate%

for this below code i am result as ename1.

but , my problem is inside the filename.txt i have 2 zip files. i

PRG-P375ICA2_2018_J1_BTT_010518.zip
PRG-P375ICA2_2018_J1_BTT_011218.zip

i need last 6 number of this 2 zip files like _010518 and _011218 as a output. i need to define a two variables the read the two zip files last 6 character and display a results
file.PNG
0
 
bhavani vAuthor Commented:
i think it will work prew.. thanks a lot. but can u explain the code if possible from here .
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name and arrange in YYMMDD format
    set FileName=%%~nA
    set FileDate=!FileName:~-2!!FileName:~-6,4!

    rem See if this is the oldest file so far, if so save it
    if "!FileDate!" LSS "!OldestDate!" (
        set OldestDate=!FileDate!
        set OldestFile=%%~A
    )

)
0
 
Bill PrewCommented:
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

The FOR /F command will read the "filename1.txt" file line by line, and assign the full content of each line to the loop variable %%A.

   rem Get right 6 characters of file name and arrange in YYMMDD format
   set FileName=%%~nA

Set the variable FileName to just the base file name (removes path, and extension)

   set FileDate=!FileName:~-2!!FileName:~-6,4!

Sets the FileDate variable to the last 6 characters of the base file name, placing the YY in front of the MMDD parts.

   rem See if this is the oldest file so far, if so save it
   if "!FileDate!" LSS "!OldestDate!" (

Compares this files date to the oldest date we have found so far.  I this files date is older then save its date and full path to variables as the oldest file.

       set OldestDate=!FileDate!

Set OldestDate variable to this files date (from file name).

       set OldestFile=%%~A

Set OldestFile variable to this files full path.
0
 
bhavani vAuthor Commented:
thanks for the explanation.

but oldest file searching in local drive only.
rem Delete the oldest file
echo Deleting oldest file "%OldestFile%"
del "%OldestFile%"

i need to delete that oldest file in some other drive, so for that what need to add in code?

this is the full code:
SET BackupPath=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles

REM mkdir %ProgramPath%\%programName%\

echo off

TITLE=%programName%

mkdir %BackupPath%

dir /a =%BackupPath%\%programName%\ /b > filename1.txt
@echo off
setlocal EnableDelayedExpansion

rem Initialize variables
set OldestDate=999999
set OldestFile=

rem Read each line from list file
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name and arrange in YYMMDD format
    set FileName=%%~nA
    set FileDate=!FileName:~-2!!FileName:~-6,4!

    rem See if this is the oldest file so far, if so save it
    if "!FileDate!" LSS "!OldestDate!" (
        set OldestDate=!FileDate!
        set OldestFile=%%~A
    )

)

rem Delete the oldest file
echo Deleting oldest file "%OldestFile%"
del "%OldestFile%"

thank you.
0
 
bhavani vAuthor Commented:
if two files means i can delete the old file from your code.
 but only one file only available for that i need a code for the echo message prew.
0
 
Bill PrewCommented:
I think you probably want one of the two below, but it's hard to know since you reference %ProgramName% in your script but I don't see it set anyplace.

echo Deleting oldest file "%BackupPath%\%programName%\%OldestFile%"
del "%BackupPath%\%programName%\%OldestFile%"

Open in new window

echo Deleting oldest file "%BackupPath%\%OldestFile%"
del "%BackupPath%\%OldestFile%"

Open in new window


»bp
0
 
bhavani vAuthor Commented:
hi prew

if i need to use call function to call the code means what i need to use for u r code??
0
 
bhavani vAuthor Commented:
hi prew
 can  u check this code?


CALL :copyFile %BackupPath%\PAT BTT Text Files\%programName%\Old\ /Q
CALL :copyFile %BackupPath%\PAT BTT Text Files\%programName%\New\*.txt %BackupPath%\PAT BTT Text Files\%programName%\Old\ /h /i /c /k /r /y
CALL :copyFile %ProgramPath%\%programName%\%dateFile%\*_stitched.txt %BackupPath%\PAT BTT Text Files\%programName%\New\ /h /i /c /k /r /y
:copyFile
echo off
if %programName%= C2_Bundle2

any wrong??
0
 
Bill PrewCommented:
Like this:

call :DeleteOldest "%BackupPath%"
exit /b


:DeleteOldest [folder-path]
    setlocal EnableDelayedExpansion

    rem Initialize variables
    set OldestDate=999999
    set OldestFile=

    rem Read each line from list file
    for /f "usebackq tokens=*" %%A in ("%~1\filename1.txt") do (

        rem Get right 6 characters of file name and arrange in YYMMDD format
        set FileName=%%~nA
        set FileDate=!FileName:~-2!!FileName:~-6,4!

        rem See if this is the oldest file so far, if so save it
        if "!FileDate!" LSS "!OldestDate!" (
            set OldestDate=!FileDate!
            set OldestFile=%%~A
        )

    )

    rem Delete the oldest file
    echo Deleting oldest file "%~1\%OldestFile%"
    del "%~1\%OldestFile%"

    exit /b

Open in new window


»bp
0
 
bhavani vAuthor Commented:
hi prew,
in two files we can delete the old files from your code.
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name and arrange in YYMMDD format
    set FileName=%%~nA
    set FileDate=!FileName:~-2!!FileName:~-6,4!

    rem See if this is the oldest file so far, if so save it
    if "!FileDate!" LSS "!OldestDate!" (
        set OldestDate=!FileDate!
        set OldestFile=%%~A
    )

)

suppose, if its a folder like 021318 and 021518 folder. how to delete the 021318 folder in bat code??
which means old folder and its files from two folders???
0
 
bhavani vAuthor Commented:
hi prew,
in two files we can delete the old files from your code.
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name and arrange in YYMMDD format
    set FileName=%%~nA
    set FileDate=!FileName:~-2!!FileName:~-6,4!

    rem See if this is the oldest file so far, if so save it
    if "!FileDate!" LSS "!OldestDate!" (
        set OldestDate=!FileDate!
        set OldestFile=%%~A
    )

)

suppose, if its a folder like 021318 and 021518 folder. how to delete the 021318 folder in bat code??
which means old folder and its files from two folders using call function??
0
 
Bill PrewCommented:
The same logic will work, just change:

    del "%~1\%OldestFile%"

to:

    rmdir /s /q "%~1\%OldestFile%"


»bp
0
 
bhavani vAuthor Commented:
i need to gather all the zip files inside the all folder then move the all zip files to another drive..
so can u help me with the code???
folder-issues.PNG
0
 
Bill PrewCommented:
I'm sorry, but you keep changing what you need, that's not how 'questions' work here on Experts Exchange.  You need to ask a single, clear question, and when you have a solution to it close that question.  Sometimes additional clarification is needed, but you seem to be asking new, or changing the original question often.  

I'll leave it to you to decide how to close this question, if a solution to your original question has been found and you want to accept it, or not in which case the question should be deleted as unsolved.

In addition I have mentioned before that you opened three of the same question.  There are still three questions open and you need to close the duplicates please.  Going forward please only open one question for the same need, not multiples.


»bp
0
 
bhavani vAuthor Commented:
prew,

original question was done here next continuation of the original code now i am doing to move all zip files. once done my bat process is finished.

i dont know the duplicates question.. wer it is and how i have to delete??
0
 
bhavani vAuthor Commented:
closed all.
0
 
bhavani vAuthor Commented:
need to gather all the zip files inside the all folder then move the all zip files to another drive in bat.
so can u help me with the code???

i tried *-*. its copying all files. i need specific zip files in all folder.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.