Batch file not generating output as expected? - batch-file

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.

Related

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"

automate .bat to get all content of a given directory

I am trying to automate a process that stores all content (directories, sub-directories, files etc.) in a .txt file. I came up with this:
cd "D:\sources\SVN_WorkingCopy\"
dir /s /b>filelist.txt
When putting the above in a .bat file and running it manually, it works fine and creates the .txt file. However, when i include it in a Windows Scheduled task, it doesnt do anything. Can anyone help, getting this to work ?
Thanks!
If you really wanted to do it in two lines then you need to make sure you use the /D option, to change drives too:
CD /D "D:\sources\SVN_WorkingCopy"
Dir /S /B > "filelist.txt"
Alternatively you could use a single line without changing the current working directory:
Dir /S /B "D:\sources\SVN_WorkingCopy" > "filelist.txt"
Or with a different command:
Where /R "D:\sources\SVN_WorkingCopy" * > "filelist.txt"
Also you need to be aware that when run as a scheduled task, the working directory is %SystemRoot%\System32 which is by default write protected, (dependent upon permissions). Therefore the command will try to send its output to a file there unless you provide an accessible location:
> "C:\Users\kalinkula\Desktop\filelist.txt"
You could send the output to a file in the assessed directory, D:\sources\SVN_WorkingCopy\filelist.txt, as long as you don't mind that file appearing in it's own contained listing.
These answers also assume that D: is mapped and/or available at the time the script/command is run.
This is probably just an FYI, but you could also do a structured output using tree
tree /f /a "D:\sources\SVN_WorkingCopy" > filelist.txt
which will result in the file structure to be something like:
D:.
├───e-shopper
│ └───Eshopper
| └───temp folder
| └───Performance Report Table.xlsx
| └───Sales Document.docx
├───test Folder
│ └───Hire Freelancers
| └───Performance Report Table.xlsx
| └───Sales Document.docx
└───Some other Directory with no subs or files

DOS DIR - exclude files that start with string

I'm trying to get a list of files from a directory in DOS and I would like to exclude any files that START with "I_"
I can get it to exclude anything with I_ but that excludes it if it doesn't start with it. Here's my script >
dir /s /b c:\TEST | findstr /vi "\c:\TEST\I_." > c:\TEST\List.txt
Anyway to get "I_" to be excluded ONLY when it's the start of the word (ie I_TEST)?
You haven't exactly defined "word," but if you mean that a file or directory name begins a word, then adding the dirsep to the search string should do the trick.
dir /s /b c:\TEST | findstr /vi "\I_" > c:\TEST\List.txt

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

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\"

How to display only files that have alternate data streams in Command Prompt

I know that to display alternate data streams of all the files (if any) in command prompt, this is the command dir /R. But, how do I do it if I only want to display files that have alternate data streams?
dir /s /r | find ":$DATA"
or
dir /r | find ":$DATA"
the first will search in all sub-directories.The second only in current folder. These will show also the ADS assigned to the directory.For only files:
dir /a-d /s /r | find ":$DATA"

Resources