cmd dir info

  • Thread starter Thread starter Tem
  • Start date Start date
T

Tem

I need help writing a script that prints the path of directories that
contains only 1 file.
can this be done with a bat script?
 
Tem said:
I need help writing a script that prints the path of directories that
contains only 1 file.
can this be done with a bat script?

Perhaps you should try alt.msdos.batch.nt

----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /f "tokens=*" %%a in ( ' dir /s c:\106x ^|findstr /i /c:":\\" /c:" 1
file(s)" ' ) do echo %%a|find ":" >nul&if errorlevel 1 (echo !ysd!) else
(for /f "tokens=2*" %%i in ("%%a") do set ysd=%%j)
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.

C:\106x... is my test directory. Modify to suit your relative-root.

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

The above will generate erroneous results for directory names that contain
certain punctuation characters with special meaning to batch, such as "&"
but will be fine for names containing only alphas, numerics, spaces $_#@
etc. This could be solved if required....
 
Thank you for your help.

How can I modify the script so that it searches the dir and sub dirs it
resides in. and also export the path of folders with only 1 file to a text
file?
I'm new to bat script syntax

billious said:
Tem said:
I need help writing a script that prints the path of directories that
contains only 1 file.
can this be done with a bat script?

Perhaps you should try alt.msdos.batch.nt

----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /f "tokens=*" %%a in ( ' dir /s c:\106x ^|findstr /i /c:":\\" /c:"
1 file(s)" ' ) do echo %%a|find ":" >nul&if errorlevel 1 (echo !ysd!) else
(for /f "tokens=2*" %%i in ("%%a") do set ysd=%%j)
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be
removed

The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.

C:\106x... is my test directory. Modify to suit your relative-root.

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

The above will generate erroneous results for directory names that contain
certain punctuation characters with special meaning to batch, such as "&"
but will be fine for names containing only alphas, numerics, spaces $_#@
etc. This could be solved if required....
 
The following will do as you ask -

[BEGIN]

@for /f "tokens=*" %%d in ('dir /ad/s/b') do @dir "%%d" 2^>nul | find "1
File(s)" >nul && @echo %%d

[/END]

.... paste the text between [begin] and [end] above into a batch file
(note that it's a single line, manually remove any line breaks) and run
the batch file from the directory you wish to begin the search at.
 
.... regarding your follow-up question, simply add ">logfile.txt" to the
end of the command, for example, if you named the batch file
"find1dir.cmd", you would run -

C:\Windows>find1dir >\output.log

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


Dean Wells (MVP) said:
The following will do as you ask -

[BEGIN]

@for /f "tokens=*" %%d in ('dir /ad/s/b') do @dir "%%d" 2^>nul | find
"1 File(s)" >nul && @echo %%d

[/END]

... paste the text between [begin] and [end] above into a batch file
(note that it's a single line, manually remove any line breaks) and
run the batch file from the directory you wish to begin the search at.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


Tem said:
I need help writing a script that prints the path of directories that
contains only 1 file.
can this be done with a bat script?
 
billious said:
Tem said:
I need help writing a script that prints the path of directories that
contains only 1 file.
can this be done with a bat script?

Perhaps you should try alt.msdos.batch.nt

----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /f "tokens=*" %%a in ( ' dir /s c:\106x ^|findstr /i /c:":\\" /c:"
1 file(s)" ' ) do echo %%a|find ":" >nul&if errorlevel 1 (echo !ysd!)
else (for /f "tokens=2*" %%i in ("%%a") do set ysd=%%j)
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be
removed

The spaces surrounding the single-quotes are for emphasis only. The
SPACES are not required but the single-quotes ARE required.

C:\106x... is my test directory. Modify to suit your relative-root.

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

The above will generate erroneous results for directory names that
contain certain punctuation characters with special meaning to batch,
such as "&" but will be fine for names containing only alphas, numerics,
spaces $_#@ etc. This could be solved if required....



Tem said:
Thank you for your help.

How can I modify the script so that it searches the dir and sub dirs it
resides in. and also export the path of folders with only 1 file to a text
file?
I'm new to bat script syntax

It already searches the subdirectories.

The starting directory can be changed by changing 'c:\106x' to the directory
you wish. Enclose the name in double-quotes if it contains spaces.

For "the directory where the batch file resides", try

"%~dp0"

in place of c:\106x above

OR, to specify a directoryname on the command line, use

"%~1"

in place of c:\106x above

You can execute the batch by placing it in any directory in your "path"
(simply execute the command

PATH

from the prompt.)

If you use batch regularly, you may wish to establish a dedicated directory
for your .bat files and include it in your PATH permanently. Popular names
include C:\bat , c:\batch and c:\belfry.

To output to a file rather than the screen, add

after the appropriate ECHO keyword

......(echo !ysd!>filename)...

above.

If your filename includes spaces, you'd need to enclose the name in
double-quotes.


Being new to .bat syntax isn't a crime. Top-posting in newsgroups however,
is. Top-posting (replying before the existing message text) is normal for
email. In newsgroups, you're likely to find that persistent top-posting will
lead to your being ignored.

As I said, try the newsgroup alt.msdos.batch.nt where you'll find many
examples and even the occasional FAQ.
 
Back
Top