DosTips.com

A Forum all about DOS Batch
It is currently 02 May 2016 13:23

All times are UTC-06:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
PostPosted: 22 May 2012 01:00 
Offline

Joined: 22 May 2012 00:42
Posts: 2
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%
)
)


Top
   
PostPosted: 22 May 2012 01:52 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 782
Location: Germany
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.


Top
   
PostPosted: 22 May 2012 02:47 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5567
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 )
)


Top
   
PostPosted: 22 May 2012 03:01 
Offline

Joined: 22 May 2012 00:42
Posts: 2
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


Top
   
PostPosted: 22 May 2012 03:37 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5567
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"
)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 14 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® Forum Software © phpBB Limited