@echo off
echo processing please wait...
setlocal enabledelayedexpansion
set txtfile=%~dp0mysql\my.ini.bak
set newfile=%~dp0mysql\my.ini
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
echo !newline! >> %newfile%
)
Now my.ini.bak file is in D:\Program Files\my.ini.bak
Error : The system cannot find the file Files\mysql\my.ini.bak.
How to make this code work, so it copy each line from my.ini.bak to my.ini
I don't think you need to quote the paths, you can just use the short names in the expanded path. So use %~dps instead of %~dp
@echo off
echo processing please wait...
setlocal enabledelayedexpansion
set txtfile=%~dps0mysql\my.ini.bak
set newfile=%~dps0mysql\my.ini
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
echo !newline! >> %newfile%
)
By the way, the error I get with your original code is slightly different from your error, not sure why.
The system cannot find the file C:\Program.
The space in the path is indeed preventing FOR /F from opening the file successfully. You need quotes around the file name, but then you also need the FOR /F "USEBACKQ" option so that the quoted name is treated as a file name instead of a text string.
Using "TOKENS=*"
is almost, but not quite the same as "DELIMS="
I generally prefer "DELIMS="
unless I have a reason to strip leading spaces.
If there is a chance that the .INI file can contain !
character then you will want to toggle delayed expansion on and off within the loop. The value of %%a
will be corrupted if it contains !
and delayed expansion is enabled.
It is more efficient to enclose the entire FOR loop within another set of parens and redirect the output just once instead of once for each iteration. It also eliminates the need to first delete the file if it already exists.
@echo off
echo processing please wait...
setlocal disableDelayedExpansion
set "txtfile=%~dp0mysql\my.ini.bak"
set "newfile=%~dp0mysql\my.ini"
>"%newfile%" (
for /f "usebackq delims=" %%a in ("%txtfile%") do (
set newline=%%a
setlocal enableDelayedExpansion
rem Presumably more processing goes here
echo !newline!
endlocal
)
)
The problem is the space in the file paths. I can't test this right now, but believe quoting the paths will fix it:
set txtfile="%~dp0mysql\my.ini.bak"
set newfile="%~dp0mysql\my.ini"
Here is the full code I tested with. For testing I added a space in "my sql" and created a folder by this name.
@echo off
echo processing please wait...
setlocal enabledelayedexpansion
set txtfile="%~dp0my sql\my.ini.bak"
echo %txtfile%
set newfile="%~dp0my sql\my.ini"
echo %newfile%
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
echo !newline! >> %newfile%
)