suppressing dir output

D

destatnj2

Hi all,

Try this at the command prompt in 2000/XP:

dir /b X

where X is any filespec which does NOT exist. The output for this is
always: File Not Found. Is it possible to suppress this output? I've
tried:

dir /b X >nul

or

@echo off
@dir /b X >nul

but to no avail. Can anyone help? I would like to suppress this output for
a batch file I wrote at work. Thanks in advance.

Pete
 
D

David S

dir /b X 2> nul

%Windir%\Help\ntcmds.chm in XP and search for redirect, read "Using command
redirection operators", 2> redirects standard error.

David
 
D

destatnj2

David,

Perfect. Thank you for your response and the tip about ntcmds.chm which I
did not know about. I will consult that in the future before posting here.

Pete
 
R

Ray at

Are you looking to find out while it always returns File Not Found when the
file exists, or do you just want to supress the response?

dir /b X>nul 2>&1

Ray at home
 
M

Matthias Tacke

:
Perfect. Thank you for your response and the tip about ntcmds.chm
which I did not know about. I will consult that in the future before
posting here.
Pete
See my signature to do it with less typing.
 
D

destatnj2

Hi Ray,

I'm not sure I understand your question exactly. In your example you're
sending stdout and stderr to a nul device which achieves the same result as
David's dir /b X 2>nul regarding stderr suppression. The problem for me is
that I have a temp directory with an arbitrary number of files with distinct
filenames. I must search that directory for one of 24 distinct file names,
and if I find anyone of those 24, I must rename it to a specified format and
move it to a specific directory for that particular file. Since I can never
be sure what files will be in the temp directory, I must search for each of
the 21 everytime I look. So after I find one, rename and move it, the next
time I search I will get File Not Found since it's just been moved.
Sometimes the temp directory has a large number of files, and after renaming
and moving 20-30 of them, I will get 20-30 File Not Found messages which I
don't want or need.

Pete
 
R

Ray at

I'm not sure why I didn't notice you already had the answer before I posted
a 50% (at best) accurate reply. Please disregard my answer! :]

Ray at work
 
W

wadester

destatnj2 said:
Hi Ray,

I'm not sure I understand your question exactly. In your example you're
sending stdout and stderr to a nul device which achieves the same result as
David's dir /b X 2>nul regarding stderr suppression. The problem for me is
that I have a temp directory with an arbitrary number of files with distinct
filenames. I must search that directory for one of 24 distinct file names,
and if I find anyone of those 24, I must rename it to a specified format and
move it to a specific directory for that particular file. Since I can never
be sure what files will be in the temp directory, I must search for each of
the 21 everytime I look. So after I find one, rename and move it, the next
time I search I will get File Not Found since it's just been moved.
Sometimes the temp directory has a large number of files, and after renaming
and moving 20-30 of them, I will get 20-30 File Not Found messages which I
don't want or need.

If that's all you are doing, why not forego restarting the search
after finding each file and just search for each of those 24 files
directly? This way you're done after a single pass, and no stderr
redirection is needed (not that it's bad, just another way to solve
your problem):

if exist c:\temp\file1.txt ren c:\temp\file1.txt foo1.txt
if exist c:\temp\file2.txt ren c:\temp\file2.txt foo2.txt
if exist c:\temp\file3.txt ren c:\temp\file3.txt foo3.txt
if exist c:\temp\file4.txt ren c:\temp\file4.txt foo4.txt

ws
 
D

destatnj2

Wadester,

True, however I didn't fully explain the environment I'm dealing with. The
people who upload the files occasionally mistype filenames etc. There's
only one thing constant about the filenames: the string "-Region " (that's
hyphen, Region and a space. "Region" is a code for a region in the US. For
instance, "*-Atl *.xls" goes to U:\...\Atlanta or "*-OHV *.xls" would go to
U:\...\Ohio Valley etc.) So I search for each one of those regions by the
form just stated given the often varying filenames. I currently use for
loops:

For /f "tokens=*" %%B In ('dir /b "*-Atl *.xls" ^2^>nul') Do IF
"%%B"==%OldFileName% (move %OldFileName% "%NXX_Stats%\Atlanta\
NewFileName%")

One for each location. Not all locations will be there on any given day so
"File Not Found" to stderr is inevitable. Also there could be multiple
files for a particular region such as: ABC-d-Atl 123456.xls and EFG-h-Alt
654321.xls as well.

Pete
 
M

Matthias Tacke

destatnj2 said:
Wadester,

True, however I didn't fully explain the environment I'm dealing with. The
people who upload the files occasionally mistype filenames etc. There's
only one thing constant about the filenames: the string "-Region " (that's
hyphen, Region and a space. "Region" is a code for a region in the US. For
instance, "*-Atl *.xls" goes to U:\...\Atlanta or "*-OHV *.xls" would go to
U:\...\Ohio Valley etc.) So I search for each one of those regions by the
form just stated given the often varying filenames. I currently use for
loops:

For /f "tokens=*" %%B In ('dir /b "*-Atl *.xls" ^2^>nul') Do IF
"%%B"==%OldFileName% (move %OldFileName% "%NXX_Stats%\Atlanta\
NewFileName%")

One for each location. Not all locations will be there on any given day so
"File Not Found" to stderr is inevitable. Also there could be multiple
files for a particular region such as: ABC-d-Atl 123456.xls and EFG-h-Alt
654321.xls as well.

Pete
I'd change the logic a bit. Store alle the Identifiers in a text file

for /"tokens=*" %%B in ('dir /b *.xls^|findstr /G:Ident.txt') do ( ...)

This way you select only files matching any identifier and can then
decide how to proceed.
 
D

destatnj2

Matthias,

On second thought, I don't think that makes things much easier. The problem
is this: there are 24 identifiers, and for each identifier, there is a
specific corresponding folder. So with your code it will loop through the
list of all files in the directory that match an identifier, but it won't be
able to tell which identifier it's currently using and therefore identify
the correct folder to send the file to. Where as explicitly stating the
identifier each time will (ie. "*-Atl *.xls" goes to U:\...\Atlanta etc.).
As far as I know, there is no way with an IF statement to check for this.
For instance, using your code:

for /f "tokens=*" %%B in ('dir /b *.xls^|findstr /G:Ident.txt') do ( if
[%%B]==[*-Atl *.xls] (move %oldFileName% U:\...\Atlanta\%newFileName%) )

This would be nice.

Pete
 
M

Matthias Tacke

destatnj2 said:
Matthias,

On second thought, I don't think that makes things much easier. The problem
is this: there are 24 identifiers, and for each identifier, there is a
specific corresponding folder. So with your code it will loop through the
list of all files in the directory that match an identifier, but it won't be
able to tell which identifier it's currently using and therefore identify
the correct folder to send the file to. Where as explicitly stating the
identifier each time will (ie. "*-Atl *.xls" goes to U:\...\Atlanta etc.).
As far as I know, there is no way with an IF statement to check for this.
For instance, using your code:

for /f "tokens=*" %%B in ('dir /b *.xls^|findstr /G:Ident.txt') do ( if
[%%B]==[*-Atl *.xls] (move %oldFileName% U:\...\Atlanta\%newFileName%) )

This would be nice.

Pete
My suggesstion was intended to be a solution for your "file not found",
and a timesaver - both is true.
If you iterate all the id's each time with a dir it should take some
more time.
I have to think a bit on a solution.
Shall the file name stay the same when moving ?
Are there any other minus signs in the filename?
Is there _always_ a space after the three letter code?
 
D

destatnj2

Matthias,

I'm not sure there really is a clean solution to this, given the arbitrary
parameters of the problem. As previously stated, each identifier has it's
own folder. However, there is an arbitrary number of files for each
identifier. For instance, the identifier "*-Atl *.xls" will return true for
"ABC-d-Atl 123456.xls" as well as for "EFG-h-Atl 654321.xls" or for that
matter, anything, as long as it contains the search string. So if you only
checked once you would miss the second file. Therefore, you need to keep
checking until none are left. Again, the only thing constant in the
filenames is "-Region " where region is the location code. The file name is
not changed until it is moved, where it's original name and corresponding
folder that it will be sent to have been verified first. I have a working
solution at this point which incorporates David S's code where the unwanted
output is dicarded. But from a coding standpoint, readability isn't the
best and efficiency isn't optimum either...

Pete

Matthias Tacke said:
destatnj2 said:
Matthias,

On second thought, I don't think that makes things much easier. The problem
is this: there are 24 identifiers, and for each identifier, there is a
specific corresponding folder. So with your code it will loop through the
list of all files in the directory that match an identifier, but it won't be
able to tell which identifier it's currently using and therefore identify
the correct folder to send the file to. Where as explicitly stating the
identifier each time will (ie. "*-Atl *.xls" goes to U:\...\Atlanta etc.).
As far as I know, there is no way with an IF statement to check for this.
For instance, using your code:

for /f "tokens=*" %%B in ('dir /b *.xls^|findstr /G:Ident.txt') do ( if
[%%B]==[*-Atl *.xls] (move %oldFileName% U:\...\Atlanta\%newFileName%) )

This would be nice.

Pete
My suggesstion was intended to be a solution for your "file not found",
and a timesaver - both is true.
If you iterate all the id's each time with a dir it should take some
more time.
I have to think a bit on a solution.
Shall the file name stay the same when moving ?
Are there any other minus signs in the filename?
Is there _always_ a space after the three letter code?

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
M

Matthias Tacke

destatnj2 said:
Matthias,

I'm not sure there really is a clean solution to this, given the arbitrary
parameters of the problem. As previously stated, each identifier has it's
own folder. However, there is an arbitrary number of files for each
identifier. For instance, the identifier "*-Atl *.xls" will return true for
"ABC-d-Atl 123456.xls" as well as for "EFG-h-Atl 654321.xls" or for that
matter, anything, as long as it contains the search string. So if you only
checked once you would miss the second file. Therefore, you need to keep
checking until none are left. Again, the only thing constant in the
filenames is "-Region " where region is the location code. The file name is
not changed until it is moved, where it's original name and corresponding
folder that it will be sent to have been verified first. I have a working
solution at this point which incorporates David S's code where the unwanted
output is dicarded. But from a coding standpoint, readability isn't the
best and efficiency isn't optimum either...

Pete
Hello Pete,
try this batch. It has a different findstr with a regular expresion. It
worked in my test environment. You can change it back to ident.txt
version. You've to fit the move statements to your needs.
The batch try's 5 times to strip up to the hypen and then echo's a
message.

::RegionMove.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
for /f "tokens=*" %%B in (
'dir /b *.xls^|findstr /R "\-[A-Z][A-Z][A-Z][^A-Z0-9]"') do (
set fn=%%B
set /A cnt=0
call :Xtract "%%B")
goto :eof
:Xtract
set fn=%fn:*-=%
set /A cnt+=1
set Reg=%fn:~0,3%
if /I "%Reg%"=="ATL" echo MOVE "%~f1" ATLANTA & goto :eof
if /I "%Reg%"=="OHV" echo MOVE "%~f1" OHIO VALLEY & goto :eof
::no match so far, strip to next -
if %cnt% lss 5 goto :Xtract
echo Houston,we have a problem with [%fn%] %~1
::RegionMove.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top