Windows Batch

12K

Solutions

11K

Contributors

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

Share tech news, updates, or what's on your mind.

Sign up to Post

Hello experts,

I have the following windows batch file:

for /f "tokens=1-3 delims=/" %%a in ('date /T') do set jour=%%a&set mois=%%b&set anneedeb=%%c
for /f "tokens=1-3 delims=:" %%a in ('echo %time%') do set heures=%%a&set minutes=%%b&set secondesdeb=%%c

SET annee=%anneedeb: =%
SET secondes=%secondesdeb: =%

SET VAR_MYDATE=%annee%%mois%%jour%_%heures%%minutes%%secondes%
SET VAR_LOGFILE="log\check_nb_prj_max_apisynchro%VAR_MYDATE%.log"


echo check du nombre de projets de l'api de synchro
sqlcmd -b -d mstt -i check_nb_prj_max_apisynchro.sql -v V_LNKSERVER="%VAR_LNKSERVER%" -v V_DBNAME="%VAR_DBNAME_DEST%" -v V_SCHNAME="%VAR_SCHNAME%" -v V_TBLNAME="%VAR_TBLNAME%" -v V_THRESHOLD_PRC="%VAR_THRESHOLD_PRC%" 2>&1 >> %VAR_LOGFILE%
	   
IF NOT %ERRORLEVEL% == 0 GOTO FIN_ERREUR
:FIN
echo.
ECHO Everything is ok.
rem Add a success string at the end 
goto FIN_ALL
:FIN_ERREUR
echo.
ECHO !!! Error found!!!!!!
rem Add an error string at the end 

Open in new window


I need to add a complement action:

Add at the end “_success” or “_error” string at %VAR_LOGFILE%

I putted where should be operated the rename actions.

if you have questions, please contact me.

Thank you in advance for your help.
0
Hi all, when I log onto my PC at work, my startup folder includes a PowerShell script. The script simply checks whether certain network drives are mapped; and if not, then map them. When I work from home, however, I have to VPN and manually authenticate before the network drives are revealed to me. I can add a time delay to my script, or wait for a manual key press.

I am thinking that the delay can be automated, possibly with a wait condition. Perhaps a ping to an IP address inside the firewall. If you have any suggestions to improve this, please let me know. One limitation, I do not have admin privileges on the PC.
0
SQLCMD : perform a select

Hello,

I am looking for a sqlcmd command in order to:
Perform a select count of an specific table
If the count is above 250 exit else continue with the batch.

If you have questions please contact me.

Regards,
0
Hello experts,

I am trying to delete automatically windows tasks.

I found the following command however I get an error message as the name of the windows task doesn’t exist.

schtasks /delete /tn TASKNAME /f

I got an error message which says that the task doesn't exist.

Should I report the full path or the name with double quote ?

Thank you very much for your help.
0
On my Win 7 Pro machine I created a .bat file to xcopy my important data to an external drive.  It’s very simple.  It’s named ‘BackupWin74ToK.bat’ and looks like this.
IF EXIST k:\Mike GOTO NONEED
md k:\Mike
:NONEED
IF EXIST k:\Downloads GOTO NONEED01
md k:\Downloads
:NONEED01
xcopy "C:\Users\Mike74\Documents\Outlook Files\Verizon.pst"   "K:\Mike\" /s /y /f
xcopy "C:\Users\Mike74\Documents\Outlook Files\archive.pst"   "K:\Mike\" /s /y /f
xcopy "I:\My Documents\Access_Databases\DevFolder\*.*" "K:\Mike\ DevFolder \" /s /y /f

Open in new window


‘DevFolder’ has all of my client development work

When I execute this .bat file, it starts out fine.
Copies the first two files then starts copying everything in the \DevFolder

The issues is that at some point in the xcopy the process terminates without finishing and deletes file 'BackupWin74ToK'.  I need to resolve why it’s not finishing and why it is deleting file ‘BackupWin74ToK.bat’ when it terminates

1.      I’ve watched the screen as the xcopy execute.  It lists each files its copying in the command window while executing but I can’t pin down what is causing the failure.
Is there some way I can have the bat file create a hard copy trace of each files it copies so I can identify where it is cancelling?

2.      The surprising thing is that when it terminates the bat file is also gone from the drive.  It disappears.  I can’t even imagine why this is happening but it must have something to do with what is causing the cancellation.  Any ideas why the bat file disappears.
0
Can someone help to produce a simple script/batch?  Need to script rename file in the certain path.
So the c:\folder\filename.pdf, will be c:\folder\folder_filename.pdf

Appreciate anyone's help!
1
Need a powershell or windows batch (pref is batch) script that takes "disk 2" offline using diskpart. I can do it manually in command line but i need a complete working script



OS: Windows 10
0
Due to a GPO I have to keep enabling TLS 1.0 and SSL 3.0.  I’m looking for a way to automate that process.
0
Executing the "netsh wlan show profiles" command I need to save the names of wifi profiles to a txt file for policy wifi and another one for user wifi.

I am using the following code;

@echo off &setlocal
set "flag="
(for /f "tokens=1*delims=:" %%a in ('netsh wlan show profiles') do (
    if "%%a"=="User profiles" set flag=true
    if defined flag if "%%~b" neq "" (
        for /f "tokens=*" %%c in ("%%~b") do echo(%%c
    )
))>out.txt
type out.txt

Open in new window


This only works for user profiles  and not group policy profiles.

netsh wlan show profiles

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
    LOGISTICS

User profiles
-------------
    <None>


C:\Users\dmarrow\Desktop>

Open in new window


netsh wlan show profiles

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
    DISDWIFI-FCNS

User profiles
-------------
    HOMEWIFI


C:\Users\dmarrow\Desktop>

Open in new window



Even when I change it to this code, nothing works

@echo off &setlocal
set "flag="
(for /f "tokens=1*delims=:" %%a in ('netsh wlan show profiles') do (
    if "%%a"=="Group policy profiles (read only)" set flag=true
    if defined flag if "%%~b" neq "" (
        for /f "tokens=*" %%c in ("%%~b") do echo(%%c
    )
))>out.txt
type out.txt

Open in new window

1
I have a folder with a bunch of jpg and jpeg files that I need to compress. Using a simple batch file called compressall.bat with the following code:
@echo off
for %%f IN (*.jpg) DO magick -quality 12 "%%f" "%%f"
for %%f IN (*.jpeg) DO magick -quality 12 "%%f" "%%f"

Open in new window


I'm able to go into a directory and manually run this to compress down my image files in a folder on windows system. The magick command is part of the opensource tool imagemagick.

I have 18.282 folders to parse through all with different names. I know there has to be a scripted way of doing this operation.
0
I am working with a .csv file and I need to change some of the contents.  - let's name it newfile.csv
Below is an example of the content.  These are only 2 lines, however there could be multiple lines in one .csv file.
In this sample, each line starts with 9,J.. and in this case ends with 002.. however each line can be different..

I would like a simple .bat script which will look at position 4 first - if it sees 65-ABCDEF then it looks at position 101.
If position 101 is blank, then it looks in position 100, and looks at tast 6 digits of position 100,  takes those six numbers - removes the last didit, and
write the 5 digits in position 101 - without touching the rest of the data and saving it to a new file called newfile1.csv

So in the examples below, since in position 4 it sees 65-ABCDEF, it then looks in position 101 and sees that it's blank.. therefore it looks at position 100.
There in the first line it sees the number 12345678123452 - therefore it will look at the last 6 digits.. being 123452, remove the last digit, and then it will write those digits
in position 101.. so side by side positions 100 and 101 would look like this before it moves on to the next line..   12345678123452,12345,

Thanks in advance for any assistance you can provide.


9,J,01234,65-ABCDEF,N,1234/2,12345612314,,20190220,20190319,20190319,,1234567890000,Company Inc.,987 Street,,Somewhere,AB,ABC DEF,AM,,,1234567891234,ABC 1234,1 Boulevard,,Here,AB,ABC …
0
Hello experts,

I am having some space disk issues and I would like to monitor this without any installation.

My idea is to launch a command: powershell & windows batch or vbscript in order to:

1-Drill down root folder: C:\
2-List the various files sorted by sizes (highest to lowest) in a csv or txt file with specific locations

This will allows me to check the various files which takes the most and remove them to free up disk space.

If you have questions, please contact me.

Regards,
1
Hello all,
i am trying to map thee Z drive using
net use z: \\blabla
but that is asking for user name and then password as arguments.

i do not want to put the credentials in this batch file, instead would like the batch file to use the same credentials the user logged in with (same user name and same password)
can this be done, by cmd or by powershell ? if yes, how ?
0
Hi,

We are trying to generate Oracle tablespace usage report, save it in common folder as html, and then email to selected recipients from the contents of html attachments.
There are 3 parts to this script.

a) Windows Batch file to invoke sql script : This works fine along with correct html file generated from the Oracle sql plus
b) The saved attachment generated from sql server, DB_Report_Tablespace.html is appended with date time stamp using below power shell script. The original saved file attachment has size of 8 kb, but after appneding with date time stamp it comes to size of 1 kb with no data. Script is attached below
c) Email is sent out correctly to recipients but body of email returns blank.
Can you please advise if any tweak can be done to the below ps script?
#>
$date = Get-Date
$d = $date.day
$m = $date.month
$y = $date.year
Outputreport | out-file .\Reports\DB_Report_Tablespace_$d-$m-$y.html
##Invoke-Expression .\Reports\Server_Healthcheck_Report_$d-$m-$y.html
##Send email functionality from below line, use it if you want  
$smtpServer = "Smtp.xxx.yyy.zzzz"
$smtpFrom = "dba.Oracle.prod@zzzzzz"
$smtpTo = "tom@ccc.ddd.yyy"
$messageSubject = "Tablespace Uage report"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = @"
Attached is the Tablespace Usage Report for the $datestring please check and review issues.
"@
$message.Body += Get-Content …
1
Hello,

I need to write a script to detect a particular software guid exists in the registry on computers and run particular msiexec command line to uninstall it.  

For example, if 7B4192A1-84C4-4E90-A31B-B4847CA8E23C exists in HKLM, run msiexec /x {7B4192A1-84C4-4E90-A31B-B4847CA8E23C} /qn

I need to do that in a bat script.  Please advise how to do it.  

Many thanks.
0
Hello Experts,

I use
ping-a 

Open in new window

to get the IP address of a server.
I will like to find a command to do it the opposite. Enter the IP and get the primary computer name of the server.
I tried net stat and nslookup but it doesn’t work.
If you have a cmd or a powershell command to get this straightforward I am in.
Thank you very much for your help.
0
I'm looking for a script or portable tool (ideally a Windows batch or VB as my office laptop don't have
PS access but Pwrshell scripts are welcome as I can do it on my personal PC with more efforts) that
could read a file containing a lists of SHA256 hashes (line by line is fine), input into
   https://www.virustotal.com/#/home/search  (the 3rd tab), hit ENTER, click on "Details" tab,
extract out the values under MD5 & SHA-1 & populate into 2 columns in a text file.

If the value can't be found in virustotal, return a "Nil" value for both columns.

I often get threat Intels that give IOCs' hashes in SHA256 but the trendmicro tool (EDR) tool I have can
only accept MD5 or SHA-1  hash values

Though I can enter them manually into virustotal,
0
Hello experts,

I am looking for the shortcut command to remove selected text for powershell and cmd
I tried backspace, enter + backspace and it doesn’t work.
2019-02-08_10h01_33.png
I am aware of the fact that I can use sup key however is time consuming if you have huge command.
If you have reference site for powershell & cmd shortcuts keyboard I am also in.

Thank you in advance for your help.
1
Hello experts,

I am looking for a cmd command to copy one root folder to a destination folder.
I was thinking to a robocopy command but if powershell can cover the need I am in.

Requirements:

Destination folder contains multiple subfolders, they contains “.txt” and “.log” ie extensionS. “.txt” and “.log”
They start with the following Prefix: “Import”
The idea is to copy the various subfolder to destination folder and omitting the various files with those extensions and prefix listed bellow
Source folder: C:\Install
Destination folder: C:\New Install
Note: files can be located in different subfolders.

If you have questions, please contact me.
Thank you very much for your help
1
Hello All,

I have been running the below command on a lot of Windows 2012 and 2016 domain servers to install a software and it is working fine.  

msiexec /i installer_vista_win7_win8-64-3.2.1.51.msi /qn TRANSFORMS=installer_vista_win7_win8-64-3.2.1.51_WFS.mst

On one domain Windows 2012 server, the above command is run but the software is not installed.  I have tried Run as administrator and run it in a admin CMD prompt.  I have also restarted the server but same result.  

Any one who knows what else I could try, please advise.  

Thanks.
1
Hello experts,

I am looking for a command cmd or powershell to:
Search and list the various files from a root folder example contains a specific string and report information in a log file.
The search should be with drill down process and log reporting should be composed by file name and folder in which it has found.

Example: string *Reference*.
Root folder: C:\
Log should be generated in default download folder.

If you have questions, please contact me.
0
We use BatchPatch to push software, scripts, patches etc. to our systems.  I'm trying to push McAfee Patch 12 to all of our systems using \quiet parameter, it returns Deployment: Exit Code:0 (SUCCESS) which usually indicates it deployed successfully, however it's not installing.
0
Hello experts,

I am looking for the fastest way to delete the various subfolders from a rootfolder and exclude some folder withtout removing the root folder and reporting the exclude folder

Example
C:\rootfolder\ contains subfolders such as:

C:\rootfolder\ogs
C:\rootfolder\bin
C:\rootfolder\conf

The command should remove C:\rootfolder\ogs and C:\rootfolder\bin and keep C:\rootfolder\conf.

The objective is to launch the command withtout saving it a bat file.

If you have questions, please contact me.

Thank you very much for your help.
0
We have an external Hard Drive (Drive E:) that is shared by 4 end users, i.e., all end users save documents to this device.   This device is never taken off the network.  Although Carbonite is being used for offsite storage of all content on this drive, we'd like to have a local backup solution also.
Our intention is to have 2 USB hard drives that would be plugged in to an end users computer in late afternoon just before our end users leave work.  Device USB1 and Device USB2 would be used on Monday for an initial copy of the Folders / Files from Drive E:.   Then, the devices would be used on alternative nights from that time on.
I need a batch file that would:
   1) On Monday, copy all files from Drive E: to USB1 and USB2
        a) Folder and File Structure would have to be duplicated exactly
 I need a different batch file that would:
   1) On Tuesday late afternoon, only APPEND to Device USB1 those files that had been changed since the files were last saved on Device USB1.
   2) On Wednesday morning, Device USB1 would be unplugged and stored in a safe place.
       a) Device USB2 would be plugged in and be ready for use late afternoon.
   3) On Wednesday late afternoon, only APPEND to Device USB2 those files that had been changed since the files were last saved on Device USB2
   4) On Thursday morning, Device USB2 would be unplugged and stored in a safe place
       a) Device USB1 would be plugged in and be ready for use late afternoon.
   5) Process would …
0
I created a SSIS package and executing it in a Batch File. My SSIS package updates an excel spreadsheet but when i run my batch file a pop up window comes up asking me if I want to replace it since the file already exists.  This option doesn't occur when i run it in Visual Studio, Since my SSIS package code saves the file alreaady. how do i get around this with my batch file.

Batch File pop up.
0

Windows Batch

12K

Solutions

11K

Contributors

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.