and exectute some command if file empty
Here's a sample batch file that does this. You call it with the name of the file you want to check is empty or not:
#ECHO OFF
IF "%~z1"=="0" GOTO FileEmpty
ECHO File is not empty
GOTO End
:FileEmpty
ECHO File is empty
:End
Note that the file must exist in order for this to work. But you can check that easily with IF EXIST
Related
I need someone to help me make a bacth file that can test if a file can be renamed and then open a info box telling if the file can be renamed or not..
The problem in all of this is that I use interactive pdf files, and if two people on my network open the file at the same time, none of them can save the changes that they make in the file. But I know that if someone has the file open it can't be renamed, so that is why I need a batch file that can test if a file can be renamed without renaming it. If it can be renamed I need it to open a popup box saying "The file is ready for editing" and if it can't be renamed I need the popup box to say "The file is in use, please try again later"
I'll be very happy if ther is someone who is will to take on this challange :)
Best Regards
Dion
#echo off
ren "file.pdf" "file.pdf" 2>nul || (
echo Sorry, the file is in use.
echo Please try again later.
else start "C:\Test" file.pdf
)
You have almost answered your own question, without realizing it :-)
Simply attempt to rename a file to the same name. If it succeeds, then the file is not locked and there is no harm. If it fails, then you know the file is locked.
For example:
#echo off
ren "yourFile.pdf" "yourFile.pdf" 2>nul || (
echo Sorry, file is locked by another user
rem Take some error action, perhaps GOTO or EXIT /B
)
rem Now open the pdf file
But beware - this strategy is not foolproof because you have a race condition. If two processes run against the same file at nearly the same time, then they both may "rename" the file successfully before either has a chance to open the file.
try this code:
ren "pdffile.pdf" "renameto.pdf"
if %errorlevel% NEQ 0 (
echo The file cannot be renamed.
rem The following line makes a dialog box saying that an error happened:
echo msgbox "The file could not be renamed.">"%TEMP%\msg.vbs" && cscript /nologo "%TEMP%\msg.vbs" && del "%TEMP%\msg.vbs"
rem put other commands here
)
What would the contents of a bat file be if I want the following
If a file exists (file name and location)
Then delete the file and then restart the computer
It's pretty straightforward -
#echo off
set FileToCheck=NAME_OF_THE_FILE_HERE
if not exist %FileToCheck% goto done
del %FileToCheck%
shutdown /r
:done
You could also use set FileToCheck=%1 instead of hard-coding the filename in your batch file if you want to pass the filename to look for as a command line parameter, i.e. (assuming your batch file is named foo.bat):
c:\>foo RemoveMeAndRebootIfIExist.txt
I have one file at c:\1\1.txt
In my batch program before performing any operation I have to check whether the file exists or not
My command is
If exist c:\1\1.txt
Echo 1
Its not working. In fact, because of if command the batch is crashing.
read HELP IF and then try
IF EXIST c:\1\1.txt ECHO 1
note that you have to put the IF command, the condition, and the conditional command instruction in the same line
alternatively you can use parentheses
IF EXIST c:\1\1.txt (
ECHO 1
)
It works for me. I have used this code, written in batch.bat:
#ECHO OFF
If exist c:\1\1.txt ECHO 1
Please, specify "bash is crashing".
test in reverse CONDITION using NOT
If NOT exist c:\1\1.txt EXIT
Echo EXIST
first check by command console the name of file doing
cd c:\1
dir *.txt
why?
if you create a new file txt with notepad and save it writing in name field 1.txt, the final file name is 1.txt.txt , you can verify this with command console doing
cd c:\1
dir *.txt
then if exist sentence work fine because 1.txt don't exist, the true name of file is 1.txt.txt.
is a posibility for this problem.
Is there any way to make a batch file that does not overwrite an existing file when there is a name conflict, but instead keeps both copies of the file in the same path?
The Batch file below works like COPY command with just one file. If the file already exist in target folder, a number in parentheses is added to new file in order to keep both files.
#echo off
Rem mycopy sourceFile targetDir
Set targetName=%~1
Set i=0
:nextName
If not exist "%~2/%targetName%" goto copy
Set /A i+=1
Set targetName=%~1 (%i%)
Goto nextName
:copy
Copy %1 "%~2/%targetName%"
I have a batch file as under:
#echo off
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "C:\Documents and Settings\vipul\Desktop\vipul.zip" files vipul.xls
copy vipul.zip "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy vipul.zip "E:\Valuations\2009"
exit
HERE vipul.xls is the file on my desktop which is to be copied to my briefcase and same is to be ziiped and then sent to E\valu...folder.
Alteration i want here is as under:
every time the file name is getting changed, e.g. it may be sanj.xls or lago.xls and so on. (in place of vipul.xls), so how i can do this?
Just like there is printdir.bat file in xp
You could have this batch file which works for any file with .xlsextension:
#echo off
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "C:\Documents and Settings\vipul\Desktop\worksheets.zip" files *.xls
copy worksheets.zip "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy worksheets.zip "E:\Valuations\2009"
exit
Or, if just one that files can exist in some time:
#echo off
set filename=
if exists vipul.xls set filename=vipul
if exists sanj.xls set filename=sanj
if exists lago.xls set filename=lago
if "%filename%" == "" goto end
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "C:\Documents and Settings\vipul\Desktop\%filename%.zip" files %filename%.xls
copy %filename%.zip "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy %filename%.zip "E:\Valuations\2009"
exit
:end
Its not tested, but this should help you:
#echo off
REM check if supplied file name actaully exists
IF "%1"=="" GOTO END
IF NOT EXIST "%1" GOTO END
REM define output file name. Use supplied files name without extension and add ".zip"
SET OutputPath=C:\Documents and Settings\vipul\Desktop\%~n1.zip
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "%OutputPath%" files "%1"
copy "%OutputPath%" "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy "%OutputPath%" "E:\Valuations\2009"
:END
This batch uses the first command line parameter (%1) as input for the file to package and copy.
The first IF statements check if the file is valid. The SET command set a variable with the name of the file to create (the zip file). The remaining part is mainly the code you already have, but now uses the variables.
EDIT:
To call the batch programm, lets name it package.bat, use a syntax like this:
package "C:\Documents and Settings\vipul\Desktop\vipul.xls"
or
package "C:\Documents and Settings\vipul\Desktop\sanj.xls"
You can also use drag 'n drop and simply drop the file you want to process on the batch file package.bat to start it.
If it doesn't work, add a comment.
EDIT:
To use this batch file in your send to context menu do the following steps:
save the above code in a file and name package.bat (or anything else you want)
put in a location you want.
create a link to the batch file package.bat (right click on the file, chose create link)
move the created link file to your Send to folder (e.g. C:\Documents and Settings\vipul\SendTo
now you can select any file you want and chose the batch file command from your context menu->send to
Hope that helps.