While splitting txt file through batch script Exclamations are ommitting - batch-file

Iam new to batch script and here I tried to split my text file into chunks for each 1 million rows.
Chunk files are generated as I expected, but inside the output file content Iam missing the Exclamations ( ! ) and even it skipping the immediate column after Exclamation. Please help me to get data as it is in original file into chunks!
#ECHO OFF
setLocal DisableDelayedExpansion
set limit=1000000
set feed_name=test.txt
set file=%Tgt_Dir%\%feed_name%
set lineCounter=1
set filenameCounter=1
set name=
set extension=
for %%a in (%file%) do (
set "name=%%~na"
set "extension=%%~xa"
)
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%file%) do (
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
if !lineCounter! gtr !limit! (
set /a filenameCounter=!filenameCounter! + 1
set lineCounter=1
echo Created !splitFile!.
)
echo %%a>> %Tgt_Dir%\!splitFile!
set /a lineCounter=!lineCounter! + 1
)
endlocal
It is a Tab delimiter file.
ScreenShot

You need to toggle delayed expansion.
setlocal DisableDelayedExpansion
for /f "tokens=*" %%a in (%file%) do (
Set "line=%%a"
setlocal EnableDelayedExpansion
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
echo(!line!>> %Tgt_Dir%\!splitFile!
if !lineCounter! gtr !limit! (
ENDLOCAL
set /a filenameCounter+=1
set lineCounter=1
echo Created file
) ELSE ENDLOCAL
set /a lineCounter=lineCounter + 1
)

Related

Batch script - Wrong If syntax

I am trying to check the text in my created array, if I am not using "if" every thing works and i can use "echo" but when I add the "if" command I get "wrong syntax"
#echo off
setlocal ENABLEDELAYEDEXPANSION
set i=0
for /f "delims= " %%a in ('command') do (
set /A i+=1
set list[!i!]=%%~a
)
set Filesx=%i%
rem Display array elements
for /L %%i in (1,1,%Filesx%) do (
if list[%%i] =="some ttext"
echo !list[%%i]!
)
I would consider changing your script accordingly:
#Echo Off
SetLocal EnableDelayedExpansion
Set "i=0"
For /F %%A In ('command') Do (Set/A i+=1
Set list[!i!]=%%~A)
Rem Display array elements
For /L %%A In (1,1,%i%) Do If /I "!list[%%A]!"=="some ttext" Echo !list[%%A]!
Pause
In your script you needed to change if list[%%i] to If /I "!list[%%i]!"
#echo off
setlocal ENABLEDELAYEDEXPANSION
set i=0
for /f "delims= " %%a in ('command') do (
set /A i+=1
set list[!i!]=%%~a
)
set Filesx=%i%
rem Display array elements
for /L %%i in (1,1,%Filesx%) do (
if /i "!list[%%i]!" =="some ttext" (
echo !list[%%i]!
)
)
An if statement requires an exact match (for == or equ) so if you quote one side, you need to quote the other.
also, the action for the if-true condition must be on the same physical line as the if

I need a format of code that only counts odd lines

test_title.bat
:GET_DOWNLOADS
set Counter=-1
for /f "DELIMS=" %%i in ('type version.txt') do (
set /a Counter+=2
set "Line_!Counter!=%%i"
)
if exist version.txt del version.txt
exit /b
:list_files
call :GET_DOWNLOADS
For /L %%C in (1,2,%Counter%) Do (
:: removing this part makes it work fine
set line=%%C
set /a line+=1
set /a line/=2
:: alternate way doesnt work either
REM set /a line=%line% / 2
:: this part without the math part would be %%C instead of %Line%
echo %line%. !Line_%%C!
)
pause
(made an edit)
the second part isnt working for some reason
it just crashes
if i remove the line that does the math it works fine but instead display 1. 3. 5. 7.
version.txt
everything
0
minecraft
0
steam
0
obs
0
fixed test_list.bat :D
#echo off
setlocal enabledelayedexpansion
set "num=1"
set "counter=0"
for /f "DELIMS=" %%i in (version.txt) do (
set /a num+=1
if "!num!"=="2" (set /a counter+=1&set "line_!counter!=%%i"&set num=0)
)
echo.
For /L %%C in (1,1,%Counter%) Do (echo %%C. !Line_%%C!)
pause
#echo off
setlocal enabledelayedexpansion
set "num=1"
set "counter=0"
for /f "DELIMS=" %%i in (version.txt) do (
set /a num+=1
if "!num!"=="2" (set /a counter+=1&set "line_!counter!=%%i"&echo %%i&set num=0)
)
echo.
set line_1
set line_2
set line_3
pause
Would output:
everything
minecraft
steam
obs
line_1=everything
line_2=minecraft
line_3=steam

How can I echo nested variables inside a for loop?

Here is a for loop to save each line of a text file to its own variable:
#echo off
setlocal enableextensions enableDelayedExpansion
set count=0
for /f "tokens=*" %%a in (file.txt) do (
set /a count=!count! + 1
set var_!count!=%%a
)
endlocal
I would like to save the content of each variable to a separate new text file. How can I do this? I tried the following. But it does not work, because !var_!count!! is a variable inside a variable.
#echo off
setlocal enableextensions enableDelayedExpansion
set count=0
for /f "tokens=*" %%a in (file.txt) do (
set /a count=!count! + 1
set var_!count!=%%a
echo !var_!count!!>file_!count!.txt
)
endlocal
#echo off
setlocal enableextensions enableDelayedExpansion
set count=0
for /f "tokens=*" %%a in (file.txt) do (
set /a "count+=1"
set "var_!count!=%%a"
for %%b in (!count!) do >file_!count!.txt echo !var_%%b!
)
endlocal

Rename with bat file from an specific number

I have a bat file that rename all of the png files to data_ files. It starts from data_1 and data_2 .... I want to start with data_140 and data_141...
How to do that?
:: Renaming files
for %%a in (*.png) do (
set /a count+=1
set "fname=%%~a"
setlocal enabledelayedexpansion
ren "!fname!" data_!count!.png
endlocal
)
I don't see the problem. SET /A count+=1 starts with count=0 as it's not defined yet. If you want your script to start at 140 just do set count=139 before you enter the loop:
:: Renaming files
set count = 139
for %%a in (*.png) do (
set /a count+=1
set "fname=%%~a"
setlocal enabledelayedexpansion
ren "!fname!" data_!count!.png
endlocal
)

how to get the output in column wise in a below batch file

how to get the output in column wise in a below batch file
#echo off
setlocal enableextensions enabledelayedexpansion
set Counter=0
for /f "usebackq tokens=2,5,6 delims= " %%a in (`findstr /c:"Cod "
1231.txt`) do (
set x=%%b
set x=!x:~3!
set y=%%c
if %%c LSS 10 set y=!y:~1!
set item!Counter!=%%a-!x!#!y!
set /a Counter+=1
)
set result=%item0%
for /l %%i in (1,1,!Counter!) do set result=!result!!item%%i!
FOR /F %%A IN ('CHCP') DO SET CHCP=%%A
echo %result% >>result.txt
endlocal
Looks like you are concatenating the values into the result variable.
Rather than:
... do set result=!result!!item%%i!
Why not output the value directly to your output file:
... do echo !item%%i!>>result.txt

Resources