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
Related
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.
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
I'm trying to make a batch script with find and replace option but the batch script doesn't do anything instead just show the blinking cursor and gets stuck there.
Here's the coding I'm using:
setlocal
set this=abhinav2
for /f "delims=" %%i in ('^<test\abc.new findstr /n "^"') do ('
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!"
if not "!line!"=="!line:<xyz>mnishamk<abc>" set "line=<xyz>%this%<abc>"
(echo(!line!)>>test\new.abc
endlocal
)
)
I can't simply replace mnishamk with abhinav2 as there are lots of names with mnishamk present in the file.
You have a syntax issue, see set /? for help and try this:
#echo off&setlocal enabledelayedexpansion
set "line=<xyz>mnishamk<abc>"
set "this=abhinav2"
if not "!line!"=="!line:<xyz>mnishamk<abc>=!" set "line=<xyz>%this%<abc>"
echo !line!
output is:
<xyz>abhinav2<abc>
And this also works:
set "line=!line:<xyz>mnishamk<abc>=<xyz>%this%<abc>!"
#ECHO off
setlocal
DEL \test\new.abc /F /Q
set this=abhinav2
for /f "delims=" %%i in ('^<\test\abc.new findstr /n "^"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!"
if not "!line!"=="!line:<xyz>mnishamk<abc>=!" set "line=!line:<xyz>mnishamk<abc>=<xyz>%this%<abc>!"
echo(!line!>>\test\new.abc
endlocal
)
FC \test\abc.new \test\new.abc
This worked for me...
I just want to echo a variable which is defined with ENABLEDELAYEDEXPANSION. It doesn't work.
Here's a small part of my long script on the issue
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%x in (%1) do (
set "D=%%x"
echo %%~nD
)
I have also written echo !~nD! but it doesn't work either.
my file (%1) only contains relative paths as so:
VENDOR\ford1.car
VENDOR\bmw.car
and my goal is to echoing 'ford1.car' or 'bmw.car' because I have to use them in the next steps of my script, that is only the file complete name.
Please some help and explanations. Thanks
Try:
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
echo %%~nxa
)
For only does substitution on the variable used in the for. You can't set a variable within and use substitution on it.
%%~na would only give you the file name without the extension. You have to use %%~nxa to get the file name and the extension.
If you want to set the file name to a variable and do something with each file, you have to use DelayedExpansion like this:
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
set d=%%~nxa
echo !d!
Do something with !d!
)
Or you could create a subroutine and not have to use a variable at all
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
call :sub %%~nxa
)
goto :eof
:sub
%1 = your file name so do some processing on it.
i am trying to get files from a directory and want to set names of the file to a variable using batch script.
this is my code .but it always setting same value to variable
can any body give solution
echo on
setlocal EnableDelayedExpansion
for /f %%x in ('dir /b C:\backup_dir') do (
SET test=%%~nx
if "%test:~0,6%"=="kdc_db" (set DUMP=%%x)
if "%test:~0,6%"=="kdc_ke" (set KEYS=%%x)
)
echo %DUMP%
echo %KEYS%
here dump and keys variables are always set to same value
You need to use delayed expansion. You have already enabled it, you just need to replace your %'s with !'s
echo on
setlocal EnableDelayedExpansion
for /f %%x in ('dir /b C:\backup_dir') do (
SET test=%%~nx
if "!test:~0,6!"=="kdc_db" (set DUMP=%%x)
if "!test:~0,6!"=="kdc_ke" (set KEYS=%%x)
)
echo %DUMP%
echo %KEYS%