Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 23rd, 2024, 8:15 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Changing Program Files to Progra~1
PostPosted: August 18th, 2007, 7:12 pm 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi,

I am trying to write a safe *.txt delete function. :?

My idea is to write a script to create a text file containing a list of all the text files starting with a "u"in a directory. (u*.txt)
Then loop through this file to delete all the text files in this directory.

This works fine, as long as SYSTEM_PUBLICATION_DIR, is not in the Program Files directory (where I want to to be).

My problem is that a dos batch file does not like using Program Files as a path, but wants Progra~1 instead. (This applies to My Documents folder as well)

I thought that the following script would work, but it gives me an error:
"Action (15, 0) : Function Not Found : Check spelling and capitalisation"

What am I doing wrong?
Regards
Neil

Code:
var wbat;
var wwbat;
var mybText;
var foundText;

wwbat = OpenFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.txt",true)
       //create a new file ulist.txt
wwbat.WriteLine("dir /b "+SYSTEM_PUBLICATION_DIR + "\Students\\"+Student+ "\\u*.txt > "+SYSTEM_PUBLICATION_DIR + "\Students\\"+Student+ "\\ylist.txt")
       //puts the dos dir command into the file to redirect the output to a file called ylist.txt
       //this will output dir /b c:\Program Files\Test\Students\12345678\u*.txt > c:\Program Files\Test\Students\12345678\ylist.txt
       // won't work because Program Files needs to be Progra~1
mybText = wwbat.ReadLine()
       //reads the line just written, into var mybText
wwbat.Close()
mybText.SetSelection(0,-1)
       //selects the entire mybText to check for existance of "Program Files"

Debug.trace(mybText)

foundText = wwbat.FindTextInSelection("Program Files",0)
if (foundText != -1)
           // if "Progam Files" is found, get start/end indexes
   {
    mybText.SetSelection(foundText,(foundText + 13))
           //sets start/end indexes (index in var foundText and index of foundText +13 (length of charactures in "Program Files"))
    mybText.ReplaceSelection("Progra~1");
           // replace "Program Files" with "Progra~1" in var mybText
}

wbat = OpenFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.txt",true)
       //following lines write the contents of the batch file to ulist.txt, then renames ulist.txt to ulist.bat
wbat.GotoFirstLine()
wbat.WriteLine("echo on")
wbat.GotoNextLine();
wbat.WriteLine(mybText)
wbat.Close()
wait(1)
CopyFile( SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.txt",SYSTEM_PUBLICATION_DIR + "\Students\\"+Student+ "\\ulist.bat")
wait(1)
LaunchFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.bat")
       //runs newly create batch file to generate a list all the u*.txt files to be looped for deletion in next function


_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
 Post subject:
PostPosted: August 18th, 2007, 10:30 pm 
Offline

Joined: October 26th, 2004, 10:23 am
Posts: 666
Location: Digital Workshop
When writing the paths to the batch file, make sure to wrap them in quotes ", then long pathnames will work. Eg.

Code:
wwbat.WriteLine("dir /b \"" + SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\u*.txt\" > \"" + SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\ylist.txt\"" )

_________________
ddww Opus Developer


Top
 Profile Visit website  
 
 Post subject:
PostPosted: August 19th, 2007, 12:58 am 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi Duncan,

Thanks, that worked. :)

I copied various scripts from the forum and tried to implement them to delete the files from the text, but have not managed to succeed yet.

Please let me know what I am doing wrong. I suspect that the "while" command that I used from an example on the forum, should be a "for" command.

I just seem to have a mental block against for commands... :oops:

Regards

Neil

Code:
var wbat;
wbat = OpenFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.txt",true)
wbat.GotoFirstLine()
wbat.WriteLine("echo on")
wbat.GotoNextLine();
wbat.WriteLine("dir /b \"" + SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\u*.txt\" > \"" + SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\ylist.txt\"" )
wbat.Close()
wait(1)
CopyFile( SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.txt",SYSTEM_PUBLICATION_DIR + "\Students\\"+Student+ "\\ulist.bat")
wait(1)
LaunchFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.bat")
wait(1)
var ylist = OpenFile(SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\ylist.txt")
   while(!ylist.EndOfFile())
   {
   var delfile = ylist.ReadLine();
   DeleteFile(delfile) ;
}

_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
 Post subject:
PostPosted: August 19th, 2007, 12:00 pm 
Offline

Joined: October 26th, 2004, 10:23 am
Posts: 666
Location: Digital Workshop
There are a couple of minor problems that could be improved and one bug in your code.

The problems:
You do not need to use GotoFirstLine or GotoNextLine when writing to the file - each line will be added to the end.
You can create the batch file directly; you do not need an intermediate step of creating the text version and renaming.

The bug (which prevents it from working):
The DeleteFile() line is attempting to delete the file with no path information ('delfile' contains only the name). You need to add the full path to make it work as desired.

Code:
var wbat;
wbat = OpenFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.bat",true)
wbat.WriteLine("dir /b \"" + SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\u*.txt\" > \"" + SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\ylist.txt\"" )
wbat.Close()
wait(1)
LaunchFile(SYSTEM_PUBLICATION_DIR + "\\Students\\"+Student+ "\\ulist.bat")
wait(1)
var ylist = OpenFile(SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\ylist.txt")
while(!ylist.EndOfFile())
{
   var delfile = ylist.ReadLine();
   DeleteFile( SYSTEM_PUBLICATION_DIR + "\Students\\" + Student + "\\" + delfile );
}

_________________
ddww Opus Developer


Top
 Profile Visit website  
 
 Post subject: Deleting TXT files in a directory - Thanks
PostPosted: August 19th, 2007, 12:41 pm 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi Duncan,

Thanks, another step solved in my project.

I really appreciate your help.

Attached is a zip file with the imp, basic directory structure and some sample txt files for anyone, who wants to look at this. (Use at your own risk.)

It only deletes txt files with a "u" prefix in a dynamic directory, selected by the variable "Student".

Regards
Neil


You do not have the required permissions to view the files attached to this post.

_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 35 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group