View unanswered posts | View active topics It is currently 26 Oct 2014 00:54



Post new topic Reply to topic  [ 5 posts ] 
Not work as add index on each ".sh" file 
Author Message

Joined: 22 May 2012 00:42
Posts: 2
Post Not work as add index on each ".sh" file
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%
)
)


22 May 2012 01:00
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 679
Location: Germany
Post Re: Not work as add index on each ".sh" file
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:
@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.


22 May 2012 01:52
Profile

Joined: 10 Feb 2012 02:20
Posts: 4323
Post Re: Not work as add index on each ".sh" file
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:
@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 )
)


22 May 2012 02:47
Profile

Joined: 22 May 2012 00:42
Posts: 2
Post Re: Not work as add index on each ".sh" file
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


22 May 2012 03:01
Profile

Joined: 10 Feb 2012 02:20
Posts: 4323
Post Re: Not work as add index on each ".sh" file
I think this is functionally equivalent and handles long filenames, and any blank lines in the .sh files.


Code:
@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"
)


22 May 2012 03:37
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


Who is online

Users browsing this forum: Google [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.