VB or batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mwatsondhs
Posts: 9
Joined: 13 Jan 2015 09:51

VB or batch file

#1 Post by mwatsondhs » 20 Jan 2015 14:50

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: VB or batch file

#2 Post by Squashman » 20 Jan 2015 16:18

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: VB or batch file

#3 Post by Ed Dyreen » 20 Jan 2015 19:42

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: VB or batch file

#4 Post by dbenham » 20 Jan 2015 22:45

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
Last edited by dbenham on 20 Jan 2015 22:48, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: VB or batch file

#5 Post by Squashman » 20 Jan 2015 22:47

I am officially naming all Hybrid Batch/Jscript to DBscript. :D

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: VB or batch file

#6 Post by Squashman » 20 Jan 2015 23:10

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>

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: VB or batch file

#7 Post by dbenham » 20 Jan 2015 23:27

@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

Post Reply