Solved Dos script help

born2achieve

New member
Local time
3:40 AM
Messages
36
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

Computer type
PC/Desktop
OS
windows7 64 bit
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Customs, Dell, Gateway, HP, Toshiba, Acer, ASUS
OS
Windows 7 Ultimate 64-bit, Windows 8.1 64-bit, Mac OS X 10.10, Linux Mint 17, Windows 10 Pro TP
Keyboard
Microsoft
Mouse
Microsoft
Hey both of you,

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.

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:
[FONT=Arial]forfiles /p "D:\Sample" /m *.sql /d 1/10/2014 /c "cmd /c if @isdir==FALSE echo @file"[/FONT]
(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

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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

Computer type
PC/Desktop
OS
windows7 64 bit
Not sure why the code is not working. Any clue please

It works perfectly.

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

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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

Computer type
PC/Desktop
OS
windows7 64 bit
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

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
  • Like
Reactions: Arc
wonderful and you are man!!! Appreciate your time and best help. Thank you somuch
 

My Computer

Computer type
PC/Desktop
OS
windows7 64 bit
Back
Top