i have a two files in a folder. i have a take a date alone from each file and save it in a variable, then call that variable. urgent pls

i have a two files in a folder. i have a take a date alone from each file and save it in a variable, then call that variable. urgent pls
sasi vAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BillDLCommented:
You are going to have to explain your question in more detail, for example:
Which of the dates do you want from the files:  Created, Last Accessed, or Last Written?
How do you want these dates to be presented?  Space in between, on separate lines, or some other layout?

Here is a very rough example of how you can read one of the dates from each file, set them in a variable, and display them side-by-side with a space in between.  You would have to put the batch file in the same folder as the two files and replace their proper names in the batch file.

@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%
0
sasi vAuthor Commented:
its possible to get a last modified date in cmd itself or without file name its possible to do get a date of the 2 filenames?
0
sasi vAuthor Commented:
my actual issue was
PRG-P375ICA2_2018_J1_BTT_010518.zip
PRG-P375ICA2_2018_J1_BTT_011218.zip


this my two files.. i have to read only 01518 and 011218 and save it in a variable and display it in CMD.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

sasi vAuthor Commented:
@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).
0
Bill PrewIT / Software Engineering ConsultantCommented:
my actual issue was
    PRG-P375ICA2_2018_J1_BTT_010518.zip
    PRG-P375ICA2_2018_J1_BTT_011218.zip
this my two files.. i have to read only 01518 and 011218 and save it in a variable and display it in CMD.

So are you saying that you want to extract those characters from the end of the file name, rather than get the last modified date from Windows for those files?  Please try and explain better so that we can provide a solution.


»bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
sasi vAuthor Commented:
my actual issue was
    PRG-P375ICA2_2018_J1_BTT_010518.zip
    PRG-P375ICA2_2018_J1_BTT_011218.zip
this my two files.. i have to read only 01518 and 011218 and save it in a variable and display it in CMD.

So are you saying that you want to extract those characters from the end of the file name, rather than get the last modified date from Windows for those files?  Please try and explain better so that we can provide a solution.

yes prew.. i need to extract those last 6  from the end of the file name,  and store them into a 2 variables. then i need to compare both 010518 and 011218 dates and delete the old file in the folder.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, this does what you described, give it a test.

@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%"

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sasi 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 PrewIT / Software Engineering ConsultantCommented:
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
sasi 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
Bill PrewIT / Software Engineering ConsultantCommented:
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
sasi 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??

Rem PAT BTT files

Del "%BackupPath%\PAT BTT Text Files\%programName%\Old\"
echo Old File removed
Move "%BackupPath%\PAT BTT Text Files\%programName%\New\*.txt" "%BackupPath%\PAT BTT Text Files\%programName%\Old\"
copy "%ProgramPath%\%programName%\%dateFile%\*_BTT.txt" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
sasi 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 from two folders??
0
Bill PrewIT / Software Engineering ConsultantCommented:
The same logic will work, just change:

    del "%~1\%OldestFile%"

to:

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


»bp
0
sasi vAuthor Commented:
thank you,,,

while moving the files i need a create a folder then move the folder from my local to that created folder.

so i need to use mkdir??
0
Bill PrewIT / Software Engineering ConsultantCommented:
MKDIR is used to create a new folder, yes.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Bat

From novice to tech pro — start learning today.