forfiles. How to parse @fdate and use that as part of name

April 29, 2020 at 10:22:39
Specs: Windows Server 2008 R2 Enterpise

I need to store these with the date in the filenames. They are different formats so, I think the best thing would be to store the filename with the date modified @fdate but it's in a wierd format. Help?

forfiles -p "D:\path\logs" /D -10 /C "cmd /c if @isdir==FALSE ^0x22C:\Program^ Files\7-Zip\7z.exe^0x22 a -tzip ^0x22D:\path\logs\Archive\%today%_archive.zip^0x22 @path -sdel"

forfiles /P "D:\path\logs\Archive" /D -20 /C "cmd /c if @isdir==FALSE del /q @path"


See More: forfiles. How to parse @fdate and use that as part of name

Reply ↓  Report •

#1
April 30, 2020 at 20:01:21
Maybe its just me, but I don't understand much of this. Where does "%today%" come from? (unless it's a win-server thing). There's no reference to filedate "@fdate" in the /c.
Maybe a couple examples...

Reply ↓  Report •

#2
May 1, 2020 at 06:43:15
Pardon, I cut the top off. No I haven't tried to do @fdate right now because the output isn't really good for a file name, the way it displays by default. 6/12/2015

I should note, I found this

for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x

_____
@fdate Date the file was last modified, in the localized date format of the current user.
___

anyone know how to change "localized date format of the current user." then use the resulting @fdate as part of the filename string?


ECHO OFF

for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x

set today=%Year%%Month%%Day%


forfiles -p "D:\path\logs" /D -10 /C "cmd /c if @isdir==FALSE ^0x22C:\Program^ Files\7-Zip\7z.exe^0x22 a -tzip ^0x22D:\path\logs\Archive\%today%_archive.zip^0x22 @path -sdel"

forfiles /P "D:\path\logs\Archive" /D -20 /C "cmd /c if @isdir==FALSE del /q @path"

message edited by doc1623


Reply ↓  Report •

#3
May 1, 2020 at 13:35:03
Well, date formats do differ across systems, but if perfect robustity is not required, just a sample of your target/intended system date format should do the trick.
F/E, mine is like: Fri 05/01/2020 To convert that, all you need is this line:
for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set today=%%c%%a%%b
in your script. Just "echo %date%" at your command-prompt to check your current format. The wmic thing could work, but it doesn't zero-pad the elements, so you have ambiguity and duplicate filenames: 1112020 would occur for both November 1st and January 11th.

Reply ↓  Report •

Related Solutions

#4
May 1, 2020 at 16:01:51
My script, with the stolen bit, does find for the current day but what I need to parse is that @fdate for the file modification date and put that in the file name on the same line as the forfiles.

I appreciate the input, but is there an environmental variable for ---'in the localized date format of the current user. '

That's part of the definition of that variable at least on the forfiles wikipedia page


Reply ↓  Report •

#5
May 5, 2020 at 02:33:29
I'll try and make it more clear. These are log files and they are being cleaned. The script works but the archive is named the day they are zipped not the day of the log file, which is what I really want. They have the date as part of the name but they are different log files as well, so the format is not exactly standard. I think the modification date variable would work but I'm not sure how to implimeant that.

Reply ↓  Report •

#6
May 5, 2020 at 11:47:41
Maybe embed a batch-script into the forfiles command, giving you the tools of both.
forfiles @fdate format sucks, but forfiles does the date-math, batch's date-math sucks, but it handles date format better. Here's a sample.batch f/e 'archive.bat':

@echo off & setlocal
rem %1 is the filename submitted by forfiles when it invokes archive.bat
set x=%~t1
for /f "tokens=1-3 delims=/ " %%a in ("%x%") do set dt=%%c%%a%%b
"C:\Program^ Files\7-Zip\7z.exe" a -tzip "D:\path\logs\Archive\%dt%_archive.zip" %1
exit
::--------------- end batch

(I wasn't sure what this does:
@path -sdel"
but it can be added to the batch if put in the correct syntax)
So:
forfiles /P "D:\path\logs" /D -10 /C "cmd /c if @isdir==FALSE archive.bat @file"


Reply ↓  Report •

#7
May 5, 2020 at 12:00:20
I'll have to try that thank you.

I'm more of a linux guy so this is driving me a bit nuts :). Thankfully, I haven't looked in a couple of days.

I would have thought there would be an easier way, but I sure don't know it. The mix maybe the best option.


P.S.

The -sdel deletes the files after they are compressed. 7zip flag

"-sdel (Delete files after compression) switch"

message edited by doc1623


Reply ↓  Report •

Ask Question