|
|
|||||||
Heres an easy one. I need a script that will delete certain files. The tricky part is the name of the files that need to be deleted will change from time to time. I need the script to look in a "delete_these_files.txt" file (stored remotly), read the names to the files to delete, then delete them. Any suggestions? |
||||||||
|
|
|||||||
Use Readline() to read the "delete_these_files.txt" |
||||||||
|
|
|||||||
readfile() UDF for each $file in readfile($filename) del $file next |
||||||||
|
|
|||||||
Thanks! I think that's what I've been looking for. I just read the syntax and I'm completely lost. I am going to be using this to clean out some old program files at login. All I need is this Kix goes to "\\remote_machine\delete_these_files.txt" reads the names of the files (sometimes 1, sometimes 100) then deletes them if they exists on the local machine |
||||||||
|
|
|||||||
I can't find readfile in the command reference |
||||||||
|
|
|||||||
You can find it in the UDF forum http://www.kixtart.org/UDF/ |
||||||||
|
|
|||||||
Thanks, I'm going to have a go at it. I think I'm in over my head!! |
||||||||
|
|
|||||||
OK, I am definitely in over my head. I'm such a noob. I don't even know how to get started on this. |
||||||||
|
|
|||||||
where do i put the path and name of the delete_these_files.txt then how to i get the names it just read into the del $files thingy |
||||||||
|
|
|||||||
Code:
|
||||||||
|
|
|||||||
I can't get it to work for each $file in readfile('\\server\c$\delete_these_files.txt') del C:\program files\$file next |
||||||||
|
|
|||||||
first would be to have the full path in the file, however... del 'C:\program files\'+$file |
||||||||
|
|
|||||||
Have you ever run a script? You create a text file and put your code in the text file. Then you save the file typically with a .KIX extension. Then run it like this (assuming you have KIX32.EXE in C:SCRIPTS folder) C:\SCRIPTS\KIX32.EXE MyNewScript.KIX It will then process what you have in the script file. The ReadLine is a UDF and there is an FAQ here on the board to show you how to use them. Basically you add the code portion of the UDF to your script and call it. You DO NOT modify the UDF code Example: ReadFile($file) for you to call would be ReadFile('C:\SCRIPTS\FileToDelelete.txt') |
||||||||
|
|
|||||||
Ok, I changed that but I am still having no luck. Thanks for all your help. I'll keep trying. |
||||||||
|
|
|||||||
try posting what you have... telepathy is hard when the weather is this cold |
||||||||
|
|
|||||||
I've written a few simple scripts, but never used a udf before. I thought this would be something simple. Sorry about that. This is all I have. Obviosuly I'm missing something. for each $file in readfile('\\server\c$\delete_these_files.txt') del 'C:\program files\'$file next |
||||||||
|
|
|||||||
I assume the UDF code is in there somewhere. del 'C:\program files\'+$file |
||||||||
|
|
|||||||
Try something like this. Modify to your values and make sure users have access to the file(s) to read and delete. |
||||||||
|
|
|||||||
Ok, here is what I have now. Code: Break On and here is what the delete_these_files.txt looks like Code: file1 i still can't to get it to work the script runs without errors, but it doesn't delete anything |
||||||||
|
|
|||||||
I read through the udf faq and still don't see the problem with the code. Has anyone else been able to get something like this to work? |
||||||||
|
|
|||||||
try this: change Del 'C:\Program Files\'+$File to: ? 'C:\Program Files\'+$File Del 'C:\Program Files\'+$File ? @error +' '+@serror also, does your people have local admin? |
||||||||
|
|
|||||||
No, they are all regular users, but I can't get it to run for me either. I'll make the changes and report back... |
||||||||
|
|
|||||||
they will probably not have permission to delete those files then... |
||||||||
|
|
|||||||
A step in the right direction...well a direction anyway!! This is the output I get when I run it in a dosbox Code: C:\test.kix |
||||||||
|
|
|||||||
Do I need a comma or semicolon in delete_these_files.txt? File1, or File1; |
||||||||
|
|
|||||||
tried both the comma and the semicolon, they were just a step backwards |
||||||||
|
|
|||||||
BTW... when you say file are you meaning folder Be specific |
||||||||
|
|
|||||||
yea, sorry about that. stupid question of the day: does that make a difference? |
||||||||
|
|
|||||||
Del Action: Deletes one or more files. http://www.scriptlogic.com/Kixtart/htmlhelp/Commands/del.htm RD Action: Removes the directory specified. http://www.scriptlogic.com/Kixtart/htmlhelp/Commands/rd.htm and yes, it makes a difference |
||||||||
|
|
|||||||
and if they do not have admin, then they will probably have have the permissions to delete those files, unless the acls have been played with... GIGO |
||||||||
|
|
|||||||
since GIGO has come up. Are you just trying to remove installed programs by deleting the directories? |
||||||||
|
|
|||||||
you clearly don't have a File1 in your program files folder if it says there is no such file in the error. |
||||||||
|
|
|||||||
Your file should specify full filenames with the extension Also run it with debug so you can see exactly what it is doing at what point. |
||||||||
|
|
|||||||
not trying to uninstall, just remove some directories also i changed del to rd and now i get this output Code: c:\test.kix |
||||||||
|
|
|||||||
if you READ the links that I provided, RD only works on empty directories so you will need to provide directory names in your file. and do something like: for each $d in readfile($file) del "%programfiles%\"+d$+"\*.*" /s /c rd "%programfiles%\"+d$ /q next |
||||||||
|
|
|||||||
btw... as funny as it would be, make sure there are no blank lines in your text file. |
||||||||
|
|
|||||||
Error 145: The directory is not empty. @SERROR would also show this Have a look here for all error codes and a short explanation. System Error Codes |
||||||||
|
|
|||||||
thanks everyone for all your help, we're getting there!!! now i get ERROR : undifined variable [$d]! Line : 11 i tried changing the delete_these_files.txt to read $dDirectory then i tried changing the $d in the scrip to $file and still no luck |
||||||||
|
|
|||||||
heh, no, the $d is the file. you have somewhere a dim $file line? change that to $d. |
||||||||
|
|
|||||||
so far i have this in my script Code: Break On this in my delete_these_files.txt Code: $FileDirectory and this is th output Code: C:\test.kix |
||||||||
|
|
|||||||
For Each $File In ReadFile('\\Server\Share\delete_these_files.txt') If Exist('C:\Program Files\'+$File) ? 'C:\Program Files\'+$File del "C:\Program Files\"+$File+"\*.*" /s /c ? @serror rd "C:\Program Files\"+$File /q ? @serror endif next |
||||||||
|
|
|||||||
just the dim $file, or all the $file? |
||||||||
|
|
|||||||
Quote: wtf? the txt file was supposed to have the darn directory names in there. slow down and carefully plan the steps to take. it's not as complex as it looks. |
||||||||
|
|
|||||||
i get an error in expression line 13 if i remove the q i get Code: C:\test.kix I am admin on the local machine and i know the name of the directory is correct |
||||||||
|
|
|||||||
show the contents (at least some) of the file show the code show the error |
||||||||
|
|
|||||||
Quote: It does, that was just one of the many things I have tried to get this to work some of the directories are: pics from home my profile.backup hotties basicly just a bunch of junk one of the other admins on base told my users to save there personal junk under program files |
||||||||
|
|
|||||||
here is everything i have test.kix Code: Break On here is the delete_these_files.txt file Code: hotties and here is the error i get when running the script Code: C:\test.kix also the script deletes itself each time i run it |
||||||||
|
|
|||||||
When you run the script use the following: Code:
This will allow you to step through the script and see what it is doing on each line and you can look at what the variables are being set to . Read up on the Debug section in the manual. |
||||||||
|
|
|||||||
it stopped deleting itself all the time now if i can just figure out how to remove a directory that has files and folders in it i'll be set |
||||||||
|
|
|||||||
If you are running it in Debug mode when you get to this line del "C:\Program Files\"+$File+"\*.*" /s /c pay attention to what the error code returned is. Using the ? @serror should tell you exactly why it is not deleting the files. From the output you posted earlier it is returning Access is denied. If this continues to hold true, look at what the permissions for that directory are for the account that is running the script. |
||||||||
|
|
|||||||
it says access denied, but the funny thing is i can delete it manualy....i can even create a directory myself then try to delete it with the script and i still get access denied |
||||||||
|
|
|||||||
correction!! if i create a directory and it is empty it works...well it deletes the dir anyway the output is Code: C:\test.kix however if i put something in the directory it fails to delete it and i get the system cannot find the file specified and the dir is not empty errors |
||||||||
|
|
|||||||
well, you are trying to delete "c:\program Files" as one. I know that someone might wanna do that, but that's why you get the error 5. deletion of non-empty dir is currently supported only by 4.52 beta 1. |
||||||||
|
|
|||||||
what do i need to change in the script to only delete the specified directories in program files |
||||||||
|
|
|||||||
change your loop: Code:
to: Code:
note, this rd /S need the new beta. |
||||||||
|
|
|||||||
the air force only allows us to use 4.11 btw: Thanks to everyone for there help so far |
||||||||
|
|
|||||||
k, then you can add your lines back. Code:
|
||||||||
|
|
|||||||
getting closer!!! here is my test.kix Code: Break On my delete_these_files.txt Code: pics from home and here is the output (when the tester dir is empty) (tester is a dir i creted myself to test the scrip) Code: c:\test.kix when the tester dir is NOT empty i get Code: C:\Program Files\tester I can manualy delete the files so I'm sure it's not a permissions problem. |
||||||||
|
|
|||||||
so, you had kix 4.11? |
||||||||
|
|
|||||||
Quote: so, change: del "C:\Program Files\"+$File+"\*.*" /s /c to: del "C:\Program Files\"+$File+"\*.*" /c |
||||||||
|
|
|||||||
a little better now i get this Code: c:\test.kix no more "Access is Denied" |
||||||||
|
|
|||||||
what you have in such dirs that it says can't find the file? |
||||||||
|
|
|||||||
Well if you can't use a newer version of KiX then just SHELL out and use RD /S /Q for what you want to remove. Files in use can not be removed though. Be quite careful too as any mistakes may render a system non bootable. Always use some type of LEN or other command to verify you're not at the root of the drive. |
||||||||
|
|
|||||||
Still won't let me live that one down, eh Doc... :-) |
||||||||
|
|
|||||||
LOL - no names mentioned. I've actually written my own complete code that did not check the length of the var and removed way too much data |
||||||||
|
|
|||||||
Then there was my disastrous BBChecker uninstall that wiped out my entire HD. |
||||||||
|
|
|||||||
I was waiting for that old memory to come alive. iirc, it was about the uninstaller script and how it was called and les didn't talk to me for a while... |
||||||||
|
|
|||||||
just a thought, what about deltree.exe Break On Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On') Dim $File For Each $File In ReadFile('\\szl-fs9ces1\roxio\delete_profiles.txt') If Exist('C:\Documents And Settings\'+$File) shell "deltree /y 'C:\Documents and Settings\$File'" endif this doesn't work how do i shell a something that has quotes in the command? |
||||||||
|
|
|||||||
shell 'deltree /y "C:\Documents and Settings\$File"' |
||||||||
|
|
|||||||
shell 'deltree /y "C:\Documents and Settings\' + $File + '"' |
||||||||
|
|
|||||||
Try again Rad He is using: $SO=SetOption('NoVarsInStrings','On') |
||||||||
|
|
|||||||
oops I see Canada is up |
||||||||
|
|
|||||||
There are a few FAQs on the use of SHELL and quotes. |
||||||||
|
|
|||||||
i hate novars... |
||||||||
|
|
|||||||
that doesn't seem to work, I also tried: Run 'deltree /y "C:\Documents and Settings\$File"' |
||||||||
|
|
|||||||
read on... |
||||||||
|
|
|||||||
I can get it to work this way shell 'deltree /y "C:\Documents and Settings\dir_to_delete"' but i can't seem to get it to recognize $File this works: Code: Break On this doesn't Code: Break On |
||||||||
|
|
|||||||
Well DELTREE is not a valid program on 2000/XP/2003 but the RD command is as I've shown before. The following code should work just fine on Windows NT/2000/XP/2003 as long as the user has Administrator rights on the system and there are no files locking the profile. |
||||||||
|
|
|||||||
Quote: ROFL now - now ... That's just lazy coding. You're letting Ruud take up the slack for you inside the EXE. |
||||||||
|
|
|||||||
Quote: What operating systems are we talking about here? I certainly hope the Air Force or whomever is not still running Windows 95/98 systems in production. DELTREE does not exist on my NT4/2000/XP/2003 systems so if it's working (semi-working) for you then we must be talking about a Windows 95/98/ME system which would mean the RD code I wrote would not work either as Windows 95/98 used a different switch as I recall. |
||||||||
|
|
|||||||
doc, my 100,000 USD is on him not reading all the replies and not really seeing that his syntax is wrong. |
||||||||
|
|
|||||||
To delete a directory, use RD. And upgrade to KiXtart 4.52 beta 1 to utilize the RD /S switch. And read the FAQ Forum with regards to e..g using UDFs. |
||||||||
|
|
|||||||
I know you found some free time and are updating posts Jens, but you missed the part where he says he can't upgrade his KiX version. Thus the RD code of CMD.EXE |
||||||||
|
|
|||||||
Darn, yes, found the Air Guess, they need to certify the newer KiXtart versions first |