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
bhavani vAsked:
Who is Participating?
 
Bill PrewCommented:
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
 
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
 
bhavani 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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
bhavani 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
 
bhavani 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 PrewCommented:
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 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:
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
 
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
 
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??

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 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 from two folders??
0
 
Bill PrewCommented:
The same logic will work, just change:

    del "%~1\%OldestFile%"

to:

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


»bp
0
 
bhavani 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 PrewCommented:
MKDIR is used to create a new folder, yes.


»bp
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.