84497

Windows .Bat file behave differently when executed from command window and by double clicking on the

Question:

Windows .Bat file behave differently when executed from 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 run this by opening command window and calling it,

Answer1:

There are some issues in your code.<br />cd d:\My_test will only work if you are on D:, you could use cd /d or pushd here.

echo ...%size% doesn't work, as it's expands when the for block is parsed not when it's executed.

The if exist seems to be redundant, as the 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 )

Answer2:

Building on jeb's answer.

1) Your FOR loop may iterate through many files that match your pattern. But you use the overwrite mode of file redirection. Each found file will over-write the output for the prior file. Your final output file will never have more than one line. You could change to the append mode using >>, but there is a better way. It is faster to enclose the entire loop in parentheses and redirect once in overwrite mode using >.

2) You are setting size to 0, then setting it to the file size, and then you don't use it after the line is echoed. I suspect you don't need the variable at all, so you don't need delayed expansion.

3) The file you delete at the top does not include the path information, so it may not be deleting from the correct folder. Even if it were, it is unnecessary since you are redirecting in overwrite mode anyway.

4) Instead of changing the current directory you could include the path in the FOR statement.

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

Recommend

  • Unable to Run Query in MySQL syntax error unexpected
  • Renaming folders with different names or removing them?
  • bazel “undeclared inclusion(s)” errors after updating gcc
  • How to use bash to test directory limits of filesystem
  • How do you run `go test` when test files are within a module?
  • Parse file name using batch automation
  • Modifying files nested in tar archive
  • Dispose not working as expected in DirectorySearcher
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • jQueryUI dialog replacement for confirm?
  • Calling a constructor through reflection in scala 2.10
  • Ruby: Why does this way of using map throw an error?
  • reset jquery smartwizard
  • Simple Factory with reflection C#
  • Calling java project from Mathematica
  • The symbol you provided is not a function
  • end daemon processes with multiprocessing module
  • Result showing lining on image
  • init_seg and warning C4073 from library code?
  • iOS Localization Doesn't Work with More Than 63 Files
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • what does prefix @- mean in makefile?
  • SonarQube: Cannot deactivate rule with missing quality profile
  • npm 5.4.1 install/uninstall all failing
  • How to generate and display a QR Code in ionic 2
  • Atlas images wrong size on iPad iOS 9
  • QLineEdit password safety
  • Upload files with Ajax and Jquery
  • Why winpcap requires both .lib and .dll to run?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?