2
if not defined filename set filename=123.txt
if not defined folder set folder=January
set BASE_DIR=C:\Users\xxx\Desktop\

set file=%BASE_DIR%%folder%\%filename%

I am having difficult time coming with algorithma to accomplish what I am trying to do.

Every time this batch script runs filename and foldercan be different.

  • I am trying to check if file exist if it does no need to do anything goto end.
  • If file does NOT exist and there is a another file under that folder. I need to update that filename with given.

  • However, there might be case where I wouldnt even have folder exist. In that case I need to make folder and file inside of that folder.

Content inside file is always empty.

I dont need code, I just need help with logic to accomplish this.

mtkilic
  • 1,213
  • 1
  • 12
  • 28
  • 2
    You are using the `DEFINED` option. Which works with variables. If you want to know if a file or folder exists then use the `EXIST` option. – Squashman May 03 '18 at 16:58
  • @Squashman filename is env. above code didnt not intent to check if file exist – mtkilic May 03 '18 at 17:50
  • If your intent is to check if a file or folder exists, why didn't you at least attempt to code that and post that code. You said you don't need code but the only way to really show you the logic is too code it for you. Why don't you just code this in python? – Squashman May 03 '18 at 18:28
  • I think I figured out. I just have to have multiple if statement to check if exist or file exist. I am actually making this updates on existing batch file – mtkilic May 03 '18 at 21:32

1 Answers1

3

This commented batch file should be helpful for you to finish your batch file coding task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not defined FileName set "FileName=123.txt"
if not defined Folder set "Folder=January"

set "BASE_DIR=%UserProfile%\Desktop"
set "FilePath=%BASE_DIR%\%Folder%"
set "FullFileName=%FilePath%\%FileName%"

rem Exit batch file execution if file already exists.
if exist "%FullFileName%" goto :EOF

rem Create the entire directory structure for the file if directory
rem does not already exist. Note the backslash at end which prevents
rem condition evaluating to true if a file with name "%Folder%"
rem exists in "%BASE_DIR%".
if not exist "%FilePath%\" md "%FilePath%" 2>nul

rem The creation of the directory tree could fail for various reasons.
if not exist "%FilePath%\" (
    echo Error by %~f0:
    echo/
    echo Directory "%FilePath%" could not be created.
    echo/
    pause
    goto :EOF
)

rem Add here more code to create/copy/move file "%FileName%" in/to "%FilePath%".
rem For example creating an empty file:
type NUL >"%FullFileName%"

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /? ... not explicitly used here. cmd.exe automatically restores previous environment on terminating batch file execution. See this answer for details about the commands SETLOCAL and ENDLOCAL.
  • goto /?
  • if /?
  • md /?
  • rem /?
  • set /?
  • setlocal /?
  • type /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143