+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, Copy File to folder by date in Coding and Web Development; ok, i am a total noobie for the forums..so please be gentle =) what i am trying to accomplish is ...
  1. #1

    Join Date
    Jun 2010
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Copy File to folder by date

    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. =(

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    Not quite sure what you're trying to do but I think this might get you started.

    Code:
    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

  3. #3

    Join Date
    Jun 2010
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by srochford View Post
    Not quite sure what you're trying to do but I think this might get you started.

    Code:
    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.

  4. #4

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    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.

  5. #5

    Join Date
    Jun 2010
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

    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. =(

  6. #6

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    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.

    Code:
    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

  7. #7

    Join Date
    Jun 2010
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by srochford View Post
    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.

    Code:
    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"

    Code:
    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

    did i do something wrong?

    oh and yes... i am american lol. good observation. =-)

  8. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    Oops!
    the oFile.delete line doesn't need the comma before true:
    Code:
    oFile.delete true
    You can delete files in 2 ways:

    Code:
    set ofSO=createobject("scripting.filesystemobject")
    set oFile=ofso.getfile("c:\temp\test1.txt")
    oFile.delete true
    and
    Code:
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 29th January 2013, 10:16 AM
  2. Replies: 3
    Last Post: 21st May 2010, 09:11 AM
  3. Copy file across variable folder name
    By SimpleSi in forum *nix
    Replies: 11
    Last Post: 10th March 2009, 06:15 PM
  4. vbscript to copy a folder & delete a folder
    By MicrodigitUK in forum Scripts
    Replies: 2
    Last Post: 29th January 2009, 12:43 PM
  5. Replies: 4
    Last Post: 23rd March 2006, 06:27 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •