I have some data exported through a batch file from my sql server database in tab delimited format. How can I add a column to the file through a batch script with the heading "channel" and the text "channel1" to each row beneath the heading?
#echo off
setlocal
set "file_path=F:\scriptests\textsql\Book1.txt"
break>tempfile
rem setlocal enableDelayedExpansion
set "firstline="
for /f "useback tokens=* delims=" %%A in ("%file_path%") do (
if not defined firstline (
rem after %%A the symbol is tab but not space
echo %%A "channel"
set "firstline=."
) else (
rem after %%A the symbol is tab but not space
echo %%A "channel1"
)
)>>tempfile
rem first check created tempfile before uncomment the line bellow
rem move /y tempfile "%file_path%"
endlocal
Related
I'm trying to make a program that types out the clipboard, word by word. So far, I have done everything except work out how to escape the double quote in the VBS file. To do this, I need another double quote. This is my code as of now:
#echo off & setlocal & cd "%temp%" & paste > x.txt
for /f "delims=" %%A in (x.txt) do set X=%%A & timeout /t 4 /nobreak
:loop
for /f "tokens=1*" %%A in ("%X%") do (
set "Y=%%A" & set X=%%B
)
set Z=%Y:^"=^"^"%
echo CreateObject("Wscript.Shell").SendKeys "%Z% " > x.vbs
start /wait x.vbs & if defined Y (set "Y=" & goto :loop) & pause
I am obviously doing something wrong on this line:
set Z=%Y:^"=^"^"%
You dont need to escape the quotes:
set z="a--b"
echo %z:"=""%
I want to place a variable before text on the same line without pushing the text forward when the variable changes its amount of characters.
Eg.
#echo off
set var=1
:start
cls
echo Variable: %var% 'Text'
echo ' '
pause >nul
set /a var= %var% * 10
goto start
Every time the variable expands the text shifts to the right, is it possible to keep the text in the same place?
set your variable to a known lenght (adding spaces to it and cutting at a fixed length
#echo off
set "spaces= "
set var=1
:start
cls
set var=%var%%spaces%
set var=%var:~0,15%
echo Variable: %var%'Text'
echo %spaces:~0,15%' '
pause >nul
set /a var= %var% * 10
goto start
#ECHO Off
SETLOCAL enabledelayedexpansion
SET "string= Text"
SET /a var=1
:loop
FOR /l %%a IN (1,1,30) DO IF "!var:~%%a,1!"=="" SET "newstr=%var%!string:~%%a!"&GOTO foundlen
:foundlen
ECHO %newstr%
SET /a var*=10
IF %var% gtr 0 GOTO loop
GOTO :EOF
Assumes the length of var is less than 30.
Varies %%a until %%a is the length of the prefix string and then strings the prefix to the other string, removing %%a characters.
If the prefix string var may be empty, then
set "newstr=%string%"
if defined var for /l ...
I chose to force the loop to terminate by exploiting the 31-bit nature of integer mathematics in cmd - if the number grows >2**31 then it may appear negative. This is simply a way to terminate the loop rather than explicitly checking that it has the maximum allowable length.
I have a windows batch file that reads in various lines of a text file for processing. During the execution I have to open another text file that has only one line in it. The line contains a serial number with about 3 or 4 spaces after the end.
I need to read in this file called (value.txt), grab the data in the first line and assign the value to a variable without any spaces at the end.
After that I need to add the .pdf extension to this variable and set to another variable.
I then need to then check if the variable with the pdf extension exists in the current directory as a file name. If it does then can I change the variable value to add -1 before the .pdf. If this file still exists then try -2 etc.
Thus
File name read in called value.txt
55400TERM1 (variable VAR1 is assigned with no spaces)
55400TERM1.pdf (variable VAR2 is set with the .pdf extension)
55400TERM1-1.pdf (variable VAR2 is updated if 55400TERM.pdf exists)
55400TERM1-2.pdf (variable VAR2 is updated if 55400TERM1.pdf exists)
Etc etc - loops until it cannot file a existing file with the variable value.
Here is a comment batch script for this task:
#echo off
rem If text file with file name does not exist in current
rem directory, exit this batch file as nothing can be done.
if not exist value.txt (
echo %~f0: Missing file "value.txt" in "%CD%".
goto :EOF
)
rem Read first line from text file. Command FOR automatically splits up the
rem line into strings using space and horizontal tab character as string
rem delimiter. As the line contains just one string with trailing spaces
rem most likely caused by wrong redirecting an output into the text file
rem the single string is assigned to the variable with no trailing spaces.
for /F %%I in (value.txt) do (
set "FileName=%%I"
goto CheckFile
)
echo %~f0: No string in file "value.txt" in "%CD%".
goto :EOF
:CheckFile
if exist "%FileName%.pdf" goto FindLatestFile
echo No file "%FileName%.pdf" found in "%CD%".
set "FileName=%FileName%.pdf"
goto ProcessFile
rem The command DIR as used here lists all files matching the wildcard
rem specification in reverse order according to last modification date.
rem This batch file assumes that the file with highest number is the
rem newest modified file. This speeds up determining the file with the
rem highest number in file name. For an alphabetical reverse order it
rem would be necessary that all files have the same number of digits,
rem i.e. the files are *-01.pdf, *-02.pdf, ..., *-10.pdf, *-11.pdf as
rem otherwise the alphabetical order would be *-1.pdf, *-10.pdf, *-11.pdf,
rem *-2.pdf, ..., *-9.pdf and then last file would be determined wrong.
:FindLatestFile
for /F %%I in ('dir /O-D /TW /B "%FileName%-*.pdf" 2^>nul') do (
set "LastFileName=%%~nI"
goto FoundLatestFile
)
echo No file "%FileName%-*.pdf" found in "%CD%".
set "FileName=%FileName%-1.pdf"
goto ProcessFile
:FoundLatestFile
for /F "tokens=2 delims=-" %%I in ("%LastFileName%") do set "LastFileNumber=%%I"
set /A LastFileNumber+=1
set "FileName=%FileName%-%LastFileNumber%.pdf"
:ProcessFile
echo Next file is: %FileName%
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
dir /?
for /?
goto /?
if /?
rem /?
set /?
#echo off
set varno=1
for /r %%A in (*.txt) do call :loopdo "%%A"
goto end
:loopdo
set var=%1
set var=%var:*libraries\=%
set var%varno%=%varno%. %var:.txt"=%
set /a varno=%varno%+1
goto :eof
:end
set ifno=1
if %ifno% LSS %varno% (
echo %var%%ifno%
set /a ifno=%ifno%+1
)
pause
How could I make this echo %var1% %var2% etc?
At the moment it just echoes textfile1.txt"1 textfile2.txt"2 and I can see why but I don't know how to make it do the above.
I have tried %varifno% and %%var%ifno%% but they don't work - again I can see why.
If you want to evaluate variables within a variable name, setlocal enabledelayedexpansion. Also, use variable expansion to strip the .txt off the end of the filename within your :loopdo subroutine. You're pretty close otherwise.
#echo off
set varno=1
for /r %%A in (*.txt) do call :loopdo "%%~pnA"
goto end
:loopdo
set "var=%~1"
set "var=%var:*libraries\=%"
set /a varno+=1
goto :eof
:end
set ifno=1
if %ifno% LSS %varno% (
setlocal enabledelayedexpansion
echo !var%ifno%!
endlocal
set /a ifno+=1
)
pause
This is untested, as your use of var%varno% and %var%%ifno% is a little confusing. I don't see that you're ever actually setting a variable called %var%%ifno%. Hopefully the changes I made will lead you in the right direction, if it doesn't provide the complete solution.
Hi im trying to batch append information from one file to another but without success
heres the code
#echo off
set backup=C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN"
copy C:\"VTS\Advanced Tools\Advanced Offset\AP\LOADING SQUENCE.AP" %backup%
the issue is that the backup is not actually C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN" but an actual other location stored in there.
basicaly i want to set the contents of the file C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN" as my backup variable
Use the command lines below. set /p V=<FILE will set the value of the variable V to the content of FILE.
set /p backup=<C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN"
copy C:\"VTS\Advanced Tools\Advanced Offset\AP\LOADING SQUENCE.AP" %backup%
give this a try
#echo off
setlocal enabledelayedexpansion
for /f %%a in ("C:\VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN") do (
set backup=%%a
echo copy "C:\VTS\Advanced Tools\Advanced Offset\AP\LOADING SQUENCE.AP" "!backup!"
goto :eof
)
and remove the echo after testing