Not work as add index on each ".sh" file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
wingcity
Posts: 2
Joined: 22 May 2012 00:42

Not work as add index on each ".sh" file

#1 Post by wingcity » 22 May 2012 01:00

I need to add some index on each ".sh" file. I try to make a bat file but not work.
Anyone can help to fix it? Thanks

@echo off
setlocal enabledelayedexpansion

for /F %i IN ('dir /b/a-d/s *.sh') do (
set FileName=%~ni.sh
set DataFileName=%FileName%
Set LocationDir=Pong\

//index information//@echo ######################################################################### >>d:\%LocationDir%%DataFileName%
@echo # File Name: >>d:\%LocationDir%%DataFileName%
@echo # Calling sql: >>d:\%LocationDir%%DataFileName%
@echo # Type (update/query): >>d:\%LocationDir%%DataFileName%
@echo # Responsible Team: >>d:\%LocationDir%%DataFileName%
@echo # Last updated by: >>d:\%LocationDir%%DataFileName%
@echo # Last updated on (dd-mm-yyyy): >>d:\%LocationDir%%DataFileName%
@echo # Description: >>d:\%LocationDir%%DataFileName%
@echo ######################################################################### >>d:\%LocationDir%%DataFileName%
@echo. >>d:\%LocationDir%%DataFileName%
@echo. >>d:\%LocationDir%%DataFileName%

for /f "tokens=*" %%k in ('type %DataFileName%') do (
set line=%%k
echo !line! >>d:\%LocationDir%%DataFileName%
)
)

jeb
Expert
Posts: 830
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Not work as add index on each ".sh" file

#2 Post by jeb » 22 May 2012 01:52

Hello wingcity,

first, it would be helpful to say which part doesn't work.
We can't help if you don't tell if the result isn't the one you expect or it fails with an error in line xyz or ...

Also it's a good idea to use the CODE button, above the textbox to format your code.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set LocationDir=D:\Pong\

for /F %i IN ('dir /b/a-d/s *.sh') do (
   set FileName=%~ni.sh
   set DataFileName=!FileName!

   (
      rem //index information//@echo #########################################################################
      echo # File Name:
      echo # Calling sql:
      echo # Type (update/query):
      echo # Responsible Team:
      echo # Last updated by:
      echo # Last updated on (dd-mm-yyyy):
      echo # Description:
      echo #########################################################################
      echo.
      echo.
   ) > !LocationDir!!DataFileName!

   rem for /f "tokens=*" %%k in ('type !DataFileName!') do (
   rem      set line=%%k
   rem      echo !line! >>!LocationDir!!DataFileName!
   rem )
)


As you enable the delayed expansion, you should use it in your FOR-block, else your DataFileName expansion will not work.

Btw. It's not clear what you are trying to do with the second FOR/F loop.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Not work as add index on each ".sh" file

#3 Post by foxidrive » 22 May 2012 02:47

The () need to be escaped in the echo-to-file function. Just the ) but I do both so it's obvious what is being done.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set LocationDir=D:\Pong\

for /F %i IN ('dir /b/a-d/s *.sh') do (
   set FileName=%~ni.sh
   set DataFileName=!FileName!

   (
      rem //index information//@echo #########################################################################
      echo # File Name:
      echo # Calling sql:
      echo # Type ^(update/query^):
      echo # Responsible Team:
      echo # Last updated by:
      echo # Last updated on ^(dd-mm-yyyy^):
      echo # Description:
      echo #########################################################################
      echo.
      echo.
   ) > !LocationDir!!DataFileName!

   rem for /f "tokens=*" %%k in ('type !DataFileName!') do (
   rem      set line=%%k
   rem      echo !line! >>!LocationDir!!DataFileName!
   rem )
)

wingcity
Posts: 2
Joined: 22 May 2012 00:42

Re: Not work as add index on each ".sh" file

#4 Post by wingcity » 22 May 2012 03:01

Dear All,

I fixed with following. :D Thanks All.

@echo off

for /F %%i IN ('dir /b/a-d/s *.sh') do (
set "FileName=%%~ni.sh"
call :proc %%i
call :proc2 %%i
)
goto :eof

:proc
Set LocationDir=Pong\
set DataFileName=%FileName%
setlocal enabledelayedexpansion
@echo ############################################################>>d:\%LocationDir%!DataFileName!
@echo # File Name: %FileName% >>d:\%LocationDir%!DataFileName!
@echo # Calling sql: >>d:\%LocationDir%!DataFileName!
@echo # Type (update/query): >>d:\%LocationDir%!DataFileName!
@echo # Responsible Team: >>d:\%LocationDir%!DataFileName!
@echo # Last updated by: >>d:\%LocationDir%!DataFileName!
@echo # Last updated on (dd-mm-yyyy): >>d:\%LocationDir%!DataFileName!
@echo # Description: >>d:\%LocationDir%!DataFileName!
@echo ############################################################ >>d:\%LocationDir%!DataFileName!
@echo. >>d:\%LocationDir%!DataFileName!
@echo. >>d:\%LocationDir%!DataFileName!
endlocal
goto :eof

:proc2
Set LocationDir=Pong\
set DataFileName=%FileName%
setlocal enabledelayedexpansion
for /f "tokens=*" %%k in ('type !DataFileName!') do (
set line=%%k
echo !line! >>d:\%LocationDir%!DataFileName!
)
endlocal
goto :eof

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Not work as add index on each ".sh" file

#5 Post by foxidrive » 22 May 2012 03:37

I think this is functionally equivalent and handles long filenames, and any blank lines in the .sh files.


Code: Select all

@echo off
set "folder=d:\pong"

for /F "delims=" %%a in ('dir /b/a-d/s *.sh') do (

  >"%folder%\%%~nxa" echo ############################################################
>>"%folder%\%%~nxa" echo # File Name: %%~nxa
>>"%folder%\%%~nxa" echo # Calling sql:
>>"%folder%\%%~nxa" echo # Type (update/query):
>>"%folder%\%%~nxa" echo # Responsible Team:
>>"%folder%\%%~nxa" echo # Last updated by:
>>"%folder%\%%~nxa" echo # Last updated on (dd-mm-yyyy):
>>"%folder%\%%~nxa" echo # Description:
>>"%folder%\%%~nxa" echo ############################################################
>>"%folder%\%%~nxa" echo.
>>"%folder%\%%~nxa" echo.

type "%%a" >>"%folder%\%%~nxa"
)

Post Reply