The Windows.Bat file behaves differently when executed from the command window and by double-clicking on the bat file

The Windows.Bat file behaves differently when executed from the command window and by double-clicking on the bat file. This is my file:

ECHO ON
del activity_ftp.log
cd D:\My_Test
IF EXIST united_ops*.csv (
for %%i in (united_ops*.csv) do (
set size=0
set /A size=%%~zi
echo %%i,%size%,397312,624640 > D:\My_Test\activity_ftp.log
)
)

When I launched this by opening a command window and calling it,

0
source share
2 answers

There are some problems in the code.
cd d:\My_testwill only work if you are on D:, you can use cd /deither pushdhere.

echo ...%size% does not work, because it expands when the for block is parsed not when it is executed.

if existseems redundant as it for %%i in ( united_ops*.csv)only expands if any file exists.

ECHO ON
setlocal EnableDelayedExpansion
del activity_ftp.log
pushd D:\My_Test
for %%i in (united_ops*.csv) do (
    set size=0
    set /A size=%%~zi         
    echo %%i,!size!,397312,624640 > D:\My_Test\activity_ftp.log
)
+3

.

1) FOR , . . . . >>, . >.

2) 0, , -. , , .

3) , , , . , , .

4) FOR.

ECHO ON
>"D:\My_Test\activity_ftp.log" (
  for %%i in ("d:\My_Test\united_ops*.csv") do (
    echo %%~nxi,%%~zi,397312,624640
  )
)
0

All Articles