Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File - First  (Read 3582 times)

0 Members and 1 Guest are viewing this topic.

Joey56

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 10
    Batch File - First
    « on: January 07, 2018, 08:05:10 PM »
    I've read all over and can't manage to script myself a batch file that will copy files to specific folders depending on their names.
    I have a company with employees saving their timesheets as .pdf files. I have a folder named "Time sheets", and within that I have folders named "2016", "2017", "2018", etc. Within each of those, I have folders named "January", "February", "March", etc. for every month.
    The timesheets are saved under names such as "Time sheet_171203_EmployeeName.pdf". I'd like to have a batch file that will
    1) identify the file as a timesheet and not another file (many reports are saved in the source folder "PDF Reports");
    2) copy the time sheet from the source folder to whatever the month is indicated as within the time sheet name. For the example above, the time sheet should be copied to "Time sheets\2017\December\Time sheet_171203_EmployeeName.pdf". Now in January, it should automatically switch over to "Time sheets\2018\January\...", etc.

    I am rather new with batch files. I have only begun coding JavaScript and have been coding Lua for some years now. The format batch files take on is rather intriguing and very new to me.

    I'm very confused as to where to start. I know these steps must be met, but I'm unsure how to code them:

    I must identify the "PDF Reports" directory;

    I must iterate through all .pdf files and identify those with the filename of "Timesheet*" using the for command;

    I must then set variables and somehow extract the year %%mmdd and month yy%%dd to then transfer to the proper output folder (ex 2017\December);

    I must make it so that this batch file executes every time a file is added to the directory

    Could anyone help me start this project? I realize this is not the simplest code to accomplish as a first-timer...! Any help is appreciated while I read through tutorials. I sure hope I'm at the right place!

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Batch File - First
    « Reply #1 on: January 13, 2018, 08:26:28 AM »
    Let me know if you still need help with this.

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch File - First
    « Reply #2 on: January 13, 2018, 09:03:03 AM »
    Welcome to Computer Hope, and there are people here interested in helping, so your at the right place I feel.  :)

    We like to see people attempt at making something work and then coming here with what they have when they have problems vs doing all the work in which people dont learn. Thats not to say that we dont, we have coded up everything from scratch before but we are all volunteers here and so its nice if people already have the majority of the work completed and we just need to edit what they have vs do all the work when some of us are limited in how much time we can put into helping and editing is quick, but writing something up from scratch is more involved. I myself at times havent responded to things just because I dont have an hour or so to create something from scratch and test it and then submit it, but editing and suggestions and pointing people in a good direction doesnt take much time and in the end they have a batch or script of another type that works for whatever they need. Lastly we do get students here with homework in which they sometimes ask for us to help but they havent done any of the work, and we dont want to be a detriment to their education by just doing the work for them, so many of us make suggestions vs code stuff up when we see stuff like that. But the suggestions may include instruction changes for the batch file to get past a problem at times vs saying keep digging and you will get there which for some people is frustrating.

    Mark.



      Adviser
    • Forum Regular
    • Thanked: 67
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 10
    Re: Batch File - First
    « Reply #3 on: January 13, 2018, 01:16:43 PM »
    totally agree with Dave, this sounds like a great little project to get your hands dirty with.

    but since I literally have done something recently that's very similar, it will at least give you a place to start.
    it's VBScript and all I do is double click it to move all my PDF files that accumulate in one folder to another folder.

    here's the code;
        set myFiles = CreateObject("Scripting.FileSystemObject")
        myfldr = "C:\MyDocs\"
        myFiles.MoveFile "x:\*.pdf", myfldr & "Business\Parts Receipts\"

    myfldr is the target.   "x:\*.pdf" is the source, adjust accordingly.  something like "x:timesheet*.pdf" will work as long as everyone saves their PDFs starting with 'timesheet'.

    you can do a lot in Batch files, but I find it gets messy real quick, and hard to decode 6 months down the track when you need to add/change things.
    give VBS a crack, if nothing else it'll have a lot more date handling options which sound like you'll be needing.