pipeline - findstr for filename in win7 batch file -
i'm kind of struggling following search. need files misplaced in wrong folders. have following testing structure (in reality hudreds of folders):
c:\test2\john\john_phone.txt c:\test2\mary\john_address.txt c:\test2\mary\mary_address.txt c:\test2\mary\mary_phone.txt c:\test2\mary\peter_address.txt
the files john_address.txt , peter_address.txt misplaced in mary's folder. want check mary's folder misplaced files , list them in separate log file. example above log contain names (paths) of 2 misplaced files, deciding identifier person's name. have piece of code:
@echo off cls set /p name="specify name: " ::forfiles /p "%cd%\%name%" /s /m *.* /c "cmd /c echo @path">>log.txt forfiles /p "%cd%\%name%" /s /m "| findstr /i /v "\*%name%*"" /c "cmd /c echo @path">>log.txt pause
the commented line forfiles works (lists files in folder), have issue findstr: error: files of type "| findstr /i /v *mary*" not found. /v switch findstr should find files not contain specified name, i'm doing wrong while using input forfiles. don't want use dir command since lists additional information , need integrate output larger log file (i need path of misplaced file).
any appreciated. thanks.
edit: easier write code if correct file name fixed this?
all_data_%name%_new.txt
the stuff before , after name fixed , format of file name correct option, else have reported.
for simple solution
@echo off setlocal enableextensions disabledelayedexpansion set "rootfolder=c:\test2" ( /d %%z in ("%rootfolder%\*" ) %%y in ("%%~fz\*" ) /f "tokens=1 delims=_" %%a in ("%%~ny" ) if /i not "%%~nxz"=="%%a" echo %%~fy ) > log.txt
this iterate files under each of folders testing if name starts same string folder name.
edited original proposed solution not work intended, so, removed
edited - name of file has not fixed format, , name of folder can in place, can used
@echo off setlocal enableextensions disabledelayedexpansion set "rootfolder=c:\test2" rem create temporary file hold patterns match against list rem of files determine if correctly placed set "patterns=%temp%\%~nx0.%random%.tmp" > "%patterns%" ( /d %%a in ("%rootfolder%\*") echo(\\%%~nxa\\[^^\\]*%%~nxa[^^\\]* ) rem list of files, , filter not match rem generated patterns dir /s /b /a-d "%rootfolder%\*" ^ | findstr /r /i /c:"%rootfolder:\=\\%\\[^\\]*\\." ^ | findstr /r /i /e /v /g:"%patterns%" ^ > log.txt rem patterns file no longer needed del "%patterns%" >nul 2>nul
Comments
Post a Comment