Batch - read array - arrays

I have a problem witch .bat file
I have somethis like this:
#echo off
setlocal EnableDelayedExpansion
set arr[a]=1
set arr[b]=2
for /F "tokens=*" %%a in (somefile.txt) do (
set key=a
echo /!key!/ /!arr[%key%]!/
)
And code above not work correctly. I mean that the key is display correct but value of !arr[%key%]! is empty (i don't know why).
When i try it like this:
#echo off
setlocal EnableDelayedExpansion
set arr[a]=1
set arr[b]=2
set key=a
for /F "tokens=*" %%a in (somefile.txt) do (
echo /!key!/ /!arr[%key%]!/
)
Code above work good. No idea why first code doesn't work and second work. Any ideas ?

#echo off
setlocal EnableDelayedExpansion
set arr[a]=1
set arr[b]=2
for /F "tokens=*" %%a in (q27861004.txt) do (
set key=%%a
echo /!key!/ /!arr[%%a]!/
)
GOTO :EOF
I used a file named q27861004.txt containing this data for my testing.
a
b
Your problem is that %kay% is evaluated at parse-time, replacing %key% with its value at that time, being empty, hence displaying !arr[]! also empty

Related

Loop through files in folder, replace content and Save as new Name

I need to loop through filed end with .edi in my folder, replace a character in the content and then save the file in another folder with "_updated"on the end.
e.g.
C:/Test/FileName.edi replace ' in file with ^ and save the file into C:/Test/Output/FileName_Updated.edi
I've tried the following code and it works up until the saving the filename part, I've got confused somewhere, I don't usually write batch scripts:
#echo off
setlocal enabledelayedexpansion
for %%f in (C:\Test\*.edi) do (
set "input=C:\Test\"
SET "output=C:\Test\Output\"
for %%a in (%%f) do (
set "output=%output%%%~na_update.%%~xa"
)
(for /f "delims=" %%i in (%%f) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:'=^!"
echo(!line!
endlocal
)))>> %output%
)
Figured it out with help from aschipfl
#echo off
setlocal enabledelayedexpansion
for %%f in (C:\Users\CHRW\Desktop\EDILocalTest\*.edi) do (
SET "output=C:\Users\CHRW\Desktop\EDILocalTest\Output\"
for %%a in (%%f) do (
set "outputfile=!output!%%~na_update%%~xa"
)
(for /f "delims=" %%i in (%%f) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:'=^!"
echo(!line!
endlocal
))>> !outputfile!
)
#echo off
setlocal enabledelayedexpansion
for %%f in (C:\Test\*.edi) do (
set "input=C:\Test\"
SET "output=C:\Test\Output\"
(for /f "delims=" %%i in (%%f) do (
set "line=%%i"
rem setlocal enabledelayedexpansion
set "line=!line:'=^!"
echo(!line!
rem endlocal
)))>> "%output%%%~nf_update.%%~xf"
)
Since you were changing the value of output within the loop, the redirection should have been to !output! not %output% - the changed value of output, not the value of output at the time the outer for loop was parsed.
Since %%f contains a filename, there's no need to re-parse it. Easier to construct the output filename in-line
Since delayedexpansion has been invoked at the start of the procedure, there's no need to re-invoke it and close the new invocation. I've REMmed-out those lines as it may be that you are presenting abbreviated code.

Batch get filename from path

Hi I don't have much experience in batch-programming and have a problem. I have a .bat script that reads a file with a list of paths and i want to get the filename of these paths. I use the script in cygwin.
My code in the Script:
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("%value%") DO SET MYPATHFILE=%%~nxI
)
When i run the Script %value% is empty.
Value of error1.txt:
a/b/c/d/TextIWant
you need delayed expansion or you can directly use %%a:
for /F %%a in (error1.txt) do (
FOR /F %%I IN ("%%a") DO SET MYPATHFILE=%%~nxI
)
or
setlocal enableDelayedExpansion
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("!value!") DO SET MYPATHFILE=%%~nxI
)
It looks like you will need Delayed Expansion.
The current problem is, that you want to use a variable in the same set of brackets where you changed the value in (the surrounding For-Loop).
Add setlocal EnableDelayedExpansion to your code at the top and change the %value% to !value!
You can test the problem yourself with this code:
#echo off
setlocal EnableDelayedExpansion
set foo=bar
For /L %%a (1,2,1) do (
set foo=foobar
echo.old value %foo%
echo.new value !foo!
)
I hope it helped :)
Greetings
geisterfurz007

remove a particular filename from the output of for loop

I have various files date wise as:
cpms_2015_09_01.txt
lms_2015_07_10.txt
kmps_2015_10_07.txt
lmps_2015_10_07.txt
cpmgs_2015_10_07.txt
I wanted to remove from the "for loop" files of today's date name
How can I do this, I wrote a code for which I can store the file paths of all files in:
XCOUNT_1=D:\check\cpms_2015_09_01.txt ...etc
But, there shouldn't be no XCOUNT for today's file name like %_2015_10_07%
I am struck at this section of code pointed below
DO ( IF NOT "_%year%_%day%_%month%.txt"=="!FTRIM_%%J!"
My code is below
SETLOCAL ENABLEDELAYEDEXPANSION
setlocal
SET /A MAXJ=1
SET /A J=1
echo %DATE%
set year=%date:~-4,4%
set year=%year: =%
set month=%date:~7,2%
set month=%month: =%
set day=%date:~4,2%
set day=%day: =%
FOR /F "usebackq tokens=*" %%i IN (`DIR /S /B D:\check\*.txt`) DO ( IF NOT "_%year%_%day%_%month%.txt"=="!FTRIM_%%J!" (
SET XCOUNT_!J!=%%~i
SET FNAME_!J!=%%~ni
SET MAXJ=!J!
SET /A J+=1
SET FTRIM_!nxi!=%%~nxi:~-10,10%
PAUSE
)
)
C:\Users\pwatson\bin>type atest.bat
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A J=1
SET /A MAXJ=0
FOR /F "usebackq tokens=*" %%i IN (`DIR /S /B C:\Python27-32\*.exe`) DO (
SET XCOUNT_!J!=%%~i
SET MAXJ=!J!
SET /A J+=1
)
SET XCOUNT
This produces:
C:\Users\pwatson\bin>call atest.bat
XCOUNT_1=C:\Python27-32\python.exe
XCOUNT_10=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\t64.exe
XCOUNT_11=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\w32.exe
XCOUNT_12=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\w64.exe
XCOUNT_13=C:\Python27-32\Lib\site-packages\setuptools\cli-32.exe
XCOUNT_14=C:\Python27-32\Lib\site-packages\setuptools\cli-64.exe
XCOUNT_15=C:\Python27-32\Lib\site-packages\setuptools\cli-arm-32.exe
XCOUNT_16=C:\Python27-32\Lib\site-packages\setuptools\cli.exe
XCOUNT_17=C:\Python27-32\Lib\site-packages\setuptools\gui-32.exe
XCOUNT_18=C:\Python27-32\Lib\site-packages\setuptools\gui-64.exe
XCOUNT_19=C:\Python27-32\Lib\site-packages\setuptools\gui-arm-32.exe
XCOUNT_2=C:\Python27-32\pythonw.exe
XCOUNT_20=C:\Python27-32\Lib\site-packages\setuptools\gui.exe
XCOUNT_21=C:\Python27-32\Scripts\easy_install-2.7.exe
XCOUNT_22=C:\Python27-32\Scripts\easy_install.exe
XCOUNT_23=C:\Python27-32\Scripts\pip.exe
XCOUNT_24=C:\Python27-32\Scripts\pip2.7.exe
XCOUNT_25=C:\Python27-32\Scripts\pip2.exe
XCOUNT_3=C:\Python27-32\w9xpopen.exe
XCOUNT_4=C:\Python27-32\Lib\distutils\command\wininst-6.0.exe
XCOUNT_5=C:\Python27-32\Lib\distutils\command\wininst-7.1.exe
XCOUNT_6=C:\Python27-32\Lib\distutils\command\wininst-8.0.exe
XCOUNT_7=C:\Python27-32\Lib\distutils\command\wininst-9.0-amd64.exe
XCOUNT_8=C:\Python27-32\Lib\distutils\command\wininst-9.0.exe
XCOUNT_9=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\t32.exe
Getting these back into the parent environment is another issue. I hope that dbenham might respond.
First of all, you can't have all of that code on the same line like that unless you tell batch where one command stops and the next command begins. Secondly, the code you have will only work on the command prompt and not in a batch file, because you need two %s when using for loop variables inside of batch files. Third, set /a is only used for math; use a regular set command when storing a string value.
Finally (and most importantly), you need delayed expansion to properly iterate your counter variable inside of your for loop.
This batch script should work:
#echo off
setlocal enabledelayedexpansion
set J=1
for /R "D:\TEST" %%I in (*.txt) do (
set XCOUNT_!J!=%%I
set /a J+=1
)
If you want to exclude a specific file from the output, wrap the set code in an if statement.
#echo off
setlocal enabledelayedexpansion
set J=1
for /R "D:\TEST" %%I in (*.txt) do (
if not "%%~nxI"=="test.txt" (
set XCOUNT_!J!=%%I
set /a J+=1
)
)

Replace Text In File Using Windows Prompt

I know there have been previous posts about this but none of them have worked for me. I want to do a find and replace string in a text file using the windows command prompt. No parameters, all hard coded. Here is what I have so far:
..........
setlocal enabledelayedexpansion
set SEARCHTEXT=oldtext
set REPLACETEXT=newtext
for /f "tokens=1 delims=" %A in ( C:\in.txt) do (
set string=%A
echo set string:%SEARCHTEXT%=%REPLACETEXT% >> C:\out.txt)
..............
This code just writes "set string:oldtext=newtext" to out.txt for each line in in.txt.
How can I get it to actually replace oldtext with newtext?
Thanks.
Test this:
#echo off
setlocal enabledelayedexpansion
set "SEARCHTEXT=oldtext"
set "REPLACETEXT=newtext"
for /f "usebackq delims=" %%A in ("C:\in.txt") do (
set "string=%%A"
set "string=!string:%SEARCHTEXT%=%REPLACETEXT%!"
>>"C:\out.txt" echo !string!
)
remove the echo.
It is there for testing the code without really destroying anything.
If the output is what you need, just remove it.
EDIT: ah wait - there is a logical failure in the code. It should obviously look like:
...
for /f "tokens=1 delims=" %%A in ( C:\in.txt) do (
set string=%%A
set string=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !string!>> C:\out.txt
)

Batch for loop cannot set variables

The task is to iterate through each line in a file named alts.txt. Then I grab the line and split it at the semicolon and print out the text before the semicolon and after the semicolon.
My file looks something like this...
username:password
username2:password2
username3:
My current code is this:
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=*" %%A in (%file%) do (
set str=%%A
set "username=%str::="^&REM #%
set "pass=%str:*:=%"
echo username=%username% pass=%pass%
)
pause
If someone would be kind enough to show me my error and EXACTLY how to fix the error it would be greatly appreciated.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=1,2 delims=:" %%A in (%file%) do (
set "$user=%%A"
set "$pass=%%B"
echo username=!$user! pass=!$pass!
)
pause
Be careful using %username%. It's a system variable. You can test writing echo %username% in the CMD prompt. You better choose another name for the Variable like i did.

Resources