Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find and replace inside a directory listing

19 views
Skip to first unread message

akr...@gmail.com

unread,
Feb 11, 2005, 10:50:21 PM2/11/05
to
Hello,

Here in a nutshell this is what I'm trying to do:

output a directory listing, easy enough.

dir /B > listing.txt

But, inside that listing I need to search and replace so lines that
have the .PDF extension, with two sets of dates. the end product would
go from something like this..

123456.pdf

To this..

123456,,,(tomorrow'sdate),(tomorrow'sdate + 6 days)

IE

123456,,,2/12/2005,2/18/2005

and would eventually be a .csv file.

I know there is an NT4 Resource kit that has a "munge.exe", which I
could easily accomplish what I'm trying to do, but I'd have to modify
the dates all the time. Any easier solutions? I have limited batch
scripting experience so I have a decent grasp of the concepts. Any
input would be greatly appreciated.

Adam

Timo Salmi

unread,
Feb 12, 2005, 12:21:56 AM2/12/05
to
<akr...@gmail.com> wrote:
> 123456.pdf
> To this..
> 123456,,,(tomorrow'sdate),(tomorrow'sdate + 6 days)
> IE
> 123456,,,2/12/2005,2/18/2005

The tricky part is getting the "tomorrow'sdate + 6 days" into an
environment variable. The rest is rather straight standard cmd.exe
scripting.

31} How many days ago was 31.12.2003? What date was it 100 days ago?

111572 Jan 22 2005 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

Windows Command Line Interface script programming links
http://www.uwasa.fi/~ts/http/http2.html#cmdscript

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Matthias Tacke

unread,
Feb 12, 2005, 7:52:53 AM2/12/05
to
Hi Adam,
Timo is right, date math is the issue. This batch uses functions from
Ritchie Lawrence's fine batch library. http://www.commandline.co.uk

::pdf2csv.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal
call :GetDate yy mm dd
call :DateToDays %yy% %mm% %dd% Today
set /A Tomorrow=%Today%+1
set /A NextWeek=%Today%+7
call :DaysToDate %Tomorrow% yy2 mm2 dd2
call :DaysToDate %NextWeek% yy3 mm3 dd3
set Tomorrow=%mm2%/%dd2%/%yy2%
set NextWeek=%mm3%/%dd3%/%yy3%
::
for %%A in (*.pdf) do echo/%%~nA,,,%Tomorrow%,%NextWeek%
::
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Download and append these functions from Ritchies Library:
:GetDate yy mm dd
:: http://www.commandline.co.uk/lib/Batch%20Function%20Library/Date%20and%20Time%20Functions/GetDate.html
:DateToDays %yy% %mm% %dd% days
:: http://www.commandline.co.uk/lib/Batch%20Function%20Library/Date%20and%20Time%20Functions/DateToDays.html
:DaysToDate %days% yy mm dd
:: http://www.commandline.co.uk/lib/Batch%20Function%20Library/Date%20and%20Time%20Functions/DaysToDate.html


HTH

--
Gruesse Greetings Saludos Saluti Salutations
Matthias
---------+---------+---------+---------+---------+---------+---------+

Dr John Stockton

unread,
Feb 12, 2005, 10:58:31 AM2/12/05
to
JRS: In article <cuk3pk$g...@poiju.uwasa.fi>, dated Sat, 12 Feb 2005
07:21:56, seen in news:alt.msdos.batch.nt, Timo Salmi <t...@UWasa.Fi>
posted :

>
>The tricky part is getting the "tomorrow'sdate + 6 days" into an
>environment variable.

Very readily done if VBScript is used - this shows the basics


type $4.vbs
N = Now
wscript.echo "set D1=" & Int(N+1)
wscript.echo "set D6=" & Int(N+6)


cscript //nologo $4.vbs
set D1=2005-02-13
set D7=2005-02-19

so redirect cscript output to file and execute; and package the lot into
one file by the usual tricks. That will also work in Win98.


That should set the localised date; see in
<URL:http://www.merlyn.demon.co.uk/vb-dates.htm> for other specific
formats, though it should be obvious enough.


IIRC, VBScript can set the environment variables directly,

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

akr...@gmail.com

unread,
Feb 14, 2005, 8:40:12 PM2/14/05
to
Hi, Matthias

Thanks for your solution, just one question. I'll use the word
'amateur' to describe my experience with scripting however where (or
for what) does the cmd file look to search and replace through? I've
been calling my directory listings 'dir.txt' but that really has no
restriction.

Thanks again.

-A

Matthias Tacke

unread,
Feb 15, 2005, 8:34:06 AM2/15/05
to
The batch works just in the current folder and output is to screen,
you may change it by tweaking the line:

for %%A in (*.pdf) do echo/%%~nA,,,%Tomorrow%,%NextWeek%

To use a folder passed as an argument it's better to parse dir output,
and to output to a file instead of screen use rediection

replace the above line ewith these statements:

:: default to current dir, else use first argument to check for pdf'S
set folder=%~1
if NOT defined folder set folder=%cd%
:: output goes to :
set outfile=yourfile.csv
:: delete file if already present.
del /Q %Outfile% >NUL 2>&1
for /f "delims=" %%A in (
'dir /B /A-D "%folder%\*.pdf"'
) do echo/%%~nA,,,%Tomorrow%,%NextWeek% >>%Outfile%
::

akr...@gmail.com

unread,
Mar 3, 2005, 9:02:20 PM3/3/05
to
Thank you Matthias and everyone else, it works a charm.

0 new messages