Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Dos script help

16 Nov 2014   #1
born2achieve

windows7 64 bit
 
 
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 System SpecsSystem Spec
.
17 Nov 2014   #2
Berton

Windows 7 Ultimate 64-bit, Windows 8.1 64-bit, Mac OS X 10.10, Linux Mint 17, Windows 10 Pro TP
 
 

Quote:
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 System SpecsSystem Spec
17 Nov 2014   #3
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Hey both of you,

Quote   Quote: Originally Posted by Berton View Post
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.

Quote   Quote: Originally Posted by born2achieve View Post
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 System SpecsSystem Spec
.

17 Nov 2014   #4
born2achieve

windows7 64 bit
 
 

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 System SpecsSystem Spec
17 Nov 2014   #5
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by born2achieve View Post
Not sure why the code is not working. Any clue please
It works perfectly.

Quote   Quote: Originally Posted by born2achieve View Post
... 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 System SpecsSystem Spec
18 Nov 2014   #6
born2achieve

windows7 64 bit
 
 

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 System SpecsSystem Spec
18 Nov 2014   #7
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by born2achieve View Post
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 System SpecsSystem Spec
18 Nov 2014   #8
born2achieve

windows7 64 bit
 
 

wonderful and you are man!!! Appreciate your time and best help. Thank you somuch
My System SpecsSystem Spec
Reply

 Dos script help




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
What is Macro / Script ?
I heard a lot about Macro and Scripts , something about automating task, file management, directory management, disk drive management etc. So how can any user use that to his efficiency, like file and directory management is one thing i could associate with the prospect of learning Macro or...
General Discussion
Logon Script
I like to create small websites, just doing fun stuff with HTML and such. I thought it'd be easier to share the htdocs folder(I'm using XAMPP - that's where are all HTML etc files are) and edit those files / create new files from any comp in my network. So... I wanted to run a script (net use...
Network & Sharing
In need of a script...
I am looking for a script which can delete cookies, Windows + browser cache automatically on a regular (scheduled) basis. Can this be done through Windows itself or would i have to implement a custom script or something? I am looking to replace CCleaner and similar software package(s). Just now...
Performance & Maintenance
ie8 script error
had a bit of trouble with ie8 tried reinstalling ie8 for msn and another or epeadia all the same problem loads of script errors I have done a reg scan and clean unticked the debugging in tools advanced anything else i can do thanks
Browsers & Mail
Need help testing my script
Hi. I didn't know where to put this thread. I felt this was the best place. I have spent all day on creating a .bat script that replaces explorer.exe with my modified explorer.exe (only change is the Start Orb .bmp file.) I would be very happy if someone could test it and see if it actually...
Customization
can't run script
Having a problem run this script at 1am /every:Friday c:\windows\shutdown.exe /l when I run it a black screen pop up and then disappears so I know the script is running but nothing is happen. I have ran it from the command line and it say access denial but I am in the local administrator...
Network & Sharing


Our Sites

Site Links

About Us

Find 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 20:29.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App