Delete a specific file if this one is empty - file

I would like to delete a specific file if this one is empty by the use of a windows .bat file. Here is my non-working script:
if %~z1 == 0
echo %~z1===================
del "%1"
goto :eof

if %~z1==0 (
echo %~z1===================
del "%1"
)
goto :eof
The goto should be placed inside the parentheses if you want to go to end-of-file having deleted the file. As I've placed it, in the absence of further information, the batch would terminate regardless.

Related

What :PROMPT means in dos batch file? [duplicate]

I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.
I have simplified my script so as to grasp some idea of what may be happening.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
:MISSING
echo file missing
ping localhost -n 5 >NUL
Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"
Why is it running running both labels?
Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:
#ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :NEXTBIT
:MISSING
echo file missing
:NEXTBIT
ping localhost -n 5 >NUL
It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:
#ECHO OFF
IF EXIST c:\test\test.txt (
ECHO File exists
) ELSE (
ECHO File missing
)
ping localhost -n 5 >nul
...so you can eliminate your GOTOs entirely.
It's because you need to skip over the "missing" bit if it exists:
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :COMMON
:MISSING
echo file missing
:COMMON
ping localhost -n 5 >NUL
You may also want to keep in mind that the current cmd.exe batch language is a fair bit more powerful than that which came with MS-DOS. I would prefer this one:
#echo off
if exist c:\test\test.txt (
echo file exists
) else (
echo file missing
)
ping localhost -n 5 >nul
After you echo file exists the next command is
echo file missing
You need to do something to skip the missing case. Perhaps another goto to a :PING label?
When you're debugging it helps to keep the echo on.
Because GOTO statement moves the execution to that label. To use it in the situation like yours, you need to add another GOTO label.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO MISSING
:EXISTING
echo file exists
GOTO END
:MISSING
echo file missing
GOTO END
:END
ping localhost -n 5 >NUL
#echo off
IF EXIST "c:\test\test.txt" ( :: warning double quotes
GOTO EXISTING
) ELSE ( :: this format best in batch
GOTO MISSING
) :: don't forget
:EXISTING
echo file exists
goto OTHER :: if file exist jump OTHER
:MISSING
echo file missing
:: label is not required
:OTHER
timeout /t 5 >nul
pause

copy function in batch script does not work

I am writing a batch script where I am writing a set of instruction inside function as I want to call it many time so want to reuse it.For me it works when i write outside function but inside function it never works . Below the code which I have used .
#echo off
set _prefs="%APPDATA%\test\test\BrowserProfile\prefs.js"
set _prefs_notes="%ProgramFiles(x86)%\test\tset1\Data\workspace\BrowserProfile\prefs.js"
#rem it works
copy /y %_prefs_notes% %_prefs_notes%.copy1 > nul
CALL :AMEND_PREFJS %_prefs_notes%
EXIT /B
:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y %~1 %~1.copy > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
set %~1=
EXIT /B 0
goto end
:prefs_not_found
rem set error level?
echo "file does not exist -- %_prefs_notes%"
:end
set _prefs=
I am going to tell you what most of us do as best practices for writing batch files.
Never assign quotes to variables. You can use quotes though to protect the assignment of the variable. This helps with protecting special characters within the assignment and also keeps you from assigning trailing spaces.
Get into the habit of always using quotes to surround your file paths when using them with another command.
This is how I would write your batch file.
#echo off
set "_prefs=%APPDATA%\test\test\BrowserProfile\prefs.js"
set "_prefs_notes=%ProgramFiles(x86)%\test\tset1\Data\workspace\BrowserProfile\prefs.js"
#rem it works
copy /y "%_prefs_notes%" "%_prefs_notes%.copy1" > nul
CALL :AMEND_PREFJS "%_prefs_notes%"
EXIT /B
:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y "%~1" "%~1.copy" > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
EXIT /B 0

Batch file set command isn't working in if statement

For the life of me, I can't figure out why the below set prompt won't work when it is in the if statement:
#echo off
REM :askdeletecsvs
if exist *.csv (
echo Warning! All files in the scripts folder that have the "CSV" extension will be deleted!
echo Answering "n" will continue the script without deleting the CSVs.
set /p ASKDELETE=Delete CSVs? (y/n):
REM
REM if ( /i %ASKDELETE% equ "y" goto :deletecsvs )
REM if ( /i %ASKDELETE% equ "n" goto :runscripts )
REM goto :askdeletecsvs
)
When I run the batch file as it is above the cmd window opens and then shuts quickly. If I move the set line outside of the if statement then the prompt shows as expected. (There are csvs in the folder the bat file is running from)
What am I missing?
To start with you had used a closing parenthesis which was prematurely ending your opening If parenthesis.
I'd suggest reversing the thinking:
If Not Exist *.csv GoTo runscripts
Echo Warning!
Echo All files in the scripts folder that have the "CSV" extension will be deleted!
Echo Answering "N" will continue the script without deleting the CSVs.
Choice /M "Delete CSVs"
If ErrorLevel 2 GoTo runscripts
:deletecsvs
Del /F /Q /A "PathTo\scripts\*.csv"
GoTo :EOF
:runscripts
You can change GoTo :EOF to a relevant valid label as necessary or remove it if you want to continue on to :runscripts. You can also replace PathTo\scripts\ with %~dp0 if the batch file is running from the scripts directory, or remove PathTo\scripts\ if the current directory holds those files. (note that the current directory and batch file path may not necessarily be the same)

Make a batch file say something only when opened for the first time

If it's possible, how do you get a batch file to only say something once? As in, only when it is opened for the first time?
Include this code wherever you want in your Batch file:
call :FirstTime 2>NUL
if errorlevel 1 (
echo :FirstTime >> "%~F0"
echo exit /B 0 >> "%~F0"
echo This is the first time this file run!
)
Just BE SURE to end your Batch file with: goto :EOF
Here's a method - you can also add a path for the "firstrun.txt" file.
#echo off
if not exist "firstrun.txt" (
echo This is the first time you have run this batch file...
type nul >"firstrun.txt"
)
:: batch code goes here

Folder creation using bat command

I have a bat file to create folder.
:X3Main
IF EXIST "%1%\JBoss" goto Test
IF EXIST "%1%\DB" goto DB
goto end
:Test
mkdir "%DESTINATION%\IX3\COSMIC\JBoss"
goto end
:DB
mkdir "%DESTINATION%\IX3\COSMIC\DB"
goto end
here first folder is created successfully(IF EXIST "%1%\JBoss" goto Test) but second function is not working. If I remove first function then second function is working.
Please can you someone explain reason behind this?
Some extra info: In the NT line of Windows for a reliable test for a folder you need to end the foldername with a backslash
and you may have meant %~1 instead of %1%
IF EXIST "%~1\JBoss\" do task
and in your case you can always use this which will do nothing if the folder already exists: the 2>nul eliminates a harmless error message when the folder exists.
mkdir "%DESTINATION%\IX3\COSMIC\JBoss" 2>nul
mkdir "%DESTINATION%\IX3\COSMIC\DB" 2>nul
try this:
:X3Main
IF EXIST "%1%\JBoss\" call:Test
IF EXIST "%1%\DB\" call:DB
goto :eof &rem or goto:end or where you want
:Test
mkdir "%DESTINATION%\IX3\COSMIC\JBoss"
goto :eof
:DB
mkdir "%DESTINATION%\IX3\COSMIC\DB"
goto :eof
use call to return from function and goto:eof to end a function.
Edited concerning foxidrive's answer.

Resources