Page 1 of 1

Recursively Search for Text file and Edit

Posted: 30 Oct 2013 23:27
by Danny
Hello,

I need to search for text file [.txt] in directories and delete the first line from multiple text files inside them.
Folder structure : Root folder ---> Folders from 001 to 100, recursively search range of folders only, please don't edit other folders text files except this (from 001 to 100) and delete the first line of this text files.

Re: Recursively Search for Text file and Edit

Posted: 31 Oct 2013 00:05
by ShadowThief
Is the folder structure
/root/001
/root/002
/root/003
...
/root/100

or is it

/root/001/002/003/.../100 ?

Re: Recursively Search for Text file and Edit

Posted: 31 Oct 2013 00:57
by Danny
Hi,

This structure:
/root/001
/root/002
/root/003
...
/root/100

Re: Recursively Search for Text file and Edit

Posted: 31 Oct 2013 01:38
by foxidrive
Are there any other folders like this in the root:

\root\apple
\root\pear
\root\001
\root\002
\root\003
...
\root\100
\root\zulu

Re: Recursively Search for Text file and Edit

Posted: 31 Oct 2013 02:56
by ShadowThief
Pass the name of the root folder in as a parameter and you should be good to go, provided you don't have any exclamation points in your filenames.

Code: Select all

:5043.bat <root_dir>
:: Searches for folders labelled 001 through 100 in /root_dir, takes all text
:: files inside those directories, and removes the first line, leaving the rest
@echo off
setlocal enabledelayedexpansion

if [%1]==[] echo Please enter a directory.&goto :eof
if not exist %1 echo Please enter a directory.&goto :eof
set root_dir=%1
pushd %root_dir%

:: That's a lot of nested for loops. There's almost certainly a better way, but
:: hopefully I'm at least close.
for /l %%Z in (1,1,100) do (
   set num=%%Z
   if !num! lss 10 set "num=0!num!"
   if !num! lss 100 set "num=0!num!"
   
   pushd !num!
   
   for /f %%X in ('dir /b *.txt') do (
      for /f "skip=1 tokens=1 delims=" %%A in ('findstr /n "^" "%%X"') do (
         for /f "tokens=1,2 delims=:" %%C in ("%%A") do if [%%D]==[] (echo.) else (echo %%D)
      )>>new_%%X
      move new_%%X %%X >nul
   )
   popd
)