Search : in
By :

Batch file to output filename to csv file

Last answer on Feb 24, 2009 7:58:35 am GMT CP1717, on Dec 9, 2008 7:59:23 pm GMT 
 Report this message to moderators

Hello,

I'm looking to write a windows batch file that will look for a fixed file in a fixed folder (ie c:\folder\file1.csv) and use the dir command to output the filename and last written date to a second csv file (ie c:\folder\file2.csv). The output must be in two columns and will look something like this when you open file2.csv:

file1.csv 10/14/2008 15:38


This seems like a fairly straightforward batch, but i'm struggling to limit my output to just these two attributes and place them in the first two columns and first row of file2.csv

Any help would be greatly appreciated. Thanks!!!

Best answers for « Batch file to output filename to csv file » in :
Batch file – Get Filenames & Timestamps Show Batch file – Get Filenames & Timestamps Issue Solution Note Issue Can anyone help me create a batch file that will display all the files, subfolders and files under subfolders of folder with both date created and date modified then...
[PYTHON]Read and Write CSV files Show[PYTHON] Read and Write CSV files Prerequisite Writing a CSV file Reading a CSV file Python Www.python.org, version 2.4 supports the de facto CSV format (comma-separated values: Comma Separated Values). The Reference Library...
Inserting text in a file ShowInserting text in a file Introduction Syntax Introduction It is sometimes useful to insert text in the header of one or more document (s) or in a specific part of the document (before or after a specific line). The publisher of...
What is a batch file? ShowWhat is a batch file? Creating a batch file Prerequisites Create file Windows comes with a version of DOS, which allows you,exploiting the features and the sequence of commands in a script. Batch file, is a simple text file (ascii)...
Download Advanced Batch PDF Splitter ShowPDF Format is a reliable format to keep your documents. Most of the time, you convert several PDF documents at once or extract specific pages. To execute this process quickly, you can use Advanced Batch PDF Splitter. This program offers three...

1

Bandy, on Dec 9, 2008 9:59:25 pm GMT
  • +2

Hi CP1717,

Does the script have to be in a batch file? You might be better off using a vbs file to get the properties of the file1.csv and output them to file2.csv.

If you need the vbs script written, I should be able to come up with it for you. You can always call this script from within a batch file so it won't detract from any other scripts you already have written.

Reply to Bandy

2

CP1717, on Dec 9, 2008 10:05:43 pm GMT

Hi,

I suppose it could be a VBS script with a batch file that kicks off that script. I have never written a VBS script before though...

for the purposes of an example script, can we use the same sample names from above? i'm really clueless about VBS, but if i just need to kick it off within a batch file i'm ok on that. THanks so much for your help!!!

Reply to CP1717

3

Swarve, on Dec 9, 2008 10:37:08 pm GMT
  • +2

Hi CP1717,

The script you need is as follows:

____________________Begin Code__________________


Dim Filepath
Dim Filename
Dim OutputFile

Filepath = "c:\test\" 'Edit this to specify the filepath
Filename = "file1.csv" 'Name of file that last edited date/time is needed
OutputFile = "file2.csv" 'File to output results to

Set objFSO = CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists(FilePath & OutputFile) then
Set objFile = objFSO.CreateTextFile(Filepath & OutputFile)
Wscript.sleep(2000) 'Pause while file is being created
End If


If objFSO.FileExists(Filepath & Filename) then
Set objFile = objFSO.GetFile(Filepath & Filename)
Lastmodifieddate = objFile.DateLastModified

Const ForWriting = 2
Set outputFile = objFSO.OpenTextFile(Filepath & "file2.csv", ForWriting) ' Open file for writing
OutputFile.Write Filename & "," & objFile.DateLastModified 'Write filename and last modified date to output file
OutputFile.Close
End If
set objFSO = Nothing


____________________End Code__________________


The three variables, filepath, filename and output file that are set up on lines 5, 6 and 7 can be modified to pick up any path and filename that you specify and will create the file on line 7 on the same path.

If you save that code as test.vbs file, then in your batch file, you will need to enter the line "Call test.vbs". Obviously if you change the name of the vbs file, you will need to change the reference to it in your batch file.

Bandy (now known as Swarve since I've joined)

Reply to Swarve

4

CP1717, on Dec 9, 2008 10:47:43 pm GMT
  • +2

Wow!

Thanks so much! I can figure it out from here. You are my hero!

Reply to CP1717

5

Swarve, on Dec 9, 2008 10:55:29 pm GMT
  • +1

No problem at all, if you get any issues, let me know.

If you want to find out more about vb scripting, here are a couple of good starting points:

http://www.w3schools.com/Vbscript/default.asp
http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_­qdsc.mspx?mfr=true

Cheers,

Swarve

Reply to Swarve

7

 00666111384, on Feb 24, 2009 7:58:35 am GMT

Hi,

How can make a vbs file is it the same as creating a batch file? I wanna know how to make my html file change it extension name to .txt files with date included.

example:

ren *.html -> 02-20-2009 *.txt


Please and Thank you.

Reply to 00666111384

6

Swarve, on Dec 9, 2008 11:00:05 pm GMT

Oops, I just noticed something that I should have changed when I set up the second filename as a variable.

On line 21 (Set outputFile = objFSO.OpenTextFile(Filepath & "file2.csv", ForWriting) ' Open file for writing)

The text :
& "file2.csv",

should be changed to:
& Outputfile,

I was a little overzealous in testing it then posting unfortunately.

Cheers, Swarve

Reply to Swarve