![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Quote: > will always be in column A. I would like it to 1) open the csv 2) make Quote: > changes 3) save and close the csv. 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 Specs![]() |
| | #3 (permalink) |
| | 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 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 Quote: >> will always be in column A. I would like it to 1) open the csv 2) make 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 Specs![]() |
| | #4 (permalink) |
| | 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 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 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 Quote: > >> will always be in column A. I would like it to 1) open the csv 2) make 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 specific date in a date format .csv. how can I automate it? 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 Specs![]() |
| | #5 (permalink) |
| | 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 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 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 Specs![]() |
| | #6 (permalink) |
| vista | Re: batch file / script to replace information Hi James Brister : Your requirements are Quote: Code:
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
Now, you mentioned the format of the csv file names is Quote: mm-dd-yyyy.csv 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 Specs![]() |
![]() |
| 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 | |||