Insert text into column b csv file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Insert text into column b csv file

#16 Post by Ken » 01 Sep 2011 06:13

Trebor68

Wow... This is what I need. One last question (Please) if I want to ask if another input Y or N where would that go in your code?

Thank you again for all your help
Ken

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Insert text into column b csv file

#17 Post by trebor68 » 01 Sep 2011 11:48

Ken,

here is the complete code with changes.
New lines are lines with the word "again".

Please note that you can enter only the rows that have a value (somewhere in the line). New lines at the end of the file are not possible with this batch file.

Code: Select all

@echo off
cls
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set dat=TestFile.csv
set tmpdat=TestFile2.csv
if not exist %dat% goto :error1
set row=0
set eing=n
set again=OK
::
for /f "tokens=1,2* delims=," %%d in (%dat%) do (
  if !again!==n set /p again= Do you want to input value set? Y/N
  if !again!#==y# (set eing=n) & cls
  if !again!#==Y# (set eing=n) & cls
  for /l %%s in (3,1,10) do set cell%%s=""
  set cell1=%%d
  set cell2=%%e
  set /a row+=1
  set help=0
  if %%f#==# set /a help+=1
  if %%f=="" set /a help+=1
  if !eing!==y set help=0
  if not !help!==0 (
    call :input !row! %%d %%e
    echo %%d,%%e,"!cell3!","!cell4!","!cell5!","!cell6!","!cell7!","!cell8!","!cell9!","!cell10!">>%tmpdat%
    set eing=y
    set again=n
  ) else echo %%d,%%e,%%f>>%tmpdat%
)
del %dat%
ren %tmpdat% %dat%
goto :eof
::
:error1
echo.
echo ERROR - File %dat% is not exist.
goto :eof
:input
echo.
echo  Date: %~2    Time: %~3
set /p "cell3=Name    : "
set /p "cell4=Address : "
set /p "cell5=City    : "
set /p "cell6=ZIP-code: "
set /p "cell7= value7 : "
set /p "cell8= value8 : "
set /p "cell9= value9 : "
set /p "cell10= value10 : "
for /l %%t in (3,1,10) do if !cell%%t!=="" set "cell%%t="
goto :eof

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Insert text into column b csv file

#18 Post by Ken » 01 Sep 2011 12:31

Trebor68

This is fantastic...! You really know your stuff. When I select 'N' for the input value it goes into a loop unless I choose another letter then it ends. Could you help me with that last line of your code?

With everything else I should be able to do. And I just want to say that you have been a GREAT GREAT HELP with this.

Thank You Trebor68

Ken

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Insert text into column b csv file

#19 Post by trebor68 » 02 Sep 2011 10:43

Ken

I always use at the end of a routine block the command "goto: eof". But I will not forget where the block ends.
But in the last line really does not need to use this command.

Robert

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Insert text into column b csv file

#20 Post by Ken » 03 Sep 2011 06:28

Hi Robert...
Your code is exactly what I need... Thank you - I am trying to add a Menu selection to allow either to view the Testfile.csv or input information. I am not placing the Menu selection in the right position of your code. My efforts have placed me into a loop of the input data. I just want to add a Menu with the option 1. View the file or 2. Input data or 0. Exit.
Would you please tell me where the correct position I would place this in your code.

example of Menu I am trying to add:
Select Option 1 to view File
Select Option 2 to Input data
Select Option 0 to exit

Thank you again for all your help
Ken

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Insert text into column b csv file

#21 Post by Ken » 03 Sep 2011 07:22

Robert - One more question I would like to add to my previous Post - If I just want to view the Testfile.csv using Option 1. View File. Does it need to Create the Testfile2.csv?.

Thank You again
Ken

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Insert text into column b csv file

#22 Post by Ken » 05 Sep 2011 01:54

Robert... Thanks for all Your help! I figured it out!

I created a Simple menu that calls your file and it checks to see if the Testfile2.csv exist if the file does exist then it displays a message ' File being used - Try again '. This way the data does not get corrupt if multiple people are trying to input.

I am very pleased with all your work and help to resolve my problem.

Thank You again
Ken

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Insert text into column b csv file

#23 Post by trebor68 » 05 Sep 2011 12:21

Hi Ken

Please wait. I have tomorrow more time to solve that problem.

Robert

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Insert text into column b csv file

#24 Post by trebor68 » 06 Sep 2011 11:00

Hi Ken

I have found one error in the last version. If you will input the letter "n" is coming this error. Now is corrected this.
In this version is new the menu and also the procedure to view the value.

In the procedure to view the value of a set, can you see any value set from the first to the last.

Code: Select all

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set dat=TestFile.csv
set tmpdat=TestFile2.csv
if not exist %dat% goto :error1
if exist %tmpdat% del %tmpdat%
::
:: ###  Menu
::
:devmenu
cls
echo.
echo   Menu for Testing with CSV files
echo.
echo   1  View the value from the CSV file
echo   2  Input one or more value sets to the CSV file
echo   0  Exit the Batch
echo.
set "inp="
set /p inp=   Please input one number:
if %inp%#==1# (call :devoutp)
if %inp%#==2# (call :devinp)
if %inp%#==0# goto :eof
goto :devmenu
:: ###  End of menu
::
:: ###  Input one or more value sets
::
:devinp
set row=0
set eing=n
set again=OK
for /f "tokens=1,2* delims=," %%d in (%dat%) do (
  if !again!==n (set "again=") & set /p again= Do you want to input value set? Y/N
  if !again!#==n# (set eing=y) & set again=m
  if !again!#==N# (set eing=y) & set again=m
  if !again!#==y# (set eing=n) & cls
  if !again!#==Y# (set eing=n) & cls
  for /l %%s in (3,1,10) do set cell%%s=""
  set cell1=%%d
  set cell2=%%e
  set /a row+=1
  set help=0
  if %%f#==# set /a help+=1
  if %%f=="" set /a help+=1
  if !eing!==y set help=0
  if not !help!==0 (
    call :input !row! %%d %%e
    echo %%d,%%e,"!cell3!","!cell4!","!cell5!","!cell6!","!cell7!","!cell8!","!cell9!","!cell10!">>%tmpdat%
    set eing=y
    set again=n
  ) else echo %%d,%%e,%%f>>%tmpdat%
)
del %dat%
ren %tmpdat% %dat%
goto :eof
:: ###  End of input one or more value sets
::
:: ###  Output to Screen
::
:devoutp
set row=0
for /f "tokens=1-10 delims=," %%a in (%dat%) do (
  set /a row+=1
  cls
  echo.
  echo    Value from set number !row!
  echo.
  echo    Date: %%~a   Time: %%~b
  echo.
  echo    Name     : %%~c
  echo    Address  : %%~d
  echo    City     : %%~e
  echo    ZIP-Code : %%~f
  echo     value7  : %%~g
  echo     value8  : %%~h
  echo     value9  : %%~i
  echo     value10 : %%~j
  echo.
  pause
)
goto :eof
:: ###  End of Output to Screen
::
:error1
echo.
echo ERROR - File %dat% is not exist.
goto :eof
::
:input
echo.
echo  Date: %~2    Time: %~3
echo.
set /p "cell3=Name    : "
set /p "cell4=Address : "
set /p "cell5=City    : "
set /p "cell6=ZIP-code: "
set /p "cell7= value7 : "
set /p "cell8= value8 : "
set /p "cell9= value9 : "
set /p "cell10= value10 : "
for /l %%t in (3,1,10) do if !cell%%t!=="" set "cell%%t="
goto :eof

Ken
Posts: 34
Joined: 09 Dec 2009 13:47

Re: Insert text into column b csv file

#25 Post by Ken » 06 Sep 2011 15:07

Robert... This is Beautiful work!!

WOW...
WOW...
WOW...

Thank you so much for your time and assistance with this... You have helped me very much.

Ken

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Insert text into column b csv file

#26 Post by trebor68 » 06 Sep 2011 22:08

Here's an idea for access control.

Access is controlled via the variable "COMPUTERNAME" to identify the computer that currently has access to the file.

Code: Select all

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set dat=TestFile.csv
set tmpdat=TestFile2.csv
set accesscontrol=AccessControl.txt
if not exist %dat% goto :error1
if exist %tmpdat% del %tmpdat%
if not exist %accesscontrol% (echo %COMPUTERNAME%>%accesscontrol%) else goto :error2
::
:: ###  Menu
::
:devmenu
cls
echo.
echo   Menu for Testing with CSV files
echo.
echo   1  View the value from the CSV file
echo   2  Input one or more value sets to the CSV file
echo   0  Exit the Batch
echo.
set "inp="
set /p inp=   Please input one number:
if %inp%#==1# (call :devoutp)
if %inp%#==2# (call :devinp)
if %inp%#==0# (del %accesscontrol%) & goto :eof
goto :devmenu
:: ###  End of menu
...

:error2
echo.
for /f %%a in (%accesscontrol%) do echo The computer with computername %accesscontrol% have access to the files.
echo.
pause
goto :eof
...

Post Reply