I want to use a batch file to automatically extract files from a password protected rar archive.
I tried this code:
#echo off
UNRAR E -INUL -P ne2020 "%~dp0program.rar"
pause
but the output that the cmd window that showed to me tells me that unrar aren't an internal/external command.
how to fix that?
You can give a try with this batch file :
#echo off
Title Unzip Winrar Files in command line
color 0A
Mode con cols=75 lines=10
Set "file=%~dp0program.rar"
set "RAR_Password=ne2020"
Set Log=%~n0_UnzipLog.txt
for %%i in ("%file%") do ( set "DEST=%~dp0%%~ni" )
set strProgramFiles=%ProgramFiles%
if exist "%ProgramFiles(x86)%" set strProgramFiles=%ProgramFiles(x86)%
Set Unrar="%strProgramFiles%\WinRAR\UnRar.exe"
If not exist "%DEST%" MD "%DEST%"
If Exist %Unrar% ( Goto :Unrar ) else ( Goto :Fail )
:Unrar
%UNRAR% E -inul -y -p%RAR_Password% "%file%" "%DEST%">nul 2>&1
IF "%ERRORLEVEL%" EQU "0" ( GOTO Succes ) Else ( Goto Fail )
:Succes
Echo(
Echo All Files are unzipped succesfuly ! in "%DEST%"
Echo All Files are unzipped succesfuly ! in "%DEST%" > "%Log%"
Start "" /MAX "%Log%"
Timeout /T 3 /nobreak>nul
Exit /b
:Fail
Color 0C
Echo(
echo There was an error !
echo There was an error ! > "%Log%"
Start /MAX "" "%Log%
Timeout /T 3 /nobreak>nul
after I checked the comments I found the answers.
there is 2 ways.
1st:
putting unrar.exe file in the same directory with the batch file
2nd:
adding this line:
cd C:\Program Files\WinRAR\
without forgetting to not to separate between -p and the password.
Related
I was making a .bat code to send .txt files to a specific folder on my ftp server. But I also wanted a way to check if these files were really uploaded. I searched a lot on the internet and unfortunately, I realized that it can't be done using .bat command.
So I tried using another way: The .bat will send the files and then will take it back to the folder, if the file already exists in the folder it will show a message that the file was successfully uploaded.
I did this script below, but the part of the "checking if was uploaded" is not working right.
Someone can help me?
#echo off
#setlocal enableextensions
#cd /d "%~dp0"
mode 34,12
color 0a
Ping www.google.nl -n 1 -w 1000 >nul 2>nul
if errorlevel 1 (set internet=Nao foi possivel se conectar ao servidor) else (set internet=Conectado) >nul 2>nul
echo %internet%
if "%internet%"=="No connection" goto 1
if "%internet%"=="Conected" goto 2
:1
echo No connection
echo.
echo Try later...
echo.
pause
exit
:2
( echo open ftp.xxxxxxxxxxx.com
echo xxxxxxx
echo xxxxxxx
echo ascii
echo lcd "c:\Vendas Pay&Go\files"
echo cd "Vendas Cartões Pay&Go"
echo cd "ECO"
echo mput *.txt
echo bye
)> %temp%\ftpsend.dat
ftp -i -s:%temp%/ftpsend.dat >nul 2>nul
del /f /s /q %temp%\ftpsend.dat >nul 2>nul
( echo open ftp.xxxxxxx.com
echo xxxxxxx
echo xxxxxxx
echo ascii
echo lcd "c:\Vendas Pay&Go\files"
echo cd "Vendas Cartões Pay&Go"
echo cd "ECO"
echo mget *.txt
echo bye
)> %temp%\ftpsend.dat
ftp -i -s:%temp%/ftpsend2.dat >nul 2>nul
del /f /s /q %temp%\ftpsend2.dat >nul 2>nul
if %*.txt% exist goto3
:4
echo File was not uploaded
pause
:3
echo File Uploaded.
del /s /f /q "c:\Vendas Pay&Go\files\*.txt"
If you want to conditional execution to display a message you can do something like this. The && means the previous command was successful. The || means the previous command was not successful.
(ftp -i -s:%temp%/ftpsend.dat | find /I "file successfully transferred" >nul) && (echo File Successfully Transferred) || ( echo File not transferred. Tray again later.)
Updated version based on comment below
(ftp -i -s:%temp%/ftpsend.dat | find /I "file successfully transferred" >nul) && (
echo File successfully sent.
del /f /s /q "c:\Vendas Pay&Go\files\*.txt" >nul
) || (
cls
echo File not uploaded. Try later.
)
i have a batch file to compress one folder and rename it with date & time and it's working well.
now i need same file to compress more than one folder into one compressed rar file.
here is my batch:
#echo off
echo STARTING BACKUP...
echo %date% %time%
echo+
:: variables
set src=D:\test
set dest=D:\Backups
set filename=%DATE:/=-%_%TIME::=-%
set filename=%filename: =%
Set Rar=%ProgramFiles%\WinRar\WinRAR.exe
"%Rar%" a -m5 -ed -pEltyar -r %dest%\%filename%.rar "%src%"
echo Backup Completed!
#pause
You can try something like this :
#echo off
Title Compress multi-folders in one with Winrar
Mode 70,5 & color 0A
echo STARTING BACKUP...
Call :GetFileNameWithDateTime
echo %filename%
:: variables
set src="D:\test1","D:\test2"
set dest=D:\Backups
If not exist "%dest%" MD "%dest%"
Set Rar=%ProgramFiles%\WinRar\RAR.exe
for /f "delims=," %%i in ('echo %src%') do (
"%Rar%" a -inul -m5 -ed -hpEltyar -r "%dest%\%filename%.rar" "%%~i"
)
If "%errorlevel%" EQU "0" (
echo(
echo Backup Completed Successfully !
) Else (
echo( & color 0C
echo There was an error occured !
)
Timeout /T 10 /nobreak>nul & exit
::*********************************************************************************************
:GetFileNameWithDateTime
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set "MyDate=%%x"
set "filename=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%-%MyDate:~8,2%-%MyDate:~10,2%"
exit /b
::*********************************************************************************************
In a batch script I'm running sqlcmd in a loop that goes through a folder of SQL scripts. I would like to be able to pass the script/output file currently being worked on to a label exterior to the loop in the case of an error.
Here's sample code:
#echo off
#setlocal enabledelayedexpansion
SET _INSTANCE=someinstance
SET _DATABASE=somedatabase
SET "_SCRIPTFOLDER=D:\Scripts for Testing"
SET "_OUTPUTFOLDER=D:\Output for Testing"
FOR %%S IN (
"%_SCRIPTFOLDER%\*.sql"
) DO (
SET /P _MSGa=Generating CSV: %%~nS.csv ... <NUL
sqlcmd -b -S %_INSTANCE% -d %_DATABASE% -i "%%~fS" -s "|" -o "%_OUTPUTFOLDER%\%%~nS.csv" -W
IF ERRORLEVEL >= 1 GOTO sqlcomderrorhandling
SET /P _MSGb=file created. Removing header dashes ... <NUL
REM REM Remove the line with dashes below the header
#FINDSTR /r /b /v /c:"-*|" "%_OUTPUTFOLDER%\%%~nS.csv" > "%_OUTPUTFOLDER%"\tmp.txt
IF ERRORLEVEL >= 1 GOTO findstrerrorhandling
XCOPY /Y "%_OUTPUTFOLDER%"\tmp.txt "%_OUTPUTFOLDER%\%%~nS.csv" >NUL
IF ERRORLEVEL >= 1 GOTO copyerrorhandling
ECHO done.
)
DEL /Q /F "%_OUTPUTFOLDER%"\tmp.txt
GOTO done
:sqlcomderrorhandling
ECHO An error occurred while processing the file %%~nS.csv
:done
#pause
The last ECHO just outputs %~nS.csv, not the actual name of the CSV file. Do I need to utilize functions in some way to do what I want to do?
Based on part of rojo's first comment and my own, would this sort of structure not make more sense?
#Echo Off
Set "_INSTANCE=someinstance"
Set "_DATABASE=somedatabase"
Set "_SCRIPTFOLDER=D:\Scripts for Testing"
Set "_OUTPUTFOLDER=D:\Output for Testing"
If Not Exist "%_OUTPUTFOLDER%\" (Echo Output folder doesn't exist
Timeout 3 /NoBreak >Nul
GoTo :EOF)
If Not Exist "%_SCRIPTFOLDER%\*.sql" (Echo Source folder doesn't contain any SQL files
Timeout 3 /NoBreak >Nul
GoTo :EOF)
CD /D "%_SCRIPTFOLDER%" 2>Nul || (Echo Source folder, %_SCRIPTFOLDER%, is not available.
Timeout 3 /NoBreak >Nul
GoTo :EOF)
For %%A In (*.sql) Do (
Echo Generating CSV: %%~nA.csv ...
SQLCmd -b -S %_INSTANCE% -d %_DATABASE% -i "%%A" -s "|" -o "%_OUTPUTFOLDER%\%%~nA.csv" -W 2>Nul && (
Echo File created. Removing header dashes ...
Findstr "[^-|]" "%_OUTPUTFOLDER%\%%~nA.csv">"%_OUTPUTFOLDER%"\%%~nA.tmp" && (
Move /Y "%_OUTPUTFOLDER%"\%%~nA.tmp" "%_OUTPUTFOLDER%\%%~nA.csv">Nul 2>&1 || (
Echo A Move error occurred while moving %%~nA.tmp
Timeout 2 /NoBreak >Nul)) || (Echo A FindStr error occurred while removing header dashes
Timeout 2 /NoBreak >Nul)) || (Echo A SQLCmd error occurred while processing the file %%~nS.csv
Timeout 2 /NoBreak >Nul))
Pause
I'm making a batch file to download the virustotal uploader tool in order to upload a suspect file.
The problem is after being uploaded by this tool, it remain opened !
So, my question is : How to close correctly the virustotal uploader tool after uploading a file by batch ?
#echo off
Mode con cols=80 lines=3 & color 9E
Title VirusTotal Uploader by Hackoo 2017
::*********************************************************************************
:Main
Set "File2Upload=%windir%\system32\wscript.exe"
echo(
echo Please wait a while ! Uploading file to VirusTotal is in progress ...
Set "VirusTotalUploaderTool=%ProgramFiles%\VirusTotalUploader2\VirusTotalUploader2.2.exe"
If Not Exist "%VirusTotalUploaderTool%" (
Call:Downloading
) else (
Start /wait "" "%VirusTotalUploaderTool%" "%File2Upload%"
)
Taskkill /IM "VirusTotalUploader2.2.exe" /F
exit
::*********************************************************************************
:Downloading
Title Downloading VirusTotal Uploader Tool v2.2 by Hackoo 2017
Mode con cols=80 lines=3 & color 9E
Set "URL=https://www.virustotal.com/static/bin/vtuploader2.2.exe"
Rem Create "MyDownload" folder in the temporary folder
set "MyDownload_Folder=%temp%\MyDownload"
If Not Exist "%MyDownload_Folder%" MD "%MyDownload_Folder%"
Set "Setup_File=%MyDownload_Folder%\vtuploader2.2.exe"
echo(
echo Please wait a while ! downloading "vtuploader2.2.exe" is in progress ...
Rem Downloading vtuploader2.2.exe to the temporary folder
Call :Download "%URL%" "%Setup_File%"
cls
Color 9A
Title Installing "vtuploader2.2.exe" is in progress ...
echo(
echo Installing "vtuploader2.2.exe" is in progress ...
Rem Silent installation of vtuploader2.2.exe the uploading tool
Call :Install_Silently %Setup_File%
Rem Removing the download folder
Call :Clean %MyDownload_Folder%
Goto Main
Exit /b
::*********************************************************************************
:Download <url> <File>
Powershell.exe -command "(New-Object System.Net.WebClient).DownloadFile('%1','%2')"
exit /b
::*********************************************************************************
:Install_Silently <Setup_File>
"%~1" /S
exit /b
::*********************************************************************************
:Clean <Folder_Setup>
RD "%~1" /S /Q >nul
exit /b
::*********************************************************************************
I answser my self and i share my code with you !
This is a special batch in order to upload all your *.exe suscpects files located on your temporary folder with command line to VirusTotal.com in the same time.
Multi_VirusTotal_Uploader.bat
#echo off
Color 9B & Mode con cols=90 lines=5
Title "%~nx0" for Multi-files VirusTotal Uploader by Hackoo 2017
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
Set TmpLogFile=%tmp%\TmpLog.txt
If Exist %TmpLogFile% Del %TmpLogFile%
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >%TmpLogFile% 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Title "%~nx0" for Multi-files VirusTotal Uploader by Hackoo 2017
:Main
Mode con cols=90 lines=3 & color 9E
Set "VirusTotalUploaderTool=%ProgramFiles%\VirusTotalUploader2\VirusTotalUploader2.2.exe"
If Not Exist "%VirusTotalUploaderTool%" ( Call:Downloading )
Set "Files_List2Upload=%~dp0ListFiles.txt"
echo(
echo Creating a list of all *.exe files located on your temporary folder ...
Timeout /t 4 /NoBreak>nul
Dir /b /s /A:-D "%Temp%\*.exe" > "%Files_List2Upload%"
If Not Exist "%Files_List2Upload%" ( Goto :Error )
For /f "delims=" %%f in ('Type "%Files_List2Upload%"') Do (
Call:Upload2VirusTotal "%%~f"
)
Taskkill /im "VirusTotalUploader2.2.exe" /f >nul 2>&1
Exit
::*********************************************************************************
:Upload2VirusTotal <File>
Title "%~nx0" for Multi-files VirusTotal Uploader by Hackoo 2017
Cls
Set "File2Upload=%~1"
echo(
echo Please wait a while ! Uploading file "%~nx1" to VirusTotal is in progress ...
Start "" "%VirusTotalUploaderTool%" "%File2Upload%"
Timeout /t 10 /nobreak>nul
exit /b
::*********************************************************************************
:Downloading
Title Downloading VirusTotal Uploader Tool v2.2 by Hackoo 2017
Set "URL=https://www.virustotal.com/static/bin/vtuploader2.2.exe"
Rem Create "MyDownload" folder in the temporary folder
set "MyDownload_Folder=%temp%\MyDownload"
If Not Exist "%MyDownload_Folder%" MD "%MyDownload_Folder%"
Set "Setup_File=%MyDownload_Folder%\vtuploader2.2.exe"
echo(
echo Please wait a while ! downloading "vtuploader2.2.exe" is in progress ...
Rem Downloading vtuploader2.2.exe to the temporary folder
Call :Download "%URL%" "%Setup_File%"
cls
Color 9A
Title Installing "vtuploader2.2.exe" is in progress ...
echo(
echo Installing "vtuploader2.2.exe" is in progress ...
Rem Silent installation of vtuploader2.2.exe the uploading tool
Call :Install_Silently %Setup_File%
Rem Removing the download folder
Call :Clean %MyDownload_Folder%
Goto Main
::*********************************************************************************
:Download <url> <File>
Powershell.exe -command "(New-Object System.Net.WebClient).DownloadFile('%1','%2')"
exit /b
::*********************************************************************************
:Install_Silently <Setup_File>
"%~1" /S
exit /b
::*********************************************************************************
:Clean <Folder_Setup>
RD "%~1" /S /Q >nul
exit /b
::*********************************************************************************
:Error
cls & Color 4C
echo(
echo The file "%Files_List2Upload%" dos not exist !
Pause>nul
exit /b
::*********************************************************************************
I don't know how to code myself, just managed to find some from Google and try to compile it. But it didn't work as I expected. Recently I've found that there is a virus spreading among my place without the user realizing it. The batch file is supposed to remove the files from the specified drive's %temp% folder with the extension .exe with exactly 138784 bytes. This the batch file I came up with, it works for removing it from the temporary folder but not for removing the file on their removable drive:
#ECHO OFF
ECHO "Enter Drive letter"
set /p letter=
for /r %%f in (*.exe) do if %%~zF EQU 138784 del %%F /f
attrib -s -h -a /s /d %letter%:*.*
c:
cd %temp%
Del wdr201.exe /f
ECHO "Process completed."
Pause
You can give a try for this code inspired from this one : Hackoo_Virus_Cleaner.bat
#echo off
Mode con cols=80 lines=6 & Color 9E
Title Searching the Drive letter of your USB Key and clean *.lnk files by Hackoo 2016
Set TmpLogFile=%tmp%\TmpLog.txt
Set "LogFile=%UserProfile%\Desktop\Hackoo_Virus_Clean_%UserName%_Log.txt"
If Exist %TmpLogFile% Del %TmpLogFile%
If Exist %LogFile% Del %LogFile%
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=2" ^|find /i ":"') do (Set MyUSB=%%i)
cls
setlocal ENABLEDELAYEDEXPANSION
set _drive=%MyUSB%
If Exist !_drive! (
cls
echo.
echo #########################################################
echo Your usb key is connected as !_drive!
echo #########################################################
echo Hit any key to remove malicious files and unhide files ...
pause>nul
Cls
echo(
Echo Removing malicious files/unhiding files... Please wait, this may take a while...
del /s /f /q !_drive!\*.lnk>>"%TmpLogFile%"2>&1
Cmd /U /C Type "%TmpLogFile%" > "%LogFile%"
Start "" %LogFile%
attrib -s -h -a -r /s /d !_drive!\*.*
Explorer "!_drive!\"
) ELSE (
cls
color 0C
echo.
echo #########################################################
echo Your usb key is not detected
echo #########################################################
echo.
)
pause & exit