Change each line of text to each column of csv file - windows

I have a text file. For example
This is computer
That one is monitor
Some products are printer
I want to save each line of data into each column of csv file as a one row.
So, I try to create a batch file like this.
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
set file=a.txt
set content=
echo %file%
for /F %%i in (a.txt) Do (
set content=!content!,%%i
)
echo %content%>>result.csv
pause
But I get only the first word from every line of text.
When I try to add tokens=* to for loop,
for /F "tokens=*" %%i in (a.txt) Do....
I got the error: The system cannot find the file specified.
I'm not understand whats wrong with my code and I'm not familiar with batch script.

You need to ensure that your protecting spaces in your line content and file names:
#Echo Off
SetLocal EnableDelayedExpansion
Set "fIn=a.txt"
Set "fOut=result.csv"
Set "str="
For /F "UseBackQ Delims=" %%A In ("%fIn%") Do Set "str=!str!,%%A"
If Defined str >>"%fOut%" Echo(%str:~1%
Edit
A slight alternative to cater for some potential problematic characters which may exist in the not provided content of the file:
#Echo Off
SetLocal DisableDelayedExpansion
Set "fIn=a.txt"
Set "fOut=result.csv"
Set "str="
SetLocal EnableDelayedExpansion
For /F "UseBackQ Delims=" %%A In ("%fIn%") Do Set "str=!str!,%%A"
EndLocal & Set/P "=%str:~1%"<Nul>>"%fOut%"

There were several bugs in your code. I've fixed them so now a proper csv string is being generated. However, newer Excel versions actually use ; instead of , to separate cells. This is your code:
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
set file=a.txt
for /F %%i in (%file%) Do (
set content=!content!;%%i
)
set content=!content:~1!
echo %content%>>result.csv
pause

Related

Set An Unknown Number of Variables That areParsed From Each Line of a Text File

Apologies for my poorly worded question and my scatterbrain workings. Essentially I want to set an unknown number of variables that are parsed from each line of a text file.
I have written batch file to create symbolic links for network shares to a C:\Volumes folder.
#echo off
echo:
set /p dest=ENTER FOLDER PATH:
set dest="%dest%"
net use %dest%
if not exist "C:\Volumes" MD "C:\Volumes"
for %%i in (%dest%) do (set "fold=%%~ni")
mklink /d "c:\VOLUMES\%fold%" "%dest%"
pause
What I want to try is the same theory but have the script point at a text file mounts.txt with a list of folder paths and for a for loop to cycle through the list make a symbolic link for each path in the list. I have toyed with counters and cannot get it working correctly. I don't think I am going about it the right way at all.
Contents of mounts.txt
\\10.19.10.238\Masters\Removed bin\Work here
\\10.19.10.241\Scanning\WIP\to process
This does not work:
#echo off
setlocal enableDelayedExpansion
set i=1
:add
Set /a "i+=1"
for /F "tokens=*" %%A in (mounts.txt) do (set dest%i%=%%A)
if exist %dest%%i% goto:add
echo %dest%
echo %dest%%i%
echo !dest!
echo !dest!%i%
pause
Nor this:
#echo off
setlocal enableDelayedExpansion
set i=0
For /F "Tokens=1* Delims=] EOL=" %%A In ('Find /N /V ""^<"mounts.txt"') Do (
set /a i=i+1
set "dest!i!=%%B"
)
For /l %%a in (1,1,4) do echo _dest%%a is !dest%%a!
For /l %%a in (1,1,4) do set dest%%a=!dest%%a!
echo !dest!
pause
I did get something like this to work to an extent but cannot figure out how to use the dest[1], dest[2] as variables in other processes further down in the script.
#echo off
setlocal enabledelayedexpansion
set counter=0
for /f "tokens=*" %%a In (mounts.txt) do (
set /a counter+=1
set "dest[!counter!]=%%a"
)
set dest[
And the list could be added to with many more. If %dest%n variables can be set, the use the same theory to set different %fold% variables based on each %dest%n then maybe the links can be set using the same process as the original script.
Any help is appreciated. Thank you.
If your question actually describes what you want to do, you don't need to set variables. Just use the lines from the text file with a for /f loop:
for /f "delims=" %%i in (mount.txt) do mklink /d "C:\VOLUMES\%%~ni" "%%i"
It's safer to quote the filename, especially if you want/need to use the FQFN. Therefore you need the usebackq option, else a quoted string will be processed as a string, not a filename:
for /f "usebackq delims=" %%i in ("C:\full path\mount.txt") do mklink /d "C:\VOLUMES\%%~ni" "%%i"
You seemingly want to dynamically set the variables as dummy array's and return results based on the line numbers. If so, use the counter you created to become the max of the for /L loop, here is an example by just echoing the results:
#echo off & set cnt=0
setlocal enabledelayedexpansion
for /F "usebackq delims=" %%i in ("mounts.txt") do (
set /a cnt+=1
set "var[!cnt!]=%%i"
)
for /L %%a in (1,1,!cnt!) do echo !var[%%a]!
Edit
As highlighted by #Compo in the comments; after the initial code block where we increment the %cnt% variable, we no longer require using delayedexpansion on the %cnt% variable specifically and can therefore use %cnt% instead of !cnt!:
#echo off & set cnt=0
setlocal enabledelayedexpansion
for /F "usebackq delims=" %%i in ("mounts.txt") do (
set /a cnt+=1
set "var[!cnt!]=%%i"
)
for /L %%a in (1,1,%cnt%) do echo !var[%%a]!
This is a courtesy example, just to show you how you could have still used find.exe, as in one of your examples, and additionally includes a different method of iterating the variables with unknown increments:
#Echo Off
SetLocal EnableExtensions
Rem The next line ensures that there are no variables defined with names beginning _
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
Rem The next lines parse the directory paths content file and defines the incrementing variables
For /F "Tokens=1,* Delims=]" %%G In (
'"%SystemRoot%\System32\find.exe /N "\" 0< "C:\Users\roar\My Directory\mounts.txt" 2>NUL"'
) Do (
Set "_Path%%G]=%%H"
Set "_Name%%G]=%%~nxH"
)
Rem The next lines iterate the defined variables beginning _Path[ and runs commands against them
For /F "Tokens=2 Delims=[]" %%G In ('"(Set _Path[) 2>NUL"') Do (
SetLocal EnableDelayedExpansion
MkLink /D "C:\Volumes\!_Name[%%G]!" "!_Path[%%G]!"
EndLocal
)
Rem The next line undefines any previously defined variables named beginning with _
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
Please note that is likely you may need to run this script elevated, in order for the MkLink command to create the symbolic links.

read multiple paths from a .txt file in a batch file

I have a batch script which requires to read multiple directory paths mentioned in config.txt. I am able to achieve this for one directory path but modified version of it is not working for multiple paths.
Below is the sample which is working fine.
#echo off
for /F "usebackq delims=" %%L in (config.txt) do set "DataPath=%%L"
set "DataPath=%DataPath:/=\%"
echo Application path is: %DataPath%
How to modify this to handle for multiple directory paths.
EDIT:
Below is my attempt to get two paths and '%DataPath%' is printing the value.
#echo off
for /F "usebackq tokens=1,2 delims=" %%L in (config.txt) do set
"DataPath=%%L"&set "filepath=%%M"
set "DataPath=%DataPath:/=\%"
set "filepath=%filepath:/=\%"
echo Application path is: %DataPath%
echo Application path is: %filepath%
Based upon the advice I gave in the comments, which you didn't implement in your edit, the following methodology is what I was expecting you to come up with:
#Echo Off
SetLocal DisableDelayedExpansion
For /F "UseBackQ Delims=" %%L In ("config.txt") Do (
Set "DataPath=%%L"
SetLocal EnableDelayedExpansion
Set "DataPath=!DataPath:/=\!"
Echo Application path is: !DataPath!
EndLocal
)
Pause
This assumes that config.txt contains a list, one per line, of just file paths.
Array version of Path recovery from file.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
Set "_P=0"
FOR /F "tokens=* USEBACKQ" %%a IN (%userprofile%\desktop\logfile.txt) DO (
Set /a _P+=1
Set "Pathway[!_P!]=%%a"
)
::: use the below to take actions with elements in the array.
::: For example, search the array for specific files to delete/copy/move/open.
FOR /L %%e IN (1,1,!_P!) DO (
ECHO Path !%%e! = !Pathway[%%e]!
)
pause

Reading a file with for statment - Batch

I have this code:
#echo off
for /f "delims=" %%a in ("%~dp0\files\text\lead_r.txt") do set /p key= < lead_r.txt
ECHO %key%
PAUSE
What I want it to do is to open a specific text file, read it and then write what's in it to %key%. It works when lead_r.txt and the batch file are in the same folder but it fails when I move lead.txt to \files\text. There are no files with spaces in \files\text so i don't know where the problem is.
Thanks
Hard to understand your goal, but try next code snippet:
#echo off >Nul
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ("%~dp0\files\text\lead_r.txt") do (
rem the same file in next line
rem set /p key=< lead_r.txt
set /p key= < %%a
ECHO !key!
PAUSE
)
endlocal
Here mind EnableDelayedExpansion keyword of setlocal command = Expand variables at execution time rather than at parse time, if used !key! syntax instead of %key% one.

Batch how to read a text file

I need to make a batch file that can read a text file and I am using windows 7 ultimate 32 bit
I am currently using this code:
#ECHO OFF
for /f "delims=" %%x in (test.txt) do set "Var=%%x"
ECHO %Var%
pause
But this only reads the first line and I need it to read the whole thing. I need it to display the full text file.
Try this:
#ECHO OFF
SetLocal EnableDelayedExpansion
for /f "delims=" %%x in ('type test.txt') do (
set "Var=%%x"
ECHO !Var!
)
pause
You need to enclose the for loop with brackets if you are performing multiple commands inside the for loop. Besides that, SetLocal EnableDelayedExpansion will helps to expand the variable at execution time rather than at parse time.
Hope this helps.
Actually, this should read the whole file and set Var to the last line.
If you need to process the whole file, you have several options:
If you just need to do something for every line, then just use a block instead of the set "Var=%%x":
for /f "delims=" %%x in (test.txt) do (
rem Do something with %%x
)
If you need the complete file line-by-line in memory, use a counter and emulate arrays with lots of variables:
setlocal enabledelayedexpansion
set cnt=0
for /f "delims=" %%x in (test.txt) do (
set "ine[!cnt!]=%%x"
set /a cnt+=1
)
set /a numlines=cnt-1
and afterwards you can just use a for /l loop to access them again.

Replace a character till fixed position in a file by batch command

I hava a file. that contains values like
E:\ABC\XYZ1\1231\AAA\SSS\name1.sql
E:\ABC\XYZ2\1232\AAA\TTT\name2.sql
E:\ABC\XYZ3\1233\AAA\UUU\name3.sql
E:\ABC\XYZ4\1234\AAA\YYY\name4.sql
E:\ABC\XYZ5\1235\AAA\ZZZ\name5.sql
and i have to rearrange these values like
#SSS\name1.sql
#TTT\name2.sql
#UUU\name3.sql
#YYY\name4.sql
#ZZZ\name5.sql
(edit - improved format)
Try this (change the name oft the input file):
#echo off &setlocal enabledelayedexpansion
set "fname=test.txt"
for /f "delims=" %%i in (%fname%) do (
set "fname=%%~nxi"
set "fpath=%%~dpi"
set "fpath=!fpath:~0,-1!
for %%j in (!fpath!) do set "fpath=%%~nxj"
echo #!fpath!\!fname!
)
endlocal
.. and if the path structure is always the same use the command line:
for /f "tokens=6,7delims=\" %i in (test.txt) do #echo #%i\%j

Resources