I am an extreme new comer to C++. Currently I have written a short script in VBscript to delete two specific files on a specific day. This script is set in the start up menu so that it checks for the day of the week. If the day matches the set day it deletes the two files that I need it two. The reason that I have set it up like this is due to another program that starts shortly after the computer starts and these two files need to be deleted prior to the other program starting. Where I am stuck at is assigning the day of the week to an int value and then deleting the file. Below is the code that I used in VBscript.

strComputer = "."
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
 For Each objItem in colItems
   If objItem.DayOfWeek=4 then
     objFSO.DeleteFile("C:\system folder\folder\file name")
   End if
  If objItem.DayOfWeek=4 then
    objFSO.DeleteFile("C:\system folder\folder\file name")
   End if
 Next

Yes this script deletes the files on Thursday. The computer type that I am trying to set this up does not have all of the libs for VBscript, so I am forced to learn another language. Please help.

Recommended Answers

All 11 Replies

Well, without testing, I think this would give you the day:

#include <stdio.h>
#include <time.h>
int main()
{
SYSTEMTIME st;
GetSystemTime(&st);
return 0;
}

Now, st.wday() would be the day. Just test it out to see what format it's in.

Someone else should help with the deleting.

PS: I googled it. :)

So this is what I have come up with. And it fails horribly. It compiles fine but does not do anything. Please keep in mind that I am trying to pull this together rapidly.

#include <time.h>
#include <iostream.h>
#include <stdlib.h>


int main()
 {
       time_t currentTime;

       time (&currentTime); // current time


       struct tm * ptm= localtime(&currentTime);
             int tm_sec;      // 0 to 59
             int tm_min;      // 0 to 59
             int tm_hour;     // 0 to 23
             int tm_mday;     // 1 to 31
             int tm_mon;      // 0 to 11
             int tm_year;     //
             int tm_wday;     // 0 to 6
             int tm_yday;     // 0 to 365
             int tm_isdst;    // 0 to 1
             char *tm_zone;   // time zone
             int tm_gmtoff;

       int dow;

       dow = tm_wday;

       if ( dow == 4 )
          delete ( "file to delete" );

       if ( dow == 4 )
          delete ( "file to delete" );


return 0;
}

Thanks in advance for any help

#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
	SYSTEMTIME st;
	GetSystemTime(&st);
	cout << st.wDayOfWeek;
	cin.get();
	return 0;
}

This code outputs (at my system), 2, since it's tuesday.

EDIT: Deleting:

#include <stdio.h>
remove("file.txt");

In addition to post above:

'delete' does exist as a command in C++, but is used to deallocate memory that you reserved using the 'new' command.
In your case you don't need either, but I thought it would be a good idea to explain why the program compiled in the first place.

Niek

Thank you all for all of the help. I think that I may be narrowing in on the solution. Here is the modified syntax.

#include <stdio.h>
#include <Windows.h>
#include <iostream>
#include <time>


      using namespace std;

      int main()

      {

      SYSTEMTIME st;

      GetSystemTime(&st);

      cout << st.wDayOfWeek;

     if ( st.wDayOfWeek == 4 )
          system ("del file.name");

     if ( st.wDayOfWeek == 4 )
          system ("del file.name");;

      return 0;

      }

Here is the error that is returning after I attempt to compile this.

"c:\documents and settings\wowmyname\my documents\c testing\filedelete.cpp:43: time: No such file or directory"

I did ensure that the files that I wish to delete are located in the directory where the program resides. Also is there a way to force a path name to the file?

As always, thank you for the help

You don't need to include time.

You can delete files from for example C: with this:

system("Del \\..\\..\\asdfbaowgsbnlwergsfhgkldnh.sgasgraslk");

But why don't you use remove? I don't know for sure, but I don't think it's platform dependent.

Arne,
Thank you for the information. I am wondering why I wouldn't need to include time? Please bear in mind that I am an infant in the C++ world and am not versed in which statements and commands are included in each pre-processor. Am I being redundant?

It's because SYSTEMTIME is defined in Windows.h. Do you need the program to run in other operative systems?

Thank you for your reply. I did not know that the systemtime was defined in Windows.h. This is trial by fire for me. Unfortunately I am attempting to learn as I go and do not have any defined structure to my learning. Could you recommend any learning resources that I could use to assist me?

What would the syntax for the remove command be? I have seen reference to this command in other cases. When I attempt to utilize the remove command it does not appear to be excepting it as a command. This is to run under windows.

The reason why I had included time is when I attempt to compile with out it I receive the following error.

c:\documents and settings\Mynameagain\my documents\c testing\filedelete.cpp: In function `int main()':
c:\documents and settings\Mynameagain\my documents\c testing\filedelete.cpp:59: implicit declaration of function `int system(...)'

All, thank you for all of the help. I finally have a functional program. Here is what I ended up with.

#include <stdio.h>
#include <Windows.h>
#include <iostream>
#include <time.h>


      using namespace std;

      int main()

      {

      SYSTEMTIME st;

      GetSystemTime(&st);

      cout << st.wDayOfWeek;

     if ( st.wDayOfWeek == 2 )
          remove ( "C:\\folder1\\folder2\\file.name" );

     if ( st.wDayOfWeek == 2 )
          remove ( "C:\\folder1\\folder2\\file.name" );

      return 0;

      }

Hmm.. strange how after you finish with something, you just want to start something else. I really enjoyed this and am looking forward to possibly helping out others when I learn a lot more. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.