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
Related
I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"
As the title explains.
With batch script. Copy a file from one directory to another, then prompt me what to rename it to.
If a similar file already exist in the target directory, then just replace/overwrite it.
Can I write something like this in batch script? I know how to copy from one directory to another. But the second part I don't know how to do it.
I think this is what you're looking for:
#echo off
set "oldFile=C:\someDirectory\test.txt"
set "newDirectory=C:\someOtherDirectory\"
set /p "newFile=Please enter a filename for the new file: "
copy /y "%oldFile%" "%newDirectory%%newFile%"
I'm writing a batch file which is supposed (among other things) to call another batch file.
The second batch file is depending on files located in the same directory, so, when I try running it from the first batch file, it fails.
Example:
Batch file #1: (Located on C:)
#echo OFF
call C:\Tests\Tests.bat
Output: "Could Not Find C:\Tests\Tests.txt
Reason: Both Tests.bat and Tests.txt are located under: C:\Tests while I'm calling it from C:.
Any ideas?
Thanks a lot,
Idan.
You can use %-dp0 within a batch file to get the path of the currently executing batch file. You can use that path every time you need to access a file from that directory. For example:
call "%~dp0\Tests.bat"
for /f "usebackq ..." ... in ("%~dp0\someFile") do ...
Another option is to temporarily set your current directory to the same path at the top of a batch script using PUSHD. From that point on you don't have to worry about providing the path to the other commands. Just be sure to use POPD before the script exits. That way the script will not impact any script that may have called it.
pushd "%~dp0"
rem Do you work
popd
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
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.