The title states my problem but I guess what I really need are some references. Help? Please?
dir /o means listing files in sorted order. Use the equivalent ls in AIX to achieve the same result. Experiment with option -t and -r as desired
Related
Is there any way to do that? Maybe some external program?
dir "d:\Folder" /ad /b /o:-d /s
Sorting is not working with "/s" and folders for me.
Compared to Unix-Shells the Windows Command Line Interface isn't very powerful. Windows PowerShell might be better for you, if Windows is mandatory for you. See this page for example.
OK I found an easy way to do this.
I just searched for "kind:folders" and sorted them by date.
I'd like to be able to search files on a Windows machine using the command line instead of the GUI interface. For example, on Linux, I use:
find . -name "*.c" -exec grep -Hn "sqlcommand" {} \;
Is there something similar with Windows?
After long time working with Unix systems I had to make some scripts on Windows.
For serious scripting Powershell is the tool you should use.
You can search Internet with keywords like powershell find string in file,
or other combinations and you find a lot of information.
That's the problem, a simple oneliner like
get-childitem C:\yourdir -include *.c -recursive |Select-String -pattern sqlcommand
won't help you much. You need to find the PowerShell IDE, learn the different syntax and try to love / accept that new stuff.
Prepare for a study with PowerShell when you want to do these things more often, or try to get a Unix-like environment on your windows (cygwin, or better git for windows)
NEW AND IMPROVED ANSWER
I recently stumbled upon a built-in command that is rather similar to find in Unix:
ForFiles
Basic syntax is:
forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c <Command>] [/d [{+|-}][{<Date>|<Days>}]]
There are several variables to use when constructing the command to execute per each file (via the /c switch):
#FILE File name.
#FNAME File name without extension.
#EXT File name extension.
#PATH Full path of the file.
#RELPATH Relative path of the file.
#ISDIR Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE.
#FSIZE File size, in bytes.
#FDATE Last modified date stamp on the file.
#FTIME Last modified time stamp on the file.
It looks like you would use the command like this:
FORFILES /m *.cs /c FINDSTR /I /N /C:"sqlcommand" #FILE
I'm not sure how long this command has been around, but the earliest reference I could find in the documentation is from 2008-09-02:
https://web.archive.org/web/20080902221744/http://technet.microsoft.com:80/en-us/library/cc753551.aspx
and that page states that it was last updated on "April 25, 2007". The documentation is filed under "Windows Server" so it likely started there and was added to the desktop OSes starting with Windows Vista, I believe. I did check Windows XP and didn't see it there, though it is on Windows 10.
ORIGINAL ANSWER
This requires a combination of two DOS commands:
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
and
DIR /B /O:N /W *.c (this is the 'command' noted in the FOR command above)
Create a CMD script as follows:
#ECHO OFF
FOR /F %%B IN ('DIR /B /O:N /W *.cs') DO (
findstr /I /N /C:"sqlcommand" %%B
)
OR, just use the find command found in this set of Unix command ports:
http://unxutils.sourceforge.net/
or
http://sourceforge.net/projects/unxutils/
(both links should be the same project)
I want to write a batch file to cleanup my Downloads folder by deleting everything in it Except Files and Folders that were created today.
Thanks.
If you are using modern Windows, recommend you use forfiles,
Folder can still messy. Do you want to do it based on the timestamp of the directory itself? Do you want to process recursively through all folders, deleting files based on date and then delete the folder if it empty after deleting the files of given age. There are other reasonable interpretations of your question as well. Personally, I use a python script so I can make the file cleanup do exactly what I want. This may also be why Uriil suggested PowerShell.
Arguably, Windows Services for Unix downloadable from Microsoft would be considered fair game (allowing the find command mentioned by Johnride). If you use this, make sure Johnride suggestion matches your actual intent. For the find command, using the option -print instead of -exec is great for debugging
If you can use forfiles, this article may give what you want. I taking the liberty of pasting in the batch file solution using forfiles from the article.
#echo off
:: set folder path
set dump_path=c:\shares\dump
:: set min age of files and folders to delete
set max_days=1
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path"
:: remove sub directories from %dump_path%
forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path"
I'm totally new to scripting, but would like to use Windows CLI compare two text files, list1.txt and list2.txt, each containing a list of values, and generate a new text file containing values that are found in one list but not in the other. I've been reading about Powershell, Shell, Batch files etc but cant seem to figure out the basics. Do I need to download anything to use these languages? Or how can I directly compare the files using the Windows CLI? Thanks.
This is very doable in PowerShell, which is preinstalled in windows 7+ and easily added in vista or less - look for some beginner powershell info on Google. You're going to want to use Get-Content and Compare-Object, then Where-Object to select which difference indicator you care about. Good luck!
To do this in the regular Windows shell, just do:
for /f "delims=" %A in (list1.txt) do #find "%A" "list2.txt" >nul2>nul || echo.%A>>list3.txt
or if you put it in a batch file, double the % signs.
for /f "delims=" %%A in (list1.txt) do #find "%%A" "list2.txt" >nul2>nul || echo.%%A>>list3.txt
This will give you all lines in list1.txt that do not exist in list2.txt, and save them into a file called list3.txt.
I am in a project where I need to find all files which are two days lesser than the current date. How can we achieve this using a batch script in Windows?
Appreciate your help!
I have previously misunderstood the question. Here is the updated answer.
The syntax is a little bit different for different OS.
forfiles /d +2 (Windows Server 2008)
forfiles -d+2 (Windows 2000)
In case there is no such command in your OS, here is a link for downloading the FORFILES command.
You can even specify what you want to do for those files. Type forfiles /? or forfiles -? for help.
Not sure what you want to do with the files after you have found them but you could use the ForFiles command.