0

I am a novice at writing scripts but I know this can be done I just don't know how...

I have a file that need to have the file NAME changed to the current date less 1 workday (ex: Mondays date back Friday, Tuesdays to Monday with a prepend of the date a static "AD"

Filename example: AD140107.pos (Tuesdays date changed to a Wednesday date, AD140108.pos) The file is located at: R:\PortfolioCenter\Output\Manual Interface Files

I have a similar file in a different folder that has a different date format that needs the date changed as well

Filename example: PC010714.slb (Tuesdays date changed to a Wednesday date, PC010814.pos) This file is located at: R:\slwin\Recon adjustments

I would like both file names to change using the same script

I know DOS doesn't do weekdays well so I can use a separate file to process Monday files if neccesary

  • This really is not a duplicate since it is asking for prior week day (mon-fri), disregarding weekend (sat,sun) – dbenham Jan 09 '14 at 23:34
  • Is there only the one file of that filetype, in each location? You don't really want Monday or Friday in the name, but the date format as shown, right? Is VBS a reasonable tool for you to use, in a batch file? It's built into Windows. – foxidrive Jan 10 '14 at 04:33

2 Answers2

0
@ECHO OFF
SETLOCAL
SET "destdir=c:\destdir"
SET "targetdir=c:\destdir"
SET today=%date:~-2%%date:~3,2%%date:~0,2%
FOR /f "delims=" %%a IN (yesterday.txt) DO SET yesterday=%%a
IF %yesterday%==%today% GOTO :EOF
>yesterday.txt ECHO %today%
ECHO REN "%destdir%\AD%today%.pos" "AD%yesterday%.pos"
SET today=%today:~-4%%today:~0,2%
SET yesterday=%yesterday:~-4%%yesterday:~0,2%
ECHO REN "%targetdir%\PC%today%.slb" "PC%yesterday%.slb"

GOTO :EOF

I use a date format of dd/mm/yyyy. Yours may be different, so you'd need to reformat the date differently. The basic method is to string together the three elements using substrings of the form %date:startpos,length% where position 0 is the start of the (date) string.

I've nominaed the two directories as destdir and targetdir to suit my system.

There's some confusion in the question about what name gets changed to what. It would seem more sensible to change from the current date to the previous day's date, but that's not what the narrative describes.

I'm assuming that you are only going to run this once per day.

It's all based around keeping the massaged last-run date in yesterday.txt, which would need to be established first containing a single line140107` or whatever. If you have this run automagically at midnight+alittle, Mon-Fri only, it should rename the files appropriately.

The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

The Batch subroutine below get the midweek date of current date minus one day and show it in the format given by its parameter: if it is Y, output format is YYMMDD, otherwise it is MMDDYY. This routine solve the main part of your request.

@echo off
setlocal

rem If Format=Y, output is YYMMDD
rem If Format=M, output is MMDDYY

:OneMidweekDayLessCurrentDate format

rem Modify next line accordingly to your locale format (this one use DD/MM/YYYY)
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A dd=1%%a-100, mm=1%%b-100, yyyy=%%c

rem Convert the Date minus one day to Julian Day Number, and get its Day Of Week (0=Sunday, ..., 6=Saturday)
set /A a=(mm-14)/12, jdn=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32076, dow=jdn%%7

rem If the dow is Sunday or Saturday, change it by the previous Monday
if %dow% equ 0 (
   set /A jdn-=2
) else if %dow% equ 6 (
   set /A jdn-=1
)

rem Convert the Julian Day Number back to date
set /A l=jdn+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447
set /A dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yyyy=100*(n-49)+i+l
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%

rem Show the resulting date
if "%1" equ "Y" (
   echo %yyyy:~-2%%mm%%dd%
) else (
    echo %mm%%dd%%yyyy:~-2%
)
exit /B

On the other hand you have NOT specified which file is the one to be renamed: "I have a file that need to have the file NAME changed... Filename example: AD140107.pos". If you specify this point, we can complete the solution.

Aacini
  • 65,180
  • 12
  • 72
  • 108