grnslv Posted June 15, 2010 Posted June 15, 2010 ok, i am a total noobie for the forums..so please be gentle =) what i am trying to accomplish is this... i want to write a simple batch file that copies a file located- c:\example.doc to any folder that has the creation date of 06/14/2010 in c:\documents and settings\all users\application data\microsoft\ i would just use the directory name instead of doing it by date created - but im trying to do this on 50+ computers and the directory name is never the same. the directory i am wanting to copy this file to is named differently on every computer. im sure this is easy for you guys...but im just completely lost on this one. =(
srochford Posted June 15, 2010 Posted June 15, 2010 Not quite sure what you're trying to do but I think this might get you started. sRoot="c:\documents and settings\all users\application data\microsoft\" sFile="c:\example.doc" sCheckDate="2010-06-14" set oFSO=createobject("scripting.filesystemobject") set oFolder=ofso.getfolder(sRoot) for each oSubFolder in oFolder.subfolders sDate=ShowDate(oSubFolder.datecreated) if sDate=sCheckDate then ofso.copyfile sFile,sRoot & oSubFolder.name & "\" exit for end if next Function ShowDate(d) ShowDate=year(d)& "-" & right("0" & month(d),2) & "-" & right("0" & day(d),2) end function
grnslv Posted June 15, 2010 Author Posted June 15, 2010 Not quite sure what you're trying to do but I think this might get you started. sRoot="c:\documents and settings\all users\application data\microsoft\" sFile="c:\example.doc" sCheckDate="2010-06-14" set oFSO=createobject("scripting.filesystemobject") set oFolder=ofso.getfolder(sRoot) for each oSubFolder in oFolder.subfolders sDate=ShowDate(oSubFolder.datecreated) if sDate=sCheckDate then ofso.copyfile sFile,sRoot & oSubFolder.name & "\" exit for end if next Function ShowDate(d) ShowDate=year(d)& "-" & right("0" & month(d),2) & "-" & right("0" & day(d),2) end function ok allow me to try to be a little more clear. What i am trying to accomplish is to deploy this batch file out to every computer on my network. every computer on my network has a folder that i need to copy a certain file to. the folder i want to copy my file to doesnt have a static name. the one thing this folder has static - is the creation date. now to break down the details. there is a directory located @ c:\documents and settings\all users\application data\microsoft\ that has a random name. the directory looks like c:\documents and settings\all users\application data\microsoft\asdfcewr\ or c:\documents and settings\all users\application data\microsoft\zxcbvcv\ I am wanting this file: c:\example.doc to be copied to that random directory. as you can see, i cannot do a command like xcopy c:\example c:\documents and settings\all users\application data\microsoft\asdfcaxv\ becuase the directory of "\asdfcaxv" is named differently on every computer on my network. This randomly named directory was created on 06/14/2010. this is true on EVERY computer on my network. SO is there a way i can copy my file - c:\example.doc - to that randomly named directory based on creation date? this is an example command that is NOT RIGHT - this is just to try to find some clarity to the situation. xcopy c:\example.doc c:\documents and settings\all users\application data\microsoft\ only-copy-to-folder-with-creation-date-of-06142010 and if the post above by srochford accomplishes this - then what do i do with his script? do i just copy and paste all of that into a .bat file and run it? or is that a .vb script? or what? lol. like i said..im pretty new to this whole thing.
srochford Posted June 15, 2010 Posted June 15, 2010 OK; the above is a vbscript - you need to save it as copyfile.vbs (or other suitable name) The best thing to do would be to add it as a machine startup script - use group policy management and add this to the startup scripts for the OU containing machines where you want this to happen Is the C:\example.doc file already on the C: drive for every machine? If so, the script will work as is. If it's not there but you were going to copy it there and then put it in the Appdata folder then just change the C:\example.doc in the script to point to where the file is actually stored (eg \\server1\shareddocs\example1.doc) If you do do it like this then make sure that the folder and share allow "domain computers" read access.
grnslv Posted June 15, 2010 Author Posted June 15, 2010 srochford! thank you SO much for that script. it worked like a champ. i added it to the "run these programs on logon" in my group policy. works works works! thanks. now on to part two. how would i go about deleting files at the root of my c:\ drive that were placed there by a specific date and only if they are *.exe or *.file ? such as this http://img295.imageshack.us/img295/4391/vir.png as you can see, the highlighted files are either .file or .exe and they were (for the most part) modified/created on the same date. what kind of script would i need that would erase all files on the root of C:\ with the extensions of *.exe or *.file and were modified/created on 06/12/2010, 06/13/2010/ 06/14/2010, 06/15/2010? if you arent able to tell, im doing a bit of virus cleanup on my network. =(
srochford Posted June 15, 2010 Posted June 15, 2010 OK; same idea, Save this is cleanfiles.vbs and add it to startup scripts. Edit the sCheckDate line to the list of dates you're interested in - I've used | characters to delimit the dates (because no date will have that character) Run this on a test machine first - it will just show the names of files it's going to delete. Once you're confident it's the right list then delete the wscript.echo line (you don't want any text popping up at machine startup) and remove the ' (comment mark) from the start of the ofile.delete line. Just being nosy - are you in the US? Your dates are all in American format. It doesn't matter but that's why I've got the ShowDate function - like this I know that every date I handle will be year-month-day and I don't have to worry about regional settings. sRoot="c:\" sCheckDate="|2010-06-15|2010-03-20|" set oFSO=createobject("scripting.filesystemobject") set oFolder=ofso.getfolder(sRoot) for each oFile in oFolder.Files sDate=ShowDate(oFile.datecreated) wscript.echo sDate if instr(sCheckDate,sDate)<>0 then 'ofile.delete, true wscript.echo oFile.name end if next Function ShowDate(d) ShowDate=year(d)& "-" & right("0" & month(d),2) & "-" & right("0" & day(d),2) end function
grnslv Posted June 15, 2010 Author Posted June 15, 2010 OK; same idea, Save this is cleanfiles.vbs and add it to startup scripts. Edit the sCheckDate line to the list of dates you're interested in - I've used | characters to delimit the dates (because no date will have that character) Run this on a test machine first - it will just show the names of files it's going to delete. Once you're confident it's the right list then delete the wscript.echo line (you don't want any text popping up at machine startup) and remove the ' (comment mark) from the start of the ofile.delete line. Just being nosy - are you in the US? Your dates are all in American format. It doesn't matter but that's why I've got the ShowDate function - like this I know that every date I handle will be year-month-day and I don't have to worry about regional settings. sRoot="c:\" sCheckDate="|2010-06-15|2010-03-20|" set oFSO=createobject("scripting.filesystemobject") set oFolder=ofso.getfolder(sRoot) for each oFile in oFolder.Files sDate=ShowDate(oFile.datecreated) wscript.echo sDate if instr(sCheckDate,sDate)<>0 then 'ofile.delete, true wscript.echo oFile.name end if next Function ShowDate(d) ShowDate=year(d)& "-" & right("0" & month(d),2) & "-" & right("0" & day(d),2) end function hmm ok. so this is what i have prepared for "showtime" sRoot="c:\" sCheckDate="|2010-06-15|2010-06-14|2010-06-13|2010-06-12|2010-06-11|2010-06-10|" set oFSO=createobject("scripting.filesystemobject") set oFolder=ofso.getfolder(sRoot) for each oFile in oFolder.Files sDate=ShowDate(oFile.datecreated) if instr(sCheckDate,sDate)<>0 then ofile.delete, true end if next Function ShowDate(d) ShowDate=year(d)& "-" & right("0" & month(d),2) & "-" & right("0" & day(d),2) end function is that correct? when i try to run it i get the following error http://img252.imageshack.us/img252/2863/err.png did i do something wrong? oh and yes... i am american lol. good observation. =-)
srochford Posted June 16, 2010 Posted June 16, 2010 Oops! the oFile.delete line doesn't need the comma before true: oFile.delete true You can delete files in 2 ways: set ofSO=createobject("scripting.filesystemobject") set oFile=ofso.getfile("c:\temp\test1.txt") oFile.delete true and set ofSO=createobject("scripting.filesystemobject") sFile="c:\temp\test1.txt" oFSO.deletefile sFile, true - the first one doesn't take a comma between the object and the true; the second one takes a comma between the name and the true and I almost always get this wrong :-) Because I didn't want to delete stuff on my PC I didn't actually check the delete bit; sorry! Incidentally, the "true" is just a "force the deletion of this file" flag.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now