Run bat file after save in NetBeans - batch-file

I am using NetBeans 8.0.2 with PHP project. I have batch file called organizer.bat that configure to do some actions with the PHP source code.
Until now I run it manually after the code was changed.
I want that NetBeans will run my organizer.bat file automatically after I save a file in my project.
How can I configure such behavior?

One solution would be to have a batch file running in the background that periodically checks for new files in your project folder.
There's a lot of ways to do that. One way, for example, would be to output the directory files into a log file every 10 seconds.
If the new log file matches the old log file, then the contents of the directory are the same and the batch file does not need to be run.
The below commands will overwrite any existing log files.
:loop
cd C:\ProjectDirectory
xcopy logfile.log logfile2.log /f /y
dir > logfile.log
for %%I in (logfile.log) do set /a fs = %%~zI
for %%I in (logfile2.log) do set /a fs2 = %%~zI
if %fs%==%fs2% (echo "No changes in directory") else (echo "Changes in directory, run batch file.")
timeout /t 10
goto loop

Related

CMD/FTP to create folder using to today date & connect ftp download into created folder

I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B

Kodi - open .strm files by running a .bat / VBScript

I have legal .strm files of different TV Shows in a folder named TV Shows. Each strm file of each episode is stored in different subfolders.
I would like to run a certain VBScript before these strm files are played.
Then I have a different folder named MOVIES. Again, I would like to run a VBScript before these strm files are played. But this VBScript is a different one.
How would I do that?
Platform is Windows.
Thanks!
If you're looking to open or do something to each file based on extension, An easy way to do this is to use an for loop with /R to search all sub-directories for an *.strm file.
#ECHO OFF
SET "LOC=C:\Folder"
CD %LOC%
FOR /R %%A IN (*.strm) DO (
Rem | Do something with each file.
Start cmd.exe /C "notepad.exe "%%A""
)
Echo No More Items Found!
pause
goto :EOF
%%A will return the file path.
Start cmd.exe /C "" will start a CMD command. (Replace with your code)
notepad.exe "" will open the file (Used as an example)
EDIT:
So you're looking to check if the file exists then if true run the script? Bellow should solve that.
#ECHO OFF
If exist (C:/Path/file.strm) (
Rem | File exists, run script
Echo Do something
)
GOTO :EOF

Batch Script to check if Zip file extracted or not

I am new to batch scripting and trying to write a script which will search the zip files inside a folder and extract if exists using 7-Zip. These two are working fine but i am trying to implement a logic where i need to update the log file which will have the entries which zip file got extracted and which got failed. Can someone help me here as when i tried to append log after extracting its showing all the entries present inside the zip files which i dont want. Below is script.
#ECHO OFF
SET LookForFile="C:\test\Software00*.*"
:CheckForFile
IF EXIST %LookForFile% GOTO RunELK
#ECHO TIMEOUT /T 20 >nul
GOTO CheckForFile
:RunELK
ECHO RunELK for this file : %LookForFile%
"C:\Program Files\7-Zip\7z.exe" e "C:\test\Software00*.*" -oC:\test >> C:\test\LOG1.LOG

Add error handling to existing Windows batch script-> to copy the newest file from a directory

I found a good post here that queries for the latest sql server backup, and then copies it over to the remote server in preparation for a restore. It's a batch file that is executed using cmd via sql agent step inside the sql restore job. I'm looking for help in adding extra logic to the existing script below:
:Variables
SET DatabaseBackupPath=\\virtualserver1\Database Backups
echo.
echo Restore WebServer Database
FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B /O:D') DO SET NewestFile=%%I
copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"
What I'd like to add are two extra pieces. First add some error handling to the existing script where it would first check the latest backup, but ensure its within the last 24 hours. If it is continue to run. If older than 24 hours, to generate an notification alert (i.e. separate batch file) if backup file is older than 24 hours. Second to generate similar notification, if there was an issue such as not being able to reach the remote share that holds the backup.
Appreciate replies.
It is not easy to check if a file was created or last modified within 24 hours. Date/time calculation is not a trivial task without conversion of the date/time strings to seconds since epoch which would make it possible to use a simple integer comparison.
There are solutions for file date comparison, see
Batch script to check when the newest file in a directory was created/modified
forfiles - Delete all files older than 1 day / 24hours using creation date?
You may use one of those solutions.
Much easier is to check if copy process worked:
copy /V "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"
if errorlevel 1 goto CopyFail
goto :EOF
:CopyFail
echo.
echo Copying file "%DatabaseBackupPath%\WebServer\%NewestFile%" failed!
pause
Solution using WinRAR
My solution below is based on usage of Rar.exe, the console version of WinRAR as this compression tool has the feature to compress only files based on file dates. Therefore this solution is only helpful for you if you have installed WinRAR and bought a license of it.
The path of the program files folder of WinRAR must be set at top of the following batch file.
#echo off
SET DatabaseBackupPath=\\virtualserver1\Database Backups
SET WinRarFolder=C:\Program Files\WinRAR
cls
echo.
echo Restore WebServer Database
echo.
rem Find newest *.bak file in backup directory on server.
FOR /F "delims=" %%I IN ('DIR /B /O-D "%DatabaseBackupPath%\WebServer\*.bak"') DO (
SET NewestFile=%%I
goto BackupFound
)
cls
echo.
echo Restore WebServer Database
echo.
echo There is no *.bak file in "%DatabaseBackupPath%\WebServer\"
echo or the backup directory on server cannot be reached at all.
goto EndBatch
:BackupFound
echo Newest backup in "%DatabaseBackupPath%\WebServer"
echo is the file "%NewestFile%".
echo.
if exist "%TEMP%\ServerBackup.rar" del "%TEMP%\ServerBackup.rar"
"%WinRarFolder%\Rar.exe" a -cfg- -ep -inul -m0 -tn24h -y "%TEMP%\ServerBackup.rar" "%DatabaseBackupPath%\WebServer\%NewestFile%"
if errorlevel 1 goto OldBackup
"%WinRarFolder%\Rar.exe" e -cfg- -inul -y "%TEMP%\ServerBackup.rar" F:\Temp
if errorlevel 1 goto ExtractFailed
del "%TEMP%\ServerBackup.rar"
echo File "%NewestFile%" copied to D:\
goto EndBatch
:ExtractFailed
del "%TEMP%\ServerBackup.rar"
echo "Copying file "%NewestFile%" to D:\ failed.
goto EndBatch
:OldBackup
echo File "%NewestFile%" is older than 24 hours.
:EndBatch
set WinRarFolder=
SET DatabaseBackupPath=
SET NewestFile=
echo.
pause
Explanation:
The batch file first uses command DIR to get a list of *.bak files in the specified directory sorted according to last modification file date from newest to oldest.
With the FOR loop the name of the file name at top of the list (= newest file) is assigned to environment variable NewestFile.
The batch file outputs an error message if no *.bak file can be found in the specified directory, for example if the server cannot be reached at all at the moment with the permissions of the used user account.
But on success on finding a *.bak file in specified directory, the batch file informs the batch file user which file was found in which directory.
After making sure that the RAR archive to create next does not already exist in directory for temporary files, Rar.exe is used to create a RAR archive in directory for temporary files into which only the found file is stored without compression, but only if this file has a last modification date within 24 hours because of switch -tn24h.
For details on the switches used on Rar.exe see text file Rar.txt in program files directory of WinRAR.
Rar.exe exits with an return value greater 0 if there is any problem on creating the RAR archive. In this case the reason is most likely that the specified file is older than 24 hours which results now in an appropriate message written into the console window.
Rar.exe is on success on creating the RAR archive without compression used once more to extract the file to target directory.
An appropriate error mesage is shown in unlikely case that this fails. Otherwise the batch file outputs a success message and finishes with cleaning up environment table (not really needed if command prompt window closed next).
The temporarily used RAR archive is deleted in any case.

Windows task scheduler can run batch file but not fire command inside for winzip

I have task schedule for backup directory list of wwwroot. For that I have written batch file.
for /F "tokens=1-3 delims=: " %%i in ('time /t') do set Hma=%%i%%j%%k
set yyyymmdd=%date:~10,4%%date:~4,2%%date:~7,2%_%Hma%
set FolderPath=D:\SystemBackup\DirListFiles\
dir c:\inetpub\wwwroot /s /o-d > %FolderPath%\DirList_%yyyymmdd%.txt
batch file will do correct at this point but after this
echo "Upload To FTP Start"
cd /d c:\Program Files (x86)\WinZip\
winzip32.exe /autorunjobfile d:\BackupScript\DirList.wjf
echo "Upload FTP Complete !"
cd /d %FolderPath%
del DirList_%yyyymmdd%.txt
Not working well. It does not winzip well and also not send to ftp server.
From forum of Winzip, I found that if you want to run winzip job in batch mode than first time should run manually and winzip open one dialog box. Tick to do not ask again checkbox. so that task scheduler do not wait for prompt.
for the first part, winzip not working, you need to review your winzip job file and post here the failing part.
EDIT Ooops I was wrong.... you don't need to enclose the path in quotes. Sorry.
CD needs you to enclose the path in quotes, instead of
cd /d c:\Program Files (x86)\WinZip\
try
pushd "c:\Program Files (x86)\WinZip\"
for the second part, ftp not working, again you need to review your winzip job file and post here the failing part.

Resources