Dos script help


  1. Posts : 36
    windows7 64 bit
       #1

    Dos script help


    Hi,

    Below is my folder path,

    D:/Sample

    I have .sql files in that folder and i need to get the file names based on modified/created date i pass. for ex : i need to get the file names created/modified on or after 10/01/2014(mm/dd/yyyy). Can any please give me some sample script to make this as batch.

    Any help is appreciated.

    thanks
      My Computer


  2. Posts : 1,519
    Windows 7 Ultimate 64-bit, Windows 8.1 64-bit, Mac OS X 10.10, Linux Mint 17, Windows 10 Pro TP
       #2

    D:/Sample
    Pleas keep in mind that should be the backslash \ when working in file management and DOS/Command Prompt, the forward slash / or slash is used in URLs/Web addresses.

    You can open a Command Prompt window, change the prompt to the Folder in question then type DIR /? to get a list of switches to use, one of which is the /O for Sort Order then add : and the choice of d for by date, there's several and can be multiple. dir /o:d
      My Computer


  3. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #3

    Hey both of you,

    Berton said:
    You can open a Command Prompt window, change the prompt to the Folder in question then type DIR /? to get a list of switches to use, one of which is the /O for Sort Order then add : and the choice of d for by date, there's several and can be multiple. dir /o:d
    While the internal command dir is the usual method of listing files, it is not an appropriate command for when dealing with dates. The command "dir /o:d" does not achieve Born2achieve's goal of listing files on or after his specified date, of 10/1/2014, by far.

    The command "dir /o:d" will just return a list of files and folders in an oldest first sorted order.

    The use of the internal dir command is altogether not the way to approach this kind of situation. Instead, a command called FORFILES exists, useful for exactly this kind of task.

    born2achieve said:
    Can any please give me some sample script to make this as batch
    No script is needed; a solution to your problem exists as a clean one-liner:

    REM Lists all .sql files in D:\Sample if the date modified is 10/1/2014 (mm/dd/yyyy) or greater.
    Code:
    forfiles /p "D:\Sample" /m *.sql /d 1/10/2014 /c "cmd /c if @isdir==FALSE echo @file"
    (Unless you need to do this routinely, a batch script is not required. However, feel free to ask about a script if the contrary is the case.)
      My Computer


  4. Posts : 36
    windows7 64 bit
    Thread Starter
       #4

    Hi Pyrophy,

    Thanks for your great time on this post. I copied your code and pasted in text file and saved it as .bat file. Then i hit the batch file it opens cmd and closed. nothing happened.

    I forgot to mention one point, The directory D:/sample has lot of sub folders in it and all sub folders has some .sql files it. So Will your code searches for sub folders on the Root folder?

    Not sure why the code is not working. any clue please
      My Computer


  5. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #5

    born2achieve said:
    Not sure why the code is not working. Any clue please
    It works perfectly.

    born2achieve said:
    ... it opens cmd and closes.
    That almost describes what happens, but more exactly: It opens Cmd, displays its output and then closes. This all takes place far too quickly for anyone to read any output; a batch file will close as soon as each line of its script has finished executing -- it will not wait for you to read any outputted text.

    Invoke the script through an interactive Command shell if you wish to see the results.

    Alternatively, you can tack the pause statement to the last line of the script to halt execution, effectively allowing you view the results that way.

    And finally, subfolders; not a problem. Simply add the '/s' switch anywhere to the forfiles command and it will recurse though subdirectories.


    Code:
    @echo off
    forfiles /p "D:\Sample" /m *.sql /d 1/10/2014 /s /c "cmd /c if @isdir==FALSE echo @file"
    pause
      My Computer


  6. Posts : 36
    windows7 64 bit
    Thread Starter
       #6

    Hi Pyprohly,
    Thanks for the reply

    I copied your code and saved it as CreateFile.bat. Also i wiped off everything in my folder and created new sub folders and each sub folder has one .sql file. Then i executed the batch file created.

    it displays the output in cmd. Can i have the output in separate file on the root folder"D:/sample".

    Thanks
      My Computer


  7. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #7

    born2achieve said:
    Can i have the output in separate file on the root folder"D:/sample".
    Redirect stdout to a txt file in 'D:\sample' (emphasis on the backslash!) instead of the console? Yes you may.

    Code:
    @echo off
    forfiles /p "D:\Sample" /m *.sql /d 1/10/2014 /s /c "cmd /c if @isdir==FALSE echo @file" >D:\Sample\output.txt
      My Computer


  8. Posts : 36
    windows7 64 bit
    Thread Starter
       #8

    wonderful and you are man!!! Appreciate your time and best help. Thank you somuch
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 12:01.
Find Us