In cmd how can I list folders that contain files with a specific extension? - file

Using just the command prompt or a batch script, I would like to look inside my current directory and all subdirectories for folders containing a specific filetype.
So, example:
Say I am in directory F:\dir\one\, and F:\dir\one\ looks like this:
F:\dir\one\
|
+--F:\dir\one\two\
| |
| +--file.c
|
+--F:\dir\one\three\
| |
| +--work.py
| |
| +--F:\dir\one\three\four\
| | |
| | +--file2.c
And I ran my command/script looking for *.c files, I would expect an output of
>F:\dir\one\two
>F:\dir\one\three\four
because those folders contain *.c files.
How could I do this?

To be run from command line. For batch files, percent signs need to be escaped, replacing % with %%
for /r %a in (.) do #if exist "%~fa\*.c" echo %~fa

This:
for /r %a in (.) do #if exist "%~fa*.c" echo %~fa
works for current directory but it doesn't if you specify in which directory you would like to have result:
for /r %a in ('F:\dir\one') do #if exist "%~fa*.c" echo %~fa
it just hangs. I tried 'F:\dir\one' and "F:\dir\one" and 'F:\dir\one\' and "F:\dir\one\"

Related

Move files with all subdirectories

I want to move all files and directories to another specific directory.
|
\---MainDirectory
| MainFiles
|
+---Subdirectory1
| MultipleFiles1
|
\---Subdirectory2
MultipleFiles2
|
\---TargetDirectory
| MainFiles
|
+---Subdirectory1
| MultipleFiles1
|
\---Subdirectory2
MultipleFiles2
Currently using following code with batch file:
move "MainDirectory" "TargetDirectory"
It is moving only MainDirectory like:
|
\---TargetDirectory
MainFiles
I do not want to move MainDirectory. I know it is very basic question, but I am not expert and have searched for more than 2 hours and haven't found a solution.
I tried
robocopy "MainDirectory" "TargetDirectory" /MOVE
It is also moving only MainFiles.
This code is moving successfully but removing MainDirectory which I do not want to remove
robocopy.exe "MainDirectory" "TargetDirectory" /S /Move

filter search output before writing to text file

For batch script, i am trying to grep some searches before saving it to a text file, but there are some search entries which are unwanted.
Command i use to search and save into text2.txt:
grep -nri !line! --colour --include=*.{c,h} >> text2.txt
is it possible to filter the output before i write to the file?
Stuff that i want to filter before writing to the file:
type text.txt | findstr /I "#include # include" | findstr /V "examples DELIVERY_REL #ifndef #define" > text2.txt
DELIVERY_REL/xxx/xxx/xxx/xxxxxx.h:139:#ifndef _xxxxxx_h_
/xxxxxxx/xxxxxxxx/xxxxxxx/xxxxxx.h:32:#include "../../xxxx/xxxxxxxx.h"
/xxxxxx/xxxxxx/xxxxxx/xxxxxxx.h:26:#include "../../xxxxxx/xxxxxxx.h"

How do you assign the output of a multiple piped command to a variable in windows CMD batch?

I need to loop through a set of files in a directory, matching a wildcard pattern and extract a portion of of the file name in between two other patterns. Unix (i.e. bash) equivalent command would be something like this:
ls -1 "$wildcard1"*"$wildcard2" | while read filename; do
string2find=`echo $filename | cut -d $delim1 -f2 | cut -d $delim2 -f1`
echo $string2find
#do something with string2find variable here
done
In batch file, I tried this
for %%f in (WILD1*WILD2) do (
echo %%f | cut -d_ -f2 | cut -d"." -f1> temp.out <--here, need something between an "_" and a "."
type temp.out <-- see the right string in the file
set var= <clear the variable
set /p var=<temp.out
echo %var% <-- ECHO is off message I am getting here
del temp.out
REM need to do something with var here
)
All the examples I was able to find is devoid of piped commands.

Windows Batch : list all files with name, path, data modified, date created and owner into a csv file or txt file

I have a script that can display the file path, there are some properties that can not be displayed, such as date modified, date create, owner.
#ECHO off
SET v1=%%~dpF
SET v2=%%~nxF
(for /r %%F in (*) do #echo "%v1%","%v2%"
pause
Example Output :
- D:/newfolder1/tester.doc | 18/02/2016 01:30:00 | 16/02/2016 02:13:12 | Nickolas
- D:/newfolder2/tester2.doc | 11/03/2016 01:30:00 | 12/02/2016 02:13:12 | Marx Timberlack
- D:/newfolder3/tester3.doc | 18/02/2016 01:30:00 | 16/02/2016 02:13:12 | Administrators
Basically I want to add a 4th parameter, which should show file owner. It is in Windows 7 environment.
As mentioned in a comment by Klitos Kyriacou, PowerShell can do this:
PARAM (
$Path = 'C:\Users\MarxHood\Desktop\', $Report = 'C:\Users\MarxHood\Output.csv'
)
$Owner = #{
Name = 'FileOwner'
Expression = { ((Get-Acl $_.FullName).Owner).Split('\')[1] }
}
Get-ChildItem -Path $Path -Recurse | Select FullName, LastWriteTime, CreationTime, $Owner | Export-Csv -NoTypeInformation -Delimiter "|" $Report
Just change the locations as necessary for $Path and $Report at the top.
Edit
Below is a method using a batch file, unfortunately this is dependent upon the localised date/time output; (tokens may need adjusting to suit your locale). Domain names in the owner output string which contain a space would also cause this to fail.
#Echo Off
Set "Path=C:\Users\MarxHood\Desktop"
Set "Report=C:\Users\MarxHood\Output.csv"
(For /F "EOL= Tokens=1,2,4*" %%A In ('Dir/A-D/-C/Q/TC "%Path%\*"'
) Do For /F "Delims=" %%E In ("%%~tD"
) Do Echo="%%~fD"^|"%%E"^|"%%A %%B"^|"%%~nC")>"%Report%"
Remember to change the locations as necessary for %Path% and %Report% near the top.
Using the command dir /TC will show you the creation date of each file. /TA will show you last-access, and /TW will show last-written.

Batch file not generating output as expected?

I have a simple dynamically generated batch file which follows the general format of:
#echo off
dir /a/s "C:\inetpub\wwwroot\files\clients\26\properties" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\pFiles_26.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\26\adverts" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\aFiles_26.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\28\properties" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\pFiles_28.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\28\adverts" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\aFiles_28.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\32\properties" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\pFiles_32.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\32\adverts" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\aFiles_32.dat"
and so on... there's about 280 different folders it will need to run through... but the resulting files generated e.g. pFiles_26.dat or the like all end up as zero length files...
I know all these directories to have some content in them, so the results should all be varied...
As far as I can tell the commands aren't firing off the way they should... how can I ensure that these commands actually fire off, one by one, and result in files that contain the actual output of the recursive values of the numbers of files and total directory sizes??
Check you have write permission to the destination folder. Try your code to a desktop folder, for example. Mind you, you shouldn't get zero byte files either if that were the case.

Resources