Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - batch file / script to replace information

Reply
 
Old 01-29-2009   #1 (permalink)
James Brister


 
 

batch file / script to replace information

I was trying to setup a batch file (.bat or .cmd, etc.) to go into an excel
cvs file and replace text. the cvs file will always be in the same
directory but the file name will change. it will always have a date.csv
name in the following format: mm-dd-yyyy.cvs (i.e. 01-22-2009.cvs). is it
possible to do this? I would like to replace ".tif" to ".pdf". The changes
will always be in column A. I would like it to 1) open the csv 2) make the
changes 3) save and close the csv.



My System SpecsSystem Spec
Old 01-29-2009   #2 (permalink)
Todd Vargo


 
 

Re: batch file / script to replace information

"James Brister" wrote:
Quote:

> I was trying to setup a batch file (.bat or .cmd, etc.) to go into an
excel
Quote:

> cvs file and replace text. the cvs file will always be in the same
> directory but the file name will change. it will always have a date.csv
> name in the following format: mm-dd-yyyy.cvs (i.e. 01-22-2009.cvs). is it
> possible to do this? I would like to replace ".tif" to ".pdf". The
changes
Quote:

> will always be in column A. I would like it to 1) open the csv 2) make
the
Quote:

> changes 3) save and close the csv.
Open the file in Excel and select column A. Then in main menu, click on Edit
then Replace. Type .tif in the Find What field and .pdf in the Replace With
field and press the Replace All button. Only cells in column A will be
modified.

Does 01-22-2009.cvs refer to today's date?
Some specified number of days ago?
Or some unspecified random date?

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 01-30-2009   #3 (permalink)
James


 
 

Re: batch file / script to replace information

the 01-22-2009.cvs refers to today date. All files generated will be for
the specific date in a date format .csv. how can I automate it?


"Todd Vargo" <tlvargo@xxxxxx> wrote in message
news:eSRVhXpgJHA.1248@xxxxxx
Quote:

> "James Brister" wrote:
Quote:

>> I was trying to setup a batch file (.bat or .cmd, etc.) to go into an
> excel
Quote:

>> cvs file and replace text. the cvs file will always be in the same
>> directory but the file name will change. it will always have a date.csv
>> name in the following format: mm-dd-yyyy.cvs (i.e. 01-22-2009.cvs). is
>> it
>> possible to do this? I would like to replace ".tif" to ".pdf". The
> changes
Quote:

>> will always be in column A. I would like it to 1) open the csv 2) make
> the
Quote:

>> changes 3) save and close the csv.
>
> Open the file in Excel and select column A. Then in main menu, click on
> Edit
> then Replace. Type .tif in the Find What field and .pdf in the Replace
> With
> field and press the Replace All button. Only cells in column A will be
> modified.
>
> Does 01-22-2009.cvs refer to today's date?
> Some specified number of days ago?
> Or some unspecified random date?
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>

My System SpecsSystem Spec
Old 01-30-2009   #4 (permalink)
Todd Vargo


 
 

Re: batch file / script to replace information

James wrote:
Quote:

> Todd Vargo wrote:
Quote:

> > "James Brister" wrote:
Quote:

> >> I was trying to setup a batch file (.bat or .cmd, etc.) to go into an
> > excel
Quote:

> >> cvs file and replace text. the cvs file will always be in the same
> >> directory but the file name will change. it will always have a
date.csv
Quote:
Quote:
Quote:

> >> name in the following format: mm-dd-yyyy.cvs (i.e. 01-22-2009.cvs). is
> >> it
> >> possible to do this? I would like to replace ".tif" to ".pdf". The
> > changes
Quote:

> >> will always be in column A. I would like it to 1) open the csv 2) make
> > the
Quote:

> >> changes 3) save and close the csv.
> >
> > Open the file in Excel and select column A. Then in main menu, click on
> > Edit
> > then Replace. Type .tif in the Find What field and .pdf in the Replace
> > With
> > field and press the Replace All button. Only cells in column A will be
> > modified.
> >
> > Does 01-22-2009.cvs refer to today's date?
> > Some specified number of days ago?
> > Or some unspecified random date?
> >
> the 01-22-2009.cvs refers to today date. All files generated will be for
> the specific date in a date format .csv. how can I automate it?
This VBScript wont open Excel but it will replace any .tif before the first
comma. Try it on a test file to see if the result is what you want. HTH

'RenameTif2Pif.vbs
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, Text
Set fso = CreateObject("Scripting.FileSystemObject")
d = Date
todaysfile = Right(0 & Month(d), 2) & "-" & _
Right(0 & Day(d), 2) & "-" & Year(d) & ".csv"

If Not fso.FileExists(todaysfile) Then
MsgBox todaysfile & " does not exist", vbCritical
Wscript.Quit
End If

Set f = fso.OpenTextFile(todaysfile, ForReading)
Do While Not f.AtEndOfStream
s = f.ReadLine
comma = InStr(s, ",") 'get position of first comma
If comma Then
s1 = Left(s, comma - 1): s2 = Mid(s, comma)
If InStr(s1, ".tif") Then
count = count + 1
s1 = Replace(s1, ".tif", ".pdf")
s = s1 & s2
End If
End If
txt = txt & s & vbCRLF
Loop

Set f = fso.OpenTextFile(todaysfile, ForWriting)
f.Write txt
If count Then
MsgBox count & " .tif to .pif replacements made."
Else
MsgBox "No replacements needed."
End If

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 02-02-2009   #5 (permalink)
James


 
 

Re: batch file / script to replace information

Works Great!! Thanks.


"Todd Vargo" <tlvargo@xxxxxx> wrote in message
news:et20sDzgJHA.448@xxxxxx
Quote:

> James wrote:
Quote:

>> Todd Vargo wrote:
Quote:

>> > "James Brister" wrote:
>> >> I was trying to setup a batch file (.bat or .cmd, etc.) to go into an
>> > excel
>> >> cvs file and replace text. the cvs file will always be in the same
>> >> directory but the file name will change. it will always have a
> date.csv
Quote:
Quote:

>> >> name in the following format: mm-dd-yyyy.cvs (i.e. 01-22-2009.cvs).
>> >> is
>> >> it
>> >> possible to do this? I would like to replace ".tif" to ".pdf". The
>> > changes
>> >> will always be in column A. I would like it to 1) open the csv 2)
>> >> make
>> > the
>> >> changes 3) save and close the csv.
>> >
>> > Open the file in Excel and select column A. Then in main menu, click on
>> > Edit
>> > then Replace. Type .tif in the Find What field and .pdf in the Replace
>> > With
>> > field and press the Replace All button. Only cells in column A will be
>> > modified.
>> >
>> > Does 01-22-2009.cvs refer to today's date?
>> > Some specified number of days ago?
>> > Or some unspecified random date?
>> >
>> the 01-22-2009.cvs refers to today date. All files generated will be for
>> the specific date in a date format .csv. how can I automate it?
>
> This VBScript wont open Excel but it will replace any .tif before the
> first
> comma. Try it on a test file to see if the result is what you want. HTH
>
> 'RenameTif2Pif.vbs
> Const ForReading = 1, ForWriting = 2, ForAppending = 8
> Dim fso, f, Text
> Set fso = CreateObject("Scripting.FileSystemObject")
> d = Date
> todaysfile = Right(0 & Month(d), 2) & "-" & _
> Right(0 & Day(d), 2) & "-" & Year(d) & ".csv"
>
> If Not fso.FileExists(todaysfile) Then
> MsgBox todaysfile & " does not exist", vbCritical
> Wscript.Quit
> End If
>
> Set f = fso.OpenTextFile(todaysfile, ForReading)
> Do While Not f.AtEndOfStream
> s = f.ReadLine
> comma = InStr(s, ",") 'get position of first comma
> If comma Then
> s1 = Left(s, comma - 1): s2 = Mid(s, comma)
> If InStr(s1, ".tif") Then
> count = count + 1
> s1 = Replace(s1, ".tif", ".pdf")
> s = s1 & s2
> End If
> End If
> txt = txt & s & vbCRLF
> Loop
>
> Set f = fso.OpenTextFile(todaysfile, ForWriting)
> f.Write txt
> If count Then
> MsgBox count & " .tif to .pif replacements made."
> Else
> MsgBox "No replacements needed."
> End If
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>

My System SpecsSystem Spec
Old 03-28-2009   #6 (permalink)


vista
 
 

Re: batch file / script to replace information

Hi James Brister :

Your requirements are
Quote:
Code:
 
 
  1. Go to the directory where .csv (comma separated values) files are stored .
  2. For each .csv file, do steps 3-5 .
  3. Open file. For each row, do step 4 .
  4. If column 1 has ".tif", change it to ".pdf" .
  5. Write file back.
I wrote a quick script for you using biterscripting.

Here is the script.

Code:
 
cd "the directory with .csv files"
 
set $wsep = ","
# Get a list of files
var str filelist ; find "*.csv" > $filelist
while ($filelist <> "")
do
    # Get the next file. Read it into a variable.
    var str file, data, altered_data ; lex "1" $filelist > $file ; cat $file > $data
 
    while ($data <> "")
    do
        # Get the next row of data.
        var str row ; lex -e "1" $data > $row
 
        # Get the first column.
        var str column ; wex -e "1" $row > $column
        # If a .tif is present, alter it to .pdf.
        sal "^.tif^" ".pdf" $column > null
        # Insert column 1 back into row
        set $row = $column + "," + $row ; echo ($row+"\n") >> $altered_data
    done
 
    # The altered data is in $altered_data. Write it back to file.
    echo $altered_data > { echo $file }
done
If you don't have biterscripting, download it free by following directions at http://www.biterscripting.com/install.html . It is very good for automating csv and spreadsheet files.

Now, you mentioned the format of the csv file names is

Quote:
mm-dd-yyyy.csv
If you need only to process TODAY'S csv file, change the find command in the above script as follows.

Code:
 
# Get today's time, mm, dd and yyyy
var str mm, dd, yyyy, time ; set $time = gettime()
set $yyyy = { chex "4]" $time }
set $mm = { chex "2]" $time }
set $dd = { chex "2]" $time }
find ($yyyy+"-"+$mm+"-"+$dd+".csv") > $filelist
# In this case the while loop for $filelist is unnecessary,
# since there is only one file in $filelist.




Sen
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How can I play a Wav file from batch/cmd script? Vista performance & maintenance
Help with a Batch file script (FTP) Vista General
Running batch file from Powershell script PowerShell
How to run PS script with parameters from a batch file PowerShell
How to run PS script with parameters from cmd batch file? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46