Rename to %datefromnumbervalue% -1

A swapping-ground for Regular Expression syntax

Rename to %datefromnumbervalue% -1

Postby fezzikjr » Tue Jan 20, 2009 10:08 pm

Greetings and Salutations!

So, I have a .bat file which copies three files (*_nc.csv, *_tx.csv, & *_west.csv) to a few corresponding sub-folders (\NC, \TX, & \West, respectively). When it copies the files, it appends " - yyyymmdd" (where yyyymmdd = %today%) to the end of the filename (without the quotes, with the spaces & before the .csv extension). This process runs at noon, whether or not I'm logged into the computer.

Unfortunately, I have not been able to find a successful way to have my batch file calculate the date for %yesterday%. As I understand it, without tons of code, it will calculate the day before the 1st of the month as 0; of course it should be something like 20081231, not 20090100.

Below is an excerpt of the .bat code:
Code: Select all
cls
ECHO OFF
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
Set All=%%C%%A%%B
)
@For %%a in ("\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_nc*.csv") do copy %%a "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\NCA\%%~na - %All%.csv"
@For %%a in ("\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_tx*.csv") do copy %%a "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\TX\%%~na - %All%.csv"
@For %%a in ("\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_west*.csv") do copy %%a "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\West\%%~na - %All%.csv"
REM PAUSE


After multiple failed attemps to calculate %today%-1 (or rather, %yesterday%), I turned to the Bulk Rename Utility found here. Having used it before (to rename a batch of scanned images at once, with a sequential numbering system), I just *knew* there had to be a way to do what I'm wanting to do.

By the way, KUDOS to the developer on this app. It's highly customizable and powerful.

I found the option to use Century dating, and to offset by -24. However, I'm wondering if there is a way to have BRU look at the date value of the end of the file name, and remove 1 day?

Edit:
An example of the output filenames would be: "abcdefghij_nc - 20090101.csv", "abcdefghij_tx - 20090101.csv", & "abcdefghij_west - 20090101.csv". These would each need to be changed to "abcdefghij_nc - 20081231.csv", "abcdefghij_tx - 20081231.csv", & "abcdefghij_west - 20081231.csv".

Also, I'd like to find some way to script this. I believe that the BRU-CLI will handle this, but unfortunately the syntax is a bit too complicated.

Many thanks to any that can help! :)

Sincerely,

RMason
FezzikJr
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby fezzikjr » Sat Jan 31, 2009 6:34 am

:?: Anyone got an idea?
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby GMA » Sat Jan 31, 2009 8:40 am

Hi, FezzikJr:
No, BRU doesn't have such a function. Since you ask for an idea, I'll tell you the closest thing I can think of:

1) Change the original creation date of your files to -1,
2) Remove the wrong date in the FILE NAME,
3) Add the right date to the file name.

The steps:

1) Go to Options > Timestamp. Set "Date Created" to "Delta > 1 0 0 0". Check "Negative", press OK.

2) Set the RegEx (1) field to:
MATCH: (.*) - ([0-9]*)
REPLACE: \1 -
(There's a space after the "-" in REPLACE)

3) Set Auto Date (8) to:
Mode: Suffix
Type: Creation (New)
Fmt: YMD
Check "Cent"

4) Rename.

Of course, this method involves changing the original creation date of your files, and I don't know if that's an option for you.
Best regards,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: Rename to %datefromnumbervalue% -1

Postby fezzikjr » Mon Feb 02, 2009 9:05 pm

GMA wrote:Of course, this method involves changing the original creation date of your files, and I don't know if that's an option for you.

Thanks for the response, Gabriel! Unfortunately, changing the original creation date of the source files is not an option. However, I believe there may be a way to change it from the 'attrib' command in the DOS batch script...?
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby GMA » Thu Feb 05, 2009 1:14 pm

Sorry, I can't think of anything else. But regarding the "attribute" command, isn't that used to specify "Read only", "Hidden", "System"...? I might be wrong, but I don't think that's the command you need for your present problem.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: Rename to %datefromnumbervalue% -1

Postby fezzikjr » Thu Feb 05, 2009 4:37 pm

You are correct, Gabriel; the 'Attrib' command is only used for changing the read-only, system, and hidden properties of a file. I did some more searching, and found some freeware/shareware programs online that will change the create date, but I am hesitant to download/install yet another program. I guess for now I will keep changing the filename(s) manually a few times a week.

Thanks for the help, Gabriel! :D
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby fezzikjr » Fri Feb 06, 2009 10:16 pm

GMA wrote:Sorry, I can't think of anything else. But regarding the "attribute" command, isn't that used to specify "Read only", "Hidden", "System"...? I might be wrong, but I don't think that's the command you need for your present problem.

Thanks again for your help, Gabriel. :-)

Just had one more thought on this...
The files *do* have a last modified date. The last modified date value is +1 of what I need it to be...

In essence, if the last modified date is 02/05/09, I need it to read as '%filename% - 20090204.csv'

Any thoughts/workarounds with this information?
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby fezzikjr » Fri Feb 06, 2009 10:51 pm

Gabriel -

My apologies for not following through with your original instructions. I was under the impression that I needed to modify the 'create' date prior to going through those steps... :roll:

Okay... So, I used the 'modified' rather than the 'created' section under 'timestamp', and it seems to be producing the actual results I need.

However, there is one (likely small) problem. Multiple files (up to ±31; one for each day of the month) will exist in each folder that will be processed. The actual number varies on how quickly I move them from one folder to their appropriate month's folder. As they are getting processed, it is modifying the 'modified date', and then it will do it again the next day.

This brings me to a few questions/situations:

* How do I have it simply 'look' at the modified date, and then base the rename off of that information?

* I tried de-selecting the 'timestamp' option, and using instruction steps 2 & 3 you gave me. Instead of 'Type' = Modified (New), I used 'Type' = Modified (Current). This led me to need to decrease the offset in order for it to not change any files that were already changed... Should I change the offset by any particular amount to make sure it changes just the ones I need?

* (Hopefully) Final question... One I get everything setup, I can save the settings in BRU as a .bru file to open later. Is there a way to call the settings within a .bru file via CLI? What's the best way to do this via CLI, so I can script it in a .bat file?

Many thanks!
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby GMA » Sat Feb 07, 2009 7:15 pm

OK, let's try a different aproach. Here's a BAT file that will do all the things you need at the same time:

1. It'll copy *_nc.csv, *_tx.csv and *_west.csv to

\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\NC
\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\TX
\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\West

respectively.

2. In the same process, it'll rename those three copied files (don't worry, it won't rename any other CSV files present in the "NC", "TX" and "West" subfolders) as

\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\NC\*_nc - YYYYMMDD.csv
\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\TX\*_tx - YYYYMMDD.csv
\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\West\*_west - YYYYMMDD.csv

but USING YESTERDAY'S DATE, so you don't need to modify that afterwards :wink:

------------------------------------------

Here's the code (or you can download the BAT file):

Code: Select all
@ECHO OFF
:: Windows NT 4 or later only
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: No command line arguments required
IF NOT [%1]==[] GOTO Syntax

:: Keep variables local
SETLOCAL

:: Export registry's date format settings to a temporary file
START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"

:: Read the exported data
FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B
FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B
DEL %TEMP%.\_TEMP.REG

:: Remove quotes from exported values
SET iDate=%iDate:"=%
SET sDate=%sDate:"=%

:: Parse today's date depending on registry's local date format settings
IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
   SET LocalFormat=MM%sDate%DD%sDate%YYYY
   SET YesterLocal=%%YesterM%%%sDate%%%YesterD%%%sDate%%%YesterY%%
   SET Year=%%C
   SET Month=%%A
   SET Day=%%B
)
IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
   SET LocalFormat=DD%sDate%MM%sDate%YYYY
   SET YesterLocal=%%YesterD%%%sDate%%%YesterM%%%sDate%%%YesterY%%
   SET Year=%%C
   SET Month=%%B
   SET Day=%%A
)
IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
   SET LocalFormat=YYYY%sDate%MM%sDate%DD
   SET YesterLocal=%%YesterY%%%sDate%%%YesterM%%%sDate%%%YesterD%%
   SET Year=%%A
   SET Month=%%B
   SET Day=%%C
)

:: Remove the day of week if applicable
FOR %%A IN (%Year%)  DO SET Year=%%A
FOR %%A IN (%Month%) DO SET Month=%%A
FOR %%A IN (%Day%)   DO SET Day=%%A

:: Today's date in YYYYMMDD format
SET SortDate=%Year%%Month%%Day%

:: Today's date in local format
FOR %%A IN (%Date%) DO SET Today=%%A

:: Strip leading zero from Day
SET DayS=%Day%
IF %Day:~0,1%==0 SET DayS=%Day:~1%

:: Calculate yesterday's date
IF %DayS% EQU 1 (
   SET YesterY=%Year%
   CALL :RollMonth
) ELSE (
   SET /A YesterD=%DayS% - 1
   SET YesterM=%Month%
   SET YesterY=%Year%
)

:: Add leading zero to YesterD if necessary
IF %YesterD% LSS 10 SET YesterD=0%YesterD%

:: Yesterday's date in YYYYMMDD format
SET SortYest=%YesterY%%YesterM%%YesterD%

:: Copy the files
copy "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_nc.csv" "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\NC\*_nc - %SortYest%.csv"
copy "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_tx.csv" "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\TX\*_tx - %SortYest%.csv"
copy "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_west.csv" "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\West\*_west - %SortYest%.csv"

:: Done
ENDLOCAL
GOTO:EOF

:: * * * * * * * *  Subroutines  * * * * * * * *


:: Subroutine to get yesterday's date if today is the first day of the month
:: Thanks for Aaron M. Jones who pointed out an error in the YesterD value for Month 2
:RollMonth
IF %Month%==01 (
   SET YesterD=31
   SET YesterM=12
   SET /A YesterY = %Year% - 1
)
IF %Month%==02 (
   SET YesterD=31
   SET YesterM=01
)
IF %Month%==03 (
   SET YesterD=28
   SET YesterM=02
   CALL :LeapYear
)
IF %Month%==04 (
   SET YesterD=31
   SET YesterM=03
)
IF %Month%==05 (
   SET YesterD=30
   SET YesterM=04
)
IF %Month%==06 (
   SET YesterD=31
   SET YesterM=05
)
IF %Month%==07 (
   SET YesterD=30
   SET YesterM=06
)
IF %Month%==08 (
   SET YesterD=31
   SET YesterM=07
)
IF %Month%==09 (
   SET YesterD=31
   SET YesterM=08
)
IF %Month%==10 (
   SET YesterD=30
   SET YesterM=09
)
IF %Month%==11 (
   SET YesterD=31
   SET YesterM=10
)
IF %Month%==12 (
   SET YesterD=30
   SET YesterM=11
)
GOTO:EOF


:LeapYear
:: Subroutine to calculate if this year is a leap year, by Anthony Walters
:: Pseudocode from http://en.wikipedia.org/wiki/Leap_year:
:: if year modulo 400 is 0 then leap
::     else if year modulo 100 is 0 then no_leap
::         else if year modulo 4 is 0 then leap
::             else no_leap

SET /A mod400 = %Year% %% 400
SET /A mod100 = %Year% %% 100
SET /A mod4   = %Year% %% 4
IF %mod400% EQU 0 (
   SET YesterD=29
) ELSE (
   IF %mod100% EQU 0 (
      SET YesterD=28
   ) ELSE (
      IF %mod4% EQU 0 (
         SET YesterD=29
      ) ELSE (
         SET YesterD=28
      )
   )
)
GOTO:EOF



:Syntax
ECHO.
ECHO Yesterday.bat,  Version 2.05 for Windows NT 4 / 2000 / XP
ECHO Display today's and yesterday's date in sorted and local format
ECHO.
IF     "%OS%"=="Windows_NT" ECHO Usage:  %~n0
IF NOT "%OS%"=="Windows_NT" ECHO Usage:  %0
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Adapted for Windows XP with help from Kailash Chanduka
ECHO Local date code by Frederic Guigand and Rob van der Woude
ECHO Improved leapyear subroutine by Anthony Walters

The only thing you need to do before using the BAT is to edit this section of the code

Code: Select all
copy "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_nc.csv" "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\NC\*_nc - %SortYest%.csv"
copy "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_tx.csv" "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\TX\*_tx - %SortYest%.csv"
copy "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_west.csv" "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\West\*_west - %SortYest%.csv"

replacing "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER" with the real path.

Of course, you don't have to use your previous BAT anymore (the one from your first post, I mean), only this new one. Just test it first to make sure that everything goes as expected.
Best regards,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: Rename to %datefromnumbervalue% -1

Postby fezzikjr » Tue Feb 10, 2009 5:13 pm

Gabriel -

Your solution worked out flawlessly! Many thanks for your assistance. :D :D :D :D :D
fezzikjr
 
Posts: 8
Joined: Tue Jan 20, 2009 9:51 pm

Re: Rename to %datefromnumbervalue% -1

Postby GMA » Wed Feb 11, 2009 3:18 am

Glad to hear that ;)
Regards.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina


Return to Regular Expressions


cron