0

A bit embarrased that my first question is about a simple batch file, but my knowledge is quite limited in this topic.

I am writing a simple batch script to copy some data from a to b. For this reason i want to create destination folders according to the current month and check if the folder already exists.

I am not able to identify why my code exits at line 14 without throwing anything after pressing a key. I also tried verifying the code with a batch code verifying tool (BatCodeCheck "quite outdated"). However it throws no errors or warnings regarding my problem.

The code in question:

@echo off
echo Getting current month...
set month=%date:~3,2%
if %month:~0,1% == 0 (
    set /A month=%month:~1,2%-1
) ELSE (
    set /A month=%month%-1
)
if [%month:~1,2%] == [] (
    set month=0%month%
)
echo "%month% is the month before"
echo Checking for monthly folder...
pause
if exist %~dp0%month%\ (
    echo "Folder already exists. Press y to overwrite"
    pause
    set /p Input=Overwrite? (y/n):
    if /I "%Input%"NEQ"y" (
        EXIT 0
    )
) ELSE (
    echo "Folder doesn't exist already. Creating..."
    mkdir %~dp0%month%\
)

The check log:

Time          :  2022-05-12 19:10:08
Program       :  BatCodeCheck, Version 0.38
Arguments     :  D:\RunBackup.bat /L /W
File name     :  "D:\RunBackup.bat"
File date     :  2022-05-12 19:10:02
File encoding :  ASCII
Tests         :  ABELMSUV
                 A =  command line Arguments for batch commands
                 B =  Best practice tips
                 E =  Environment variables
                 L =  Labels
                 M =  common Mistakes
                 S =  verbose Summary (variables, labels, subroutines)
                 U =  Undefined environment variables
                 V =  Vulnerabilities

RISKY CODE:
Line    18: SET /P could make your code vulnerable to exploits (see http://www.robvanderwoude.com/battech_inputvalidation.php#SetP)

SUMMARY:
1   line generated a warning and should be examined


Note that some warnings are only displayed once. Correct the errors and run this test again.

Hopefully its not a syntax error i am missing...

  • 2
    `Cmd` is very picky about spaces: `if /I "%Input%"NEQ"y"` should read `if /I "%Input%" NEQ "y"`. For troubleshooting, run the code from an already open `cmd` window and it won't close on severe syntax errors, so you can read any error messages. – Stephan May 12 '22 at 18:02
  • 2
    some word of advice: use `exit /b` instead of `exit` (`exit` exits the command window, `exit /b` exits the script only (the window will close too, if you executed the script with a double-click, because there is nothing more to do) – Stephan May 12 '22 at 18:04
  • 2
    In a command prompt, run `echo *%date:~3,2%*` and make sure that it shows what you think it does. Personally, it returns `* 0*` for me, which means that when you check `%month:~0,1%` without quotes, line 4 reads `if == 0 (`, which is a syntax error. Strings start at 0. – SomethingDark May 12 '22 at 18:09

1 Answers1

0

When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.

I suspect if exist %~dp0%month%\ ( should be quoted if exist "%~dp0%month%\" ( - the path may contain spaces.

Same goes for mkdir - without the quotes, each space-separated string will be regarded as a directory-name to create.

Your comparison statement "%Input%"NEQ"y"requires spaces each side of neq.

Read Stephan's DELAYEDEXPANSION link for why this won't work as-is. There are many, many SO articles about how to use choice in this situation.

Stephan
  • 53,940
  • 10
  • 58
  • 91
Magoo
  • 77,302
  • 8
  • 62
  • 84