Page 1 of 1

VB or batch file

Posted: 20 Jan 2015 14:50
by mwatsondhs
I have a txt file that looks like this:
201501,20140101_20141231
201502,20140201_20150131
201503,20140301_20150228
201504,20140401_20150331
201505,20140501_20150430
201506,20140601_20150531
201507,20140701_20150630
201508,20140801_20150731
201509,20140901_20150831.

Would it be easier to just write a dos batch file to search this txt file or should I use a small VB application.
I am trying to automate the renaming of 55 .pdf files. The pdf files look like this when they are created: xxxxxx_2015-01-05.pdf.
xxxxxx is the name of a report contained in the pdf and 2015-01-05 is the date the pdf was created. I need to rename the files to look like this xxxxxx_20140101_20141231_20150105.pdf where the 20140101_20141231 is always the 12 month period preceding the run date.
I have never written a batch file like this and was just wondering if it would be easier to us VB.

Thanks,
Mark

Re: VB or batch file

Posted: 20 Jan 2015 16:18
by Squashman
Batch will handle this like a pro. You would end up writing more lines of code in VBscript then you would for a batch file.

Re: VB or batch file

Posted: 20 Jan 2015 19:42
by Ed Dyreen
Which language is best suited, depends on many factors: Functionality, Usability, Reliability, Performance, Supportability
VBScript is more suited for moderately complex time critical tasks, your project doesn't seem to fall into this category.

Re: VB or batch file

Posted: 20 Jan 2015 22:45
by dbenham
The easiest thing to do would be to use my JREN.BAT utility :wink: :D

JREN.BAT can easily do date computation and formatting, so there is no need for the look-up text file.

The command below has the /T option so it just prints the before and after names, without renaming. Simply remove the /T option to actually rename the files.

Code: Select all

jren "^(.*_)(\d{4})-(\d\d)-(\d\d)(\.pdf)$" "$1+($2-1)+$3+'01'+ts({dt:[$2,$3-1,0],fmt:'_{yyyy}{mm}{dd}_'})+$2+$3+$4+$5" /t /j


Dave Benham

Re: VB or batch file

Posted: 20 Jan 2015 22:47
by Squashman
I am officially naming all Hybrid Batch/Jscript to DBscript. :D

Re: VB or batch file

Posted: 20 Jan 2015 23:10
by Squashman
Just one way to do it in batch.

Code: Select all

@echo off

FOR /F "TOKENS=1-5 DELIMS=_-." %%G IN ('DIR /A-D /B *.PDF') DO (
   FOR /F "TOKENS=2 DELIMS=," %%S IN ('findstr /B "%%H%%I" ^<search.txt') DO (
      REN "%%G_%%H-%%I-%%J.%%K" "%%G_%%S_%%H%%I%%J.%%K"
   )
)

output

Code: Select all

C:\BatchFiles\renaming>dir /b *.pdf
xxxxxx_2015-01-05.pdf
xxxxxx_2015-02-05.pdf
xxxxxx_2015-03-05.pdf
xxxxxx_2015-04-05.pdf

C:\BatchFiles\renaming>renamepdf.bat

C:\BatchFiles\renaming>dir /b *.pdf
xxxxxx_20140101_20141231_20150105.pdf
xxxxxx_20140201_20150131_20150205.pdf
xxxxxx_20140301_20150228_20150305.pdf
xxxxxx_20140401_20150331_20150405.pdf

C:\BatchFiles\renaming>

Re: VB or batch file

Posted: 20 Jan 2015 23:27
by dbenham
@Squashman - Best to filter the DIR /B listing to exclude file names that don't match the template:

Code: Select all

... in('dir /a-d /b *_????-??-??.pdf^|findstr /i "_[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]\.pdf$"') do ...


Dave Benham