1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Seeking Dos cmd help

Discussion in 'DOS/Other' started by dosls, Nov 16, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. dosls

    dosls Thread Starter

    Joined:
    Nov 16, 2011
    Messages:
    2
    I am new to cmd line scripting. I would like to set up a script that runs each first day of the month to do the following:

    Check to see if a destination folders exists.
    Currently the desitination folders are set up as follows:
    Y:\Destination\company1\year\monthname
    Y:\Destination\company2\year\monthname
    Y:\Destination\company3\year\monthname etc

    I would like to make sure, for each company in the Destination folder, the subfolder Year exists and then the subfolder to year, monthname exists.

    Then I want to copy from the source folders such as:

    X:\Source\company1
    X:\Source\company2
    X:\Source\company3 etc
    So all the files in each of the source folders are copied to the corresponding destination company folder but in the subfolder for the corresponding month().

    Then once all the files are copied, I can delete the source files in each of the company folders.
    Also, I don't want to have to hardcode all of the company folders in the source or destination folders. Maybe a loop can be created for each of the folders?

    Can someone help me create a script to accomplish this?
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I would assume you have the source folders created already with the company names with files inside the folders. If not how would it be possible to even do this.

    It is easy enough to look at the folder with the company name in the source and create a corresponding folder in the destination.
     
  3. dosls

    dosls Thread Starter

    Joined:
    Nov 16, 2011
    Messages:
    2
    Yes the source folders and files within each folder exist. The destination company folders exist but each month I want to create a month name folder and then copy in the source files in the month folder under each company folder.
    Does that make sense?
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Well lets build this in steps. I could easily just pump out all the code for you but I am hoping as I teach you a few fundamentals that you then can build on top of that knowledge to do a few things yourself.

    So the first thing we are going to learn about is checking to see if the folders exist in your destination and if they don't we will create it.

    So currently I have a few company folders in my Source location.
    Code:
    C:\Source>dir /ad /b
    Company1
    Company2
    Company3
    
    C:\Source>
    And there is nothing in my destination folder.
    Code:
    E:\Destination>dir /ad /b
    
    E:\Destination>
    So now we are just going to focus on checking to see if the folder structure for a company exists in the destination and if not we will create it. For now we are just going to hard code everything so you can see how the base code functions. Have to start small and then build up to the larger code.
    Code:
    C:\Source>IF NOT EXIST E:\destination\Company1\2011\11 MKDIR E:\destination\Company1\2011\11
    
    C:\Source>
    So now if we look at the destination directory you will have this.
    Code:
    E:\Destination>dir /ad /b /s
    E:\Destination\Company1
    E:\Destination\Company1\2011
    E:\Destination\Company1\2011\11
    
    E:\Destination>
    In my Next post I will show you how to loop through the Source folders and check if a destination folder exists and if not we will create it. After that I will focus on getting the current year and month into a variable so that you don't have to hardcode the year or month.
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    So currently we just have one company in our source that exists in the destination. We don't want to continue manually creating the destination directories 1 at a time so we will use a for loop to create the remaining folders in the destination by looping through the source.
    Code:
    C:\Source>for /D %A in (*) do IF NOT EXIST E:\destination\%A\2011\11 MKDIR E:\destination\%A\2011\11
    
    C:\Source>IF NOT EXIST E:\destination\Company1\2011\11 MKDIR E:\destination\Company1\2011\11
    
    C:\Source>IF NOT EXIST E:\destination\Company2\2011\11 MKDIR E:\destination\Company2\2011\11
    
    C:\Source>IF NOT EXIST E:\destination\Company3\2011\11 MKDIR E:\destination\Company3\2011\11
    
    C:\Source>
    So now if we look at our Destination we see
    Code:
    E:\Destination>dir /ad /b /s
    E:\Destination\Company1
    E:\Destination\Company2
    E:\Destination\Company3
    E:\Destination\Company1\2011
    E:\Destination\Company1\2011\11
    E:\Destination\Company2\2011
    E:\Destination\Company2\2011\11
    E:\Destination\Company3\2011
    E:\Destination\Company3\2011\11
    
    E:\Destination>
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    So now we need to get the Month and Year into a variable. Well the Date is already a predefined variable.
    Code:
    C:\Source>echo %date%
    Fri 11/18/11
    
    C:\Source>
    The output of the date variable is defined by your systems Regional & Language settings. As you can see my only displays a Two digit year. We have it that way for a lot of the legacy applications we run. I believe the default within Windows is actually to display the 4 digit year. But the regional settings can be customized to output the date in just about any format. Could be YYYY/MM/DD if you wanted it to.

    So If I want to just pull out the Month and Year from my date variable we can substring the variable. Now we have to remember that computers use the number 0 for the start of most things. So if you want the first position of a variable you have to ask for position 0. Position 2 is referenced by asking for position 1. So to get the Month and Year out of my Date variable I do the following.
    Code:
    C:\Source>echo %date:~4,2%
    11
    
    C:\Source>echo %date:~10,2%
    11
    
    C:\Source>
    Lets see if you can put some of this code together on your own.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1027136

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice