Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Batch file to list filenames, ignoring the duplicates

Need some expert's help with this. I need the path of all the files saved to a text file.
I know it is as simple as

Dir H:\folder /a-d /b /s | find /i ".dat" >> output.txt

But my problem arises with duplicates. A file can have a multiple instances I need only the first occurance ECHOed in the output file.

Thanks in advance.
0
mkhan900
Asked:
mkhan900
  • 9
  • 7
1 Solution
 
SteveGTRCommented:
I'm not sure I understand what you want.

"I need the path of all the files saved to a text file"

You want the path only?

You don't want duplicates?

Can you give a sample of what you want?
0
 
mkhan900Author Commented:
Sorry about the confusion.

All files ending with ".dat" should be ECHOed in a text file. Problem is that a file can exists on more then one location. For example when I run DIR H:\FolderName /a-d /b /s | find ".dat" >> OutPut.txt
this line is echoed
H:\FolderName\NameOfTheFolder\Folder\Child_folder\File_Name.dat

File_Name.dat could be found again, if its is found then I want it to be skipped so that any duplicates could be avoided

Thanks for the help
0
 
SteveGTRCommented:
Try this:

@echo off

setlocal

set baseDir=H:\FolderName
set fileMask=*.dat

set indexFile=%cd%\_index.txt
set outFile=%cd%\output.txt

if exist "%indexFile%" del "%indexFile%"
if exist "%outFile%" del "%outFile%"

pushd "%baseDir%"

for /f "tokens=*" %%a in ('dir /a-d /b /s "%fileMask%" 2^>NUL') do call :PROCESS "%%a"

popd

if exist "%indexFile%" del "%indexFile%"

if exist "%outFile%" echo Results in %outFile%&goto :EOF

echo No files found in %baseDir% matching the mask %fileMask%

goto :EOF

:PROCESS

if exist "%indexFile%" (
  for /f "tokens=*" %%a in ('type "%indexFile%" ^| findstr /I [%~nx1]') do echo Skipping %~f1&goto :EOF
)

echo Adding %~f1...
(echo %~f1)>>"%outFile%"
(echo [%~nx1])>>"%indexFile%"

goto :EOF
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
mkhan900Author Commented:
On initiating the program it displays
"The filename, directory name, or volume label syntax is incorrect,"
goes ahead Skipping all the *.dat files
ending with
"The filename, directory name, or volume label syntax is incorrect,"
"Results in" Path
0
 
SteveGTRCommented:
Change the baseDir to the drive and base directory of your processing. My code was programmed using your example directories and I'm assuming that H:\FolderName is made up.
0
 
mkhan900Author Commented:
I did change that before I executed the program
0
 
SteveGTRCommented:
So when you say:

dir h:\foldername

It prints out the directory? Of course the name will be whatever it is you specified it.

What is your baseDir setting?
0
 
mkhan900Author Commented:
set baseDir=c:\cbt
set fileMask=*.dat
I have a few .dat files in c:\cbt\2082
0
 
SteveGTRCommented:
Okay run this and post the complete output. You'll want to cut and past from the DOS box:

REM @echo off

setlocal

set baseDir=c:\cbt
set fileMask=*.dat

set indexFile=%cd%\_index.txt
set outFile=%cd%\output.txt

if exist "%indexFile%" del "%indexFile%"
if exist "%outFile%" del "%outFile%"

pushd "%baseDir%"

for /f "tokens=*" %%a in ('dir /a-d /b /s "%fileMask%" 2^>NUL') do call :PROCESS "%%a"

popd

if exist "%indexFile%" del "%indexFile%"

if exist "%outFile%" echo Results in %outFile%&goto :EOF

echo No files found in %baseDir% matching the mask %fileMask%

goto :EOF

:PROCESS

if exist "%indexFile%" (
  for /f "tokens=*" %%a in ('type "%indexFile%" ^| findstr /I [%~nx1]') do echo Skipping %~f1&goto :EOF
)

echo Adding %~f1...
(echo %~f1)>>"%outFile%"
(echo [%~nx1])>>"%indexFile%"

goto :EOF
0
 
SteveGTRCommented:
past == paste
0
 
mkhan900Author Commented:

C:\>setlocal
C:\>set baseDir=c:\cbt
C:\>set fileMask=*.dat
C:\>set indexFile=C:\\_index.txt
C:\>set outFile=C:\\output.txt
C:\>if exist "C:\\_index.txt" del "C:\\_index.txt"
C:\>if exist "C:\\output.txt" del "C:\\output.txt"
C:\>pushd "c:\cbt"
C:\cbt>for /F "tokens=*" %a in ('dir /a-d /b /s "*.dat" 2>NUL') do call :PROCESS "%a"
C:\cbt>call :PROCESS "C:\cbt\filename.dat"
C:\cbt>if exist "C:\\_index.txt" (for /F "tokens=*" %a in ('type "C:\\_index.txt" | findstr /I [filename.dat]') do echo Skipping C:\cbt\filename.dat  & goto :EOF )
C:\cbt>echo Skipping C:\cbt\filename.dat  & goto :EOF
Skipping C:\cbt\filename.dat
C:\cbt>popd
C:\>if exist "C:\\_index.txt" del "C:\\_index.txt"
C:\>if exist "C:\\output.txt" echo Results in C:\\output.txt  & goto :EOF
Results in C:\\output.txt
0
 
SteveGTRCommented:
I see. The easy fix is to run the batch processing from a directory other than the root directory (c:\). I'll fix this shortly.
0
 
SteveGTRCommented:
@echo off

setlocal

set baseDir=c:\cbt
set fileMask=*.dat

set curDir=%cd%

if "%curDir:~-1,1%"=="\" set curDir=%curDir:~0,-1%

set indexFile=%curDir%\_index.txt
set outFile=%curDir%\output.txt

if exist "%indexFile%" del "%indexFile%"
if exist "%outFile%" del "%outFile%"

pushd "%baseDir%"

for /f "tokens=*" %%a in ('dir /a-d /b /s "%fileMask%" 2^>NUL') do call :PROCESS "%%a"

popd

if exist "%indexFile%" del "%indexFile%"

if exist "%outFile%" echo Results in %outFile%&goto :EOF

echo No files found in %baseDir% matching the mask %fileMask%

goto :EOF

:PROCESS

if exist "%indexFile%" (
  for /f "tokens=*" %%a in ('type "%indexFile%" ^| findstr /I [%~nx1]') do echo Skipping %~f1&goto :EOF
)

echo Adding %~f1...
(echo %~f1)>>"%outFile%"
(echo [%~nx1])>>"%indexFile%"

goto :EOF
0
 
mkhan900Author Commented:
Only works for the first file and goes on skipping the rest of them
0
 
SteveGTRCommented:
I'm a stupid programmer... Sorry about that :)

@echo off

setlocal

set baseDir=c:\cbt
set fileMask=*.dat

set curDir=%cd%

if "%curDir:~-1,1%"=="\" set curDir=%curDir:~0,-1%

set indexFile=%curDir%\_index.txt
set outFile=%curDir%\output.txt

if exist "%indexFile%" del "%indexFile%"
if exist "%outFile%" del "%outFile%"

pushd "%baseDir%"

for /f "tokens=*" %%a in ('dir /a-d /b /s "%fileMask%" 2^>NUL') do call :PROCESS "%%a"

popd

if exist "%indexFile%" del "%indexFile%"

if exist "%outFile%" echo Results in %outFile%&goto :EOF

echo No files found in %baseDir% matching the mask %fileMask%

goto :EOF

:PROCESS

if exist "%indexFile%" (
  for /f "tokens=*" %%a in ('type "%indexFile%" ^| findstr /I /C:[%~nx1]') do echo Skipping %~f1&goto :EOF
)

echo Adding %~f1...
(echo %~f1)>>"%outFile%"
(echo [%~nx1])>>"%indexFile%"

goto :EOF
0
 
mkhan900Author Commented:
AH just one option changed things around.
I wish I was as stupid as your. :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now