View Full Version : Help writing a batch file
boss2000
08-30-2009, 06:15 PM
Hello
I am a first time user so apologise if I am posting this in the wrong area.
I know very little about writing batch files but would like help with the following...
is possible I would like to write a batch file that when it is run it will delete a specified file/files on or after a specified date
Thanks
Paul Komski
08-31-2009, 08:54 AM
The following code should do the sort of thing you want but you need to check out the date format and so forth and edit as appropriate. Post back for any clarification.
@echo off
:: THE FOLLOWING COMMANDS SHOULD INDICATE JUST HOW DATE IS FORMATTED ON THIS MACHINE
:: YOU MAY NEED TO AMEND THE THREE DATE VARIABLES TO SUIT YOUR SYSTEM
echo Today's date is %date%
echo ---
echo The four digits starting four from the end are %date:~-4,4%
echo The two digits starting seven from the end are %date:~-7,2%
echo The two digits starting ten from the end are %date:~-10,2%
echo ---
echo Thus if the date format is DD/MM/YYYY the current date can be represented as:
echo %date:~-4,4%%date:~-7,2%%date:~-10,2%
echo ---
::SET THE TWO FOLLOWING VARIABLES APPROPRIATELY LEAVING NO SPACES EACH SIDE OF THE = SIGN
::THE DATENUM VARIABLE SHOULD BE TOTALLY NUMERIC IN YYYYMMDD FORMAT
set datenum=20090831
::THE FULL PATH TO THE RELEVANT FILE (Enclosed in quotes if any spaces in the path)
set FileToDel="C:\File To Delete.txt"
echo ---
IF %date:~-4,4%%date:~-7,2%%date:~-10,2% geq %datenum% (echo The file %FileToDel% will now be deleted) ELSE goto nope
pause
IF %date:~-4,4%%date:~-7,2%%date:~-10,2% geq %datenum% del %FileToDel%
echo Command to delete %FileToDel% was issued
pause
goto exit
:nope
echo Today's date is before %datenum% so nothing was deleted
pause
:exit
boss2000
08-31-2009, 07:34 PM
Thanks Paul, I worked through it and after a bit of playing around it worked perfectly. This is my first encounter with batch files so I hope you don't mind me asking for further assistance?
If instead of using the batch file to delete a specific file is it possible to delete more than one file within a directory and subdirectories that have been created on or after a specified date.
Thanks
Paul Komski
08-31-2009, 07:48 PM
Just about any command you can issue in a command prompt window you can issue as a line in a batch file. Enter del /? at a command prompt to see the options for del but basically you can use wildcards to delete multiple files in the same directory. Thus del C:\SomeFolder\*.txt should delete all text files in the SomeFolder directory and you could use the /S switch (del /S C:\SomeFolder\*.txt) to delete all text files in the directory and all sub-directories.
Etc, etc. Post back or Google for such things as Batch+File+delete and so forth.
To include files with specific date time datestamps requires first digging out such attributes. It might be possible in batch files but would in fact be much easier with a simple bit of VBSript or straight VisualBasic.
Cuc Tu
09-10-2009, 09:33 AM
Can one execute a batch file as an automated process? For example, if I leave my PC on and want to execute at midnight when I'm sleeping.
* OK, I've rediscovered Windows Task Scheduler. But then I found this password blockade. Since I don't know the password, I guess I can't use the scheduler? BTW, this is my PC and when I installed XP Pro, I think I intentionally did not enter a PW because there is no reason for one. Or did Microsoft require me to enter one and I just forgot?
did you try leaving the password prompt blank? sometimes it asks for a password when there isn't one...
Paul Komski
09-10-2009, 02:35 PM
If your account has Administrator Status and a password has been set you may still have been able to autologon but Windows wont let you delete or reset your own password without first inputting the password as confirmation. Ironically in such circumstances I have been able to create a new Administrator account and logon to it and when logged on to that account it will let you delete or reset your normal account's password. Go figure. Admittedly there will be a loss of root certificates and so forth but you should be able to manipulate your users passwords that way. Another approach is to start up in Safe Mode and log on to the original Administrator Account, called Administrator, and do the same thing from there. No one ever seems to set a password for that original default account.
Cuc Tu
09-10-2009, 02:48 PM
I tried leaving the auto task PW blank, but was warned that it might not be able to run. Then, when it came time to run, it did not run because of a privelage issue.
Paul, I really do not remember ever entering a PW when I was installing XP. I do remember something about entering a computer name and my name, but the PC has always booted to the desktop.
I'll try this on my work machine as I am also admin and I know the password for my account. That is really where I'd use it anyway.
But the topic of PWs is of interest because I have my late grandfather's PC that I'd like to be able to use. Right now it boot straight to the desktop, but I have no passwords. It is an HP machine, so I'm not sure if there is a standard way those are set up. I'd be happy to reimage the whole thing, but there are no restore disks (although there is a partition for something). I've just left it alone because it works and I don't want to have to try and find an XP disk.
Thanks.
Paul Komski
09-10-2009, 02:53 PM
If your grandfather's pc is running xp then try logging on in Safe Mode and see if the Adminstrator account lets you in. If so you should be in clover. If no password was ever set then Scheduled Tasks may complain but should probably work. Experiment with maybe a one-liner batch file that just says
notepad
Cuc Tu
09-10-2009, 04:11 PM
Well, I'm so glad I looked into this. Our IS group has installed a myriad of tasks to run daily on my machine.
The simple task test without entering PW resulted in a "Could not start" status.
I also just thought of something...we are also required to change our PWs every three months. I suppose my scheduled tasks will need to be updated with my new PW, or is that automagic?
Paul Komski
09-11-2009, 01:48 PM
or is that automagic?
I would think it too would need updating - but PCs can be very funny things and not do what is expected.
vBulletin v3.6.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.